#include<iostream>
using namespace std;
class Product {
private:
string name;//产品名称
double price;//单价
int quantity;//剩余产品数量
public:
//构造函数
Product(string n, double p, int q) :name(n), price(p), quantity(q) {}
//生产产品
void produce(int q) {
quantity += q;
}
//销售产品
void sell(int q) {
if (q > quantity) {
cout << "产品数量不足,无法销售" << endl;
}
else {
quantity -= q;
cout << "已销售" << q << "个产品,当前剩余" << quantity << "个产品" << endl;
}
}
//显示剩余产品数量
void showQuantity() {
cout << "当前剩余" << quantity << "个产品" << endl;
}
};
int main() {
Product p("手机", 1999.99, 100);//创建产品对象
p.showQuantity();//显示剩余产品数量
p.produce(50);//生产50个产品
p.showQuantity();
p.sell(30);//销售30个产品
p.showQuantity();
p.sell(80);//销售80个产品(超过了剩余产品数量)
return 0;
}
05-16
1769
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交