标签:
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include <string>
class Account
{
public:
Account(std::string name, double money);
double getAccount() const;
void deposit(double money);
void applyint();
static void rate(double newRate); // 静态成员函数只能访问静态数据成员,只能调用静态成员函数,
private:
std::string owner;
double amount;
static double interestRate; // 这个就是静态数据成员,这个是所有的对象共用的,单独保存在数据存储区里边,
// 这里只是一个声明,没有分配内存,在定义的时候才分配内存,在这里不能初始化,
};
#endif#include "Account.h"
double Account::interestRate = 0.066; // 这个是函数的定义,
Account::Account(std::string name, double money) : owner(name), amount(money) {}
double Account::getAccount() const
{
return this->amount;
}
void Account::deposit(double money)
{
this->amount += money;
}
void Account::applyint()
{
this->amount += this->amount * interestRate;
}
void Account::rate(double newRate)
{
interestRate = newRate;
}# include <iostream>
# include "Account.h"
using namespace std;
int main()
{
Account a("小崔", 8000);
Account b("崔", 6000);
Account c("崔崔", 6688);
//a.deposit(10);
cout << a.getAccount() << endl;
Account::rate(0.1); // 在这里是我们调整所有账号的类,
a.applyint();
cout << a.getAccount() << endl;
return 0;
}标签:
原文地址:http://blog.csdn.net/qq_31248551/article/details/51347882