静态成员

#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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值