首页 > 其他分享 >通过类的公用方法操作类的私有成员

通过类的公用方法操作类的私有成员

时间:2022-10-26 21:02:24浏览次数:58  
标签:std toal goods 私有 成员 公用

#include<iostream>
using namespace std;

class goods
{
public:
	goods() {total =0;}
	void get_toal() {
		cout << "当前货物总量 total = "  << total << endl;
	}
	double add_total(double weigh) {total = total + weigh; return total;}
	double reduce(double weigh) {total = total - weigh; return total;}
private:
	double total;
};


int main()
{
	double in = 10;
	double out = 5;
	goods gd;
	gd.get_toal();
	cout << "进货" << in << "吨货物," ; 
	gd.add_total(in);
	gd.get_toal();
	cout << "出货" << out << "吨货物," ; 
	gd.reduce(out);
	gd.get_toal();
	return 0;
}

 

标签:std,toal,goods,私有,成员,公用
From: https://www.cnblogs.com/littleboss/p/16830005.html

相关文章