产品信息管理模块c++
时间: 2023-07-01 18:19:54 浏览: 33
一个简单的产品信息管理模块可以使用面向对象的思想,定义一个 `Product` 类来表示每个产品,然后使用 `vector` 容器来存储多个产品对象。以下是一个示例代码:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Product {
public:
Product(int id, string name, float price) {
this->id = id;
this->name = name;
this->price = price;
}
int get_id() const {
return id;
}
string get_name() const {
return name;
}
float get_price() const {
return price;
}
private:
int id;
string name;
float price;
};
class ProductManager {
public:
void add_product(Product product) {
products.push_back(product);
}
void remove_product(int id) {
for (auto iter = products.begin(); iter != products.end(); iter++) {
if (iter->get_id() == id) {
products.erase(iter);
break;
}
}
}
void print_products() {
for (auto product : products) {
cout << "商品编号:" << product.get_id() << endl;
cout << "商品名称:" << product.get_name() << endl;
cout << "商品价格:" << product.get_price() << endl;
}
}
private:
vector<Product> products;
};
int main() {
ProductManager manager;
manager.add_product(Product(1, "苹果", 5.0));
manager.add_product(Product(2, "香蕉", 3.0));
manager.add_product(Product(3, "橙子", 4.0));
manager.print_products();
manager.remove_product(2);
manager.print_products();
return 0;
}
```
该示例代码定义了一个 `Product` 类,包括商品编号、名称和价格三个属性,以及对这些属性的获取方法。然后定义了一个 `ProductManager` 类,用于管理多个产品对象,包括添加、删除和打印产品信息等方法。最后在 `main` 函数中创建 `ProductManager` 对象,添加三个产品对象,打印所有产品信息,删除一个产品对象,再次打印所有产品信息。