题目描述
某商店经销一种货物,货物成箱购进,成箱卖出,购进和卖出时均以重量为单位,各箱的重量不一样,因此,商店需要记录下库存货物的总重量,现要求用C++编程,模拟商店货物购进和卖出的情况。
主函数中,先输出商店货物的初始重量(初始为0),再增加两箱货物,货物重量由键盘输入,输出增加后商店货物总重量。
提示:需要定义静态数据成员存储库存货物的总重量。
输入描述
两箱货物的重量
输出描述
商店货物原始总重量
增加两箱货物后的总重量
输入样例
56
54
输出样例
货物的初始重量:0
此时货物的重量:110
#include <iostream>
using namespace std;
class Goods
{
public:
Goods(int w);
void add(int n);
int GettotalWeight();
private:
int weight;
static int totalWeight;
};
int Goods::totalWeight = 0;
Goods::Goods(int w)
{
this->weight = w;
}
int Goods::GettotalWeight()
{
return totalWeight;
}
void Goods::add(int n)
{
totalWeight= totalWeight+n;
}
int main()
{
Goods goods1(0);
int n1, n2;
cin >> n1>>n2;
goods1.add(n1);
goods1.add(n2);
cout << "货物的初始重量:0" << endl;
cout << "此时货物的重量:" <<goods1.GettotalWeight()<< endl;
return 0;
}