ex10_11修改Loan类

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

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

 

头文件loan.h

#ifndef LOAN_H
#define LOAN_H

class 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 rate
	cout << "Enter yearly interest rate, for example 8.25: ";
	double annualInterestRate;
	cin >> annualInterestRate;

	//Enter number of years
	cout << "Enter number of years as an integer, for example 5: ";
	int numberOfYears;
	cin >> numberOfYears;

	//Enter loan amount
	cout << "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 ex1
	cout << "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 ex2
	double 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值