超市收银系统c++库存管理
时间: 2024-01-31 13:10:15 浏览: 39
超市收银系统的C++库存管理可以通过使用类和对象来实现。你可以创建一个名为"Inventory"的类来管理库存,并在该类中定义各种操作库存的方法。下面是一个简单的示例:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Inventory {
private:
string itemName;
int quantity;
double price;
public:
// 构造函数
Inventory(string name, int qty, double cost) {
itemName = name;
quantity = qty;
price = cost;
}
// 获取商品名称
string getItemName() {
return itemName;
}
// 获取商品数量
int getQuantity() {
return quantity;
}
// 获取商品价格
double getPrice() {
return price;
}
// 更新商品数量
void updateQuantity(int qty) {
quantity += qty;
}
// 更新商品价格
void updatePrice(double cost) {
price = cost;
}
};
int main() {
// 创建一个库存对象
Inventory item("苹果", 10, 5.99);
// 输出商品信息
cout << "商品名称: " << item.getItemName() << endl;
cout << "商品数量: " << item.getQuantity() << endl;
cout << "商品价格: " << item.getPrice() << endl;
// 更新商品数量和价格
item.updateQuantity(5);
item.updatePrice(6.99);
// 输出更新后的商品信息
cout << "更新后的商品数量: " << item.getQuantity() << endl;
cout << "更新后的商品价格: " << item.getPrice() << endl;
return 0;
}
```
这个示例中,我们创建了一个名为"Inventory"的类,它具有商品名称、数量和价格等属性,并定义了一些方法来获取和更新这些属性。在主函数中,我们创建了一个名为"item"的库存对象,并演示了如何使用该对象来管理库存。