通过类的公用方法操作类的私有成员
#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;
}