静态函数无须创建一个类的对象,静态成员函数和静态成员变量都是独立于对象的,如果其中一个对象修改了静态变量数据,那么所有对象次变量的值都发生改变。静态函数可以通过对象来访问,也可以通过类名直接访问。

静态函数在多文件编译时,头文件用static声明,实现文件可以不用在写static关键字。

头文件loan.h

#ifndef LOAN_H
#define LOAN_Hclass Loan{
public:Loan();Loan(double rate, int years, double amount);double getAnnualInterestRate();int getNumberOfYears();double getLoanAmount();void setAnnualInterestRate(double rate);void setNumberOfYesrs(int years);void setLoanAmount(double amount);double getMonthlyPayment();double getTotalPayment();static double getMonthlyPayment(double annualInterestRate,int numberOfYears, double loanAmount);static double getTotalPayment(double annualInterestRate,int numberOfYears, double loanAmount);private:double annualInterestRate;int numberOfYears;double loanAmount;
};#endif // loan.h

实现文件loan.cpp

#include "loan.h"
#include <cmath>
using namespace std;Loan::Loan(){annualInterestRate = 9.5;numberOfYears = 30;loanAmount = 100000;
}Loan::Loan(double rate, int years, double amount){annualInterestRate = rate;numberOfYears = years;loanAmount = amount;
}double Loan::getAnnualInterestRate(){return annualInterestRate;
}int Loan::getNumberOfYears(){return numberOfYears;
}double Loan::getLoanAmount(){return loanAmount;
}void Loan::setAnnualInterestRate(double rate){annualInterestRate = rate;
}void Loan::setNumberOfYesrs(int years){numberOfYears = years;
}void Loan::setLoanAmount(double amount){loanAmount = amount;
}double Loan::getMonthlyPayment(){return getMonthlyPayment(annualInterestRate, numberOfYears, loanAmount);
}double Loan::getTotalPayment(){return getTotalPayment(annualInterestRate, numberOfYears, loanAmount);
}double Loan::getMonthlyPayment(double annualInterestRate,int numberOfYears, double loanAmount){double monthlyInterestRate = annualInterestRate / 1200;return loanAmount * monthlyInterestRate / (1 - (pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
}double Loan::getTotalPayment(double annualInterestRate,int numberOfYears, double loanAmount){return getMonthlyPayment(annualInterestRate, numberOfYears,loanAmount) * numberOfYears * 12;}

测试文件main.cpp

#include <iostream>
#include "loan.h"
using namespace std;int main(){//Enter annual interest ratecout << "Enter yearly interest rate, for example 8.25: ";double annualInterestRate;cin >> annualInterestRate;//Enter number of yearscout << "Enter number of years as an integer, for example 5: ";int numberOfYears;cin >> numberOfYears;//Enter loan amountcout << "Enter loan amount, for example 120000.95: ";double loanAmount;cin >> loanAmount;Loan loan(annualInterestRate, numberOfYears, loanAmount);cout << "The monthly payment is: " << loan.getMonthlyPayment() << endl;cout << "The total payment is: " << loan.getTotalPayment() << endl;//use static menber function ex1cout << "The monthly payment is: " <<loan.getMonthlyPayment(annualInterestRate, numberOfYears, loanAmount) << endl;cout << "The total payment is: " <<loan.getTotalPayment(annualInterestRate, numberOfYears, loanAmount) << endl;//use static menber function ex2double rate = 8.25;int years = 10;double amount = 500000;cout << "The monthly payment is: " << Loan::getMonthlyPayment(rate, years, amount) << endl;cout << "The total payment is: " << Loan::getTotalPayment(rate, years, amount) << endl;return 0;
}

转载于:https://www.cnblogs.com/mocuishle/p/7987376.html

ex10_11修改Loan类相关推荐

  1. java.lang.Instrument 动态修改替换类代码

    java.lang.Instrument 动态修改替换类代码 | java.lang.Instrument包是在JDK5引入的,程序员通过修改方法的字节码实现动态修改类代码. 这通常是在类的main方 ...

  2. SAP UI5 应用开发教程之三十六 - 使用 Chrome 开发者工具 Elements 标签动态修改 CSS 类试读版

    一套适合 SAP UI5 初学者循序渐进的学习教程 教程目录 SAP UI5 本地开发环境的搭建 SAP UI5 应用开发教程之一:Hello World SAP UI5 应用开发教程之二:SAP U ...

  3. java 修改 枚举类字段_枚举枚举和修改“最终静态”字段的方法

    java 修改 枚举类字段 在本新闻通讯中,该新闻通讯最初发表在Java专家的新闻通讯第161期中,我们研究了如何使用sun.reflect包中的反射类在Sun JDK中创建枚举实例. 显然,这仅适用 ...

  4. 用Java写PTA 7-11 设计一个能处理异常的Loan类

    用Java写PTA 7-11 设计一个能处理异常的Loan类 定义一个贷款类Loan,其中有属性: annualInterestRate:double,表示贷款的年利率(默认值:2.5) number ...

  5. 模拟贷款,设计贷款类Loan,Loan类包括贷款年利率(annualInterestRate),贷款年限(numberOfYears)、贷款额(loanAmount)......

    设计贷款类Loan,Loan类包括贷款年利率(annualInterestRate),贷款年限(numberOfYears).贷款额(loanAmount),贷款日期(loanDate)成员变量,还包 ...

  6. 程序功能:创建打印机类Printer,定义抽象方法Print()。 创建针式打印机类DotMatrixtPrinter和墨式打印机InkpetPrinter两个子类,修改测试类,实现该打印机打印。

    程序功能:创建打印机类Printer,定义抽象方法Print(). 创建针式打印机类DotMatrixtPrinter和墨式打印机InkpetPrinter两个子类,并在各自类中重新print方法,编 ...

  7. Javassist实战-修改现有类

    对于新增类应用场景不常见,而修改现有类应用场景更多,比如常见的日志切面,权限切面. 修改现有.class文件 已有类新增方法 1.现有类Person public class Person {priv ...

  8. 修改Menu类增加普通员工,经理,管理员对应的功能菜单的方法

    分析: 1在menu类中增加3个方法显示不同的角色的功能菜单 2shou StaffMenu()显示普通员工功能菜单 3 showManagerMenu()显示经理功能菜单 4showAdminMen ...

  9. UE4 编辑器下修改蓝图类继承的父类 ReparentBlueprint C++

    一.添加代码,修改蓝图类继承的父类 .cpp #include "Kismet2/BlueprintEditorUtils.h" #include "Kismet2/Ki ...

  10. Django实战(11):修改Model类

    我们已经实现了卖方的产品维护界面,根据最初的需求,还要为买方实现一个目录页:买方通过这个界面浏览产品并可以加入购物车.通过进一步需求调研,了解到产品有一个"上架时间",在这个时间之 ...

最新文章

  1. ubuntu16.04 cuda9.0 cudnn Tensorflow GPU 1.10.0
  2. Hibernate事务处理
  3. ML之相似度计算:图像数据、字符串数据等计算相似度常用的十种方法简介、代码实现
  4. vue新版本和旧版本关闭eslint总结
  5. superset可视化-country map
  6. escape in ABAP and JavaScript
  7. [Javascript] Avoid Creating floats if they are not needed
  8. ssh报错java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to org.service.impl.EmpServi
  9. GitHub政府用户破万:开源成重塑政府新手段
  10. TechWeb:转载合作须知!
  11. Jmeter(四十七)_性能测试统计超时率
  12. 字模提取软件怎么放大_图片无损放大软件 Topaz Gigapixel AI
  13. 过程FMEA(PFMEA)步骤一:策划与准备
  14. sht20中写用户寄存器_哪位帮忙看看下,程序读取SHT20 的温度时就不行,无ACK反馈了...
  15. 2019备考[嵌入式系统设计师]之基础知识
  16. 排列组合Cnm的求法
  17. 撰写营销邮件:避开10 种常见雷区
  18. DDN周报|3月26日-4月1日
  19. 格兰杰因果关系检验(原理及Python实例)
  20. 国外程序员推荐的免费编程书籍资源

热门文章

  1. 《活着》读后感4500字
  2. Maven错误:was cached in the local repository, resolution will not be reattempted until the update
  3. php网页抓取浏览者手机号码_php 获取 手机浏览器的信息 获取手机号
  4. python3正则re的使用
  5. 迅雷下载到99.99%速度0kb/s怎么办?
  6. 产品经理技术脑:怎么看懂接口文档
  7. 51Nod1203 2012集训队答辩 JZPLCM
  8. 企业微信公众号怎么运营管理?
  9. 阿里云域名的注册到使用流程
  10. C语言求21000内最大素数(20983)