为完成超市管理系统,需要进行如下设计; (1)设计 个超市类Supmarket,包含超市号id、超市规格type、超市位置address、超市内商品列表、商品个数num等5个私有数据成员,超市总数量count、超市物品总价值sumOfValue等2个静念数据成员,1个构造函数,1个析构函数,2个静态成员函数getCount()和getSumOIValue(),1个成员函数display(),1个重载运算符“+”,以实现为超市增加货物的操作 (2)设计一个超市内商品类Goods(以“个”为单位),包含商品id、商品规格type、商品价格value、商品重量weight等4个私有数据成员,1个构造函数,1个成员函数display() (3)超市类与商品类是组合关系(即,每个超市内有多个商品,目不固定有多少商品)。 要求:请画出类图(5分),写出各类代码(Goods类5分,Supmarket类10分),且实现用于测函数(10分)。
时间: 2023-03-14 18:03:53 浏览: 143
类图:Supmarket
+id:int
+type:string
+address:string
+itemList:Goods[]
+num:int
+count:int
+sumOfValue:int
+Supmarket()
+~Supmarket()
+display()
+operator+()Goods
+id:int
+type:string
+value:int
+weight:int
+Goods()
+display()Supmarket类代码:
class Supmarket
{
private:
int id;
string type;
string address;
Goods* itemList;
int num;
static int count;
static int sumOfValue;public:
Supmarket(int id, string type, string address, Goods* itemList, int num)
:id(id), type(type), address(address), itemList(itemList), num(num){
count++;
sumOfValue += num;
}
~Supmarket(){
count--;
sumOfValue -= num;
}
static int getCount(){
return count;
}
static int getSumOfValue(){
return sumOfValue;
}
void display() {
cout << "id:" << id << endl;
cout << "type:" << type << endl;
cout << "address:" << address << endl;
cout << "itemList:" << endl;
for (int i = 0; i < num; i++)
{
itemList[i].display();
}
cout << "num:" << num << endl;
}
friend Supmarket operator+(Supmarket &s, Goods& g){
Goods* newItems = new Goods[s.num + 1];
for (int i = 0; i < s.num; i++)
{
newItems[i] = s.itemList[i];
}
newItems[s.num] = g;
return Supmarket(s.id, s.type, s.address, newItems, s.num + 1);
}
};Goods类代码:
class Goods
{
private:
int id;
string type;
int value;
int weight;public:
Goods(int id, string type, int value, int weight)
:id(id), type(type), value(value), weight(weight) {}
void display() {
cout << "id:" << id << endl;
cout << "type:" << type << endl;
cout << "value:" << value << endl;
cout << "weight:" << weight << endl;
}
};测试函数:
void testSupmarket(){
Goods g1(0, "apple", 10, 2);
Goods g2(1, "banana", 5, 4);
Goods goods[] = { g1, g2 };
Supmarket s(0, "supermarket", "China", goods, 2);
s.display();
cout << "count:" << Supmarket::getCount() << endl;
cout << "sumOfValue:" << Supmarket::getSumOfValue() << endl; Goods g3(2, "orange", 15, 6);
s = s + g3;
s.display();
cout << "count:" << Supmarket::getCount() << endl;
cout << "sumOfValue:" << Supmarket::getSumOfValue() << endl;
}
阅读全文
相关推荐








