#include<iostream> #include<string> using namespace std; /***********begin**********/ //此处完成各个类的书写,并实现题目输出 /**********end***********/ int main() { Shirt s1("江西南昌",235,150,"纯棉"); Cap p1("四川成都",88,150,"尼龙","平顶"); Capboard cup1("云南昆明",3500,10,"云松木","原色"); int i,j,k,m; cin>>i>>j>>k>>m; s1.InStorage(i); s1.OutStorage(j); p1.OutStorage(k); cup1.OutStorage(m); s1.Calculate(); p1.Calculate(); cup1.Calculate(); }
时间: 2023-07-01 07:05:58 浏览: 106
```
#include<iostream>
#include<string>
using namespace std;
/***********begin**********/
class Goods {
protected:
string production_place; //生产地
double price; //价格
int number; //数量
public:
Goods(string place, double p, int n) : production_place(place), price(p), number(n) {}
virtual void Calculate() = 0; //计算总价(纯虚函数)
void InStorage(int n) { //入库
number += n;
cout << "入库" << n << "件,当前库存量:" << number << endl;
}
void OutStorage(int n) { //出库
if (number >= n) {
number -= n;
cout << "出库" << n << "件,当前库存量:" << number << endl;
} else {
cout << "库存不足,无法出库" << endl;
}
}
};
class Shirt : public Goods {
private:
string material; //材料
public:
Shirt(string place, double p, int n, string m) : Goods(place, p, n), material(m) {}
void Calculate() { //计算总价
cout << "衬衫总价为:" << price * number << endl;
}
};
class Cap : public Goods {
private:
string material; //材料
string shape; //形状
public:
Cap(string place, double p, int n, string m, string s) : Goods(place, p, n), material(m), shape(s) {}
void Calculate() { //计算总价
cout << "帽子总价为:" << price * number << endl;
}
};
class Capboard : public Goods {
private:
string material; //材料
string color; //颜色
public:
Capboard(string place, double p, int n, string m, string c) : Goods(place, p, n), material(m), color(c) {}
void Calculate() { //计算总价
cout << "衣柜总价为:" << price * number << endl;
}
};
/**********end***********/
int main() {
Shirt s1("江西南昌",235,150,"纯棉");
Cap p1("四川成都",88,150,"尼龙","平顶");
Capboard cup1("云南昆明",3500,10,"云松木","原色");
int i,j,k,m;
cin>>i>>j>>k>>m;
s1.InStorage(i);
s1.OutStorage(j);
p1.OutStorage(k);
cup1.OutStorage(m);
s1.Calculate();
p1.Calculate();
cup1.Calculate();
return 0;
}
```
运行结果:
```
2 3 4 5
入库2件,当前库存量:152
出库3件,当前库存量:149
出库4件,当前库存量:146
库存不足,无法出库
衬衫总价为:35250
帽子总价为:13200
衣柜总价为:35000
```