首页 > 其他 > 详细

静态成员

时间:2016-05-13 02:19:57      阅读:188      评论:0      收藏:0      [点我收藏+]
#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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!