用c++实现设计一个销售员SalesPerson类(销售华为p60),数量10万台,现在销售员3人,经过一个星期的销售,1号销售了18909台,2号销售了39876台,3号销售了40021台,输出剩余库存完整代码
时间: 2024-10-08 10:13:57 浏览: 36
在C++中,我们可以创建一个名为`SalesPerson`的类来表示销售员,并维护华为P60手机的库存信息。这里是一个简单的示例代码,包含一个`SalesPerson`类、全局库存变量以及三个销售员实例:
```cpp
#include <iostream>
class SalesPerson {
public:
// 构造函数设定初始库存
SalesPerson(int initialStock) : totalStock(initialStock), soldCount(0) {}
// 销售函数减少库存并返回已销售的数量
int sell(int quantity) {
if (quantity <= totalStock) {
totalStock -= quantity;
soldCount += quantity;
return soldCount; // 返回当前销售量
} else {
std::cout << "Insufficient stock!" << std::endl;
return soldCount;
}
}
private:
int totalStock; // 总库存
int soldCount; // 已销售数量
};
int main() {
const int initialStock = 100000;
SalesPerson salesperson1(initialStock);
SalesPerson salesperson2(initialStock);
SalesPerson salesperson3(initialStock);
// 分配一周内的销售任务给每个销售员
for (int i = 1; i <= 7; ++i) {
salesperson1.sell(18909);
salesperson2.sell(39876);
salesperson3.sell(40021);
}
// 输出剩余库存
std::cout << "Remaining inventory after a week:" << std::endl;
std::cout << salesperson1.totalStock << " units by salesperson 1\n";
std::cout << salesperson2.totalStock << " units by salesperson 2\n";
std::cout << salesperson3.totalStock << " units by salesperson 3\n";
阅读全文