静态函数无须创建一个类的对象,静态成员函数和静态成员变量都是独立于对象的,如果其中一个对象修改了静态变量数据,那么所有对象次变量的值都发生改变。静态函数可以通过对象来访问,也可以通过类名直接访问。
静态函数在多文件编译时,头文件用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;
}