📄
字号:
/*24.某商场有如下的几种货品:衬衣、鞋子、帽子、裤子、冰箱、电视、立柜、壁橱、沙发。每一种货物都有详细的说明信息。
衬衣:布料、尺寸、单价、产地、库存量、所属货柜;
鞋子:皮料、尺寸、单价、产地、库存量、所属货柜;
帽子:布料、样式(平顶或尖顶)、尺寸、单价、产地、库存量、所属货柜;
裤子:布料、尺寸、单价、产地、库存量、所属货柜;
冰箱:制冷类型、样式(二门或三门)、颜色、尺寸、单价、产地、库存量、重量、所属货柜;
电视:样式(彩色或黑白)、颜色、尺寸、单价、产地、库存量、重量、所属货柜;
立柜:木料、颜色、尺寸、单价、产地、库存量、所属货柜;
壁橱:木料、颜色、尺寸、单价、产地、库存量、所属货柜;
沙发:木料、皮料、颜色、尺寸、单价、产地、库存量、所属货柜;
对这些商品的操作有:
新商品的录入,商品的进库,商品的出库,商品的调价,所属货柜的管理,库存的统计,总价格的计算,产地的统计。
要求自行设计数据结构,用类结构将上述的货品表示出来。在上一步的基础上,将上述的商品管理计算机化,完成操作要求的功能。*/
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>
///////////////////////////////////////////////////////////////////////////
class base //base类,为一个基类
{
double price; //单价
char place[20]; //产地
double size; //尺寸
public:
base() //无参构造函数
{
price=0;
size=0;
strcpy(place,"");
}
base (double pr,double sz, char* pl)
{
price=pr;
size=sz;
strcpy(place,pl);
}
void set_base(double pr,double sz, char* pl) //按所设数据生成对象
{
price=pr;
size=sz;
strcpy(place,pl);
}
void display()//显示base 类对象的有关数据
{
cout<<setw(10)<<price<<setw(10)<<size<<setw(10)<<place;
}
void input() //输入base类对象的有关数据
{
cin>>price>>size>>place;
}
double get_price()
{
return price;
}
char* get_place()
{
return place;
}
};
//////////////////////////////////////////////////////////////////////////////////////////
class shirt:public base //派生类shirt(衬衣)
{
char material[20]; //增加"布料"数据
public:
shirt():base()//派生类构造函数,无参
{
strcpy(material, "");
}
shirt(double pr,double sz, char* pl,char* mat):base (pr,sz,pl) //派生类构造函数,负责其基类的初始化
{
strcpy(material, mat);
}
void set_shirt(double pr,double sz, char* pl, char* mat)
{
set_base (pr,sz,pl); //按所设数据生成派生类对象
strcpy(material,mat);
}
void display()
{
base::display();
cout<<setw(10)<<material;
}
void input()
{
base::input(); //调用基类的input,输入“共性”数据
cin>>material;
}
char *get_material()
{
return material;
}
};
//////////////////////////////////////////////////////////////////////////////////////
class trousers:public shirt //派生类trousers,由shirt 派生
{
};
/////////////////////////////////////////////////////////////////////////////////////
class cap:public shirt //派生类cap(帽子),由shirt 派生
{
char style; //增加“样式”数据 m n o p
public:
cap():shirt() //无参构造函数
{
style=' ';
}
cap(double pr,double sz, char* pl,char* mat,char sty):shirt (pr,sz,pl,mat)//派生类构造函数,负责其基类的初始化
{
style=sty;
}
void set_cap(double pr,double sz, char* pl,char* mat,char sty)
{
set_shirt (pr,sz,pl,mat); //按所设数据生成派生类对象
style=sty;
}
void display ()
{
shirt::display(); //调用基类shirt 的display
cout<<setw(10)<<style<<endl;
}
void input()
{
shirt::input(); //调用基类的input,输入“共性”数据
cin>>style; //假设帽子的四种样式m,n,o,p
}
};
///////////////////////////////////////////////////////////////////////////////////////
class television:public base //派生类television,由base 派生
{
char color[20];//增加颜色数据
double weight;//增加重量数据
char style; //增加“样式”数据c 或b,表示彩色和黑白
public:
television():base() //无参构造函数
{
strcpy(color, "");
style=' ';
weight=0;
}
television(double pr,double sz, char* pl,char* co,char sty,double w):base (pr,sz,pl)//派生类构造函数,负责其基类的初始化
{
strcpy(color,co);
style=sty;
weight=w;
}
void set_television(double pr,double sz, char* pl,char* co,char sty,double w)
{
set_base (pr,sz,pl); //按所设数据生成派生类对象
strcpy(color,co);
style=sty;
weight=w;
}
void display ()
{
base::display(); //调用基类base的display
cout<<setw(10)<<color<<setw(10)<<style<<setw(10)<<weight<<endl;
}
void input()
{
base::input(); //调用基类的input,输入“共性”数据
cin>>color>>style>>weight;
}
};
///////////////////////////////////////////////////////////////////////////////////////
class sofa:public shirt //派生类sofa,由base 派生
{
char color[20];//增加颜色数据
char woodmaterial[20]; //增加木料数据
public:
sofa():shirt() //无参构造函数
{
strcpy(color, "");
strcpy(woodmaterial, "");
}
sofa(double pr,double sz, char* pl,char *mat,char *woodmat,char* co):shirt(pr,sz,pl,mat)//派生类构造函数,负责其基类的初始化
{
strcpy(color,co);
strcpy(woodmaterial,woodmat);
}
void set_sofa(double pr,double sz, char* pl,char *mat,char *woodmat,char* co)
{
set_shirt(pr,sz,pl,mat); //按所设数据生成派生类对象
strcpy(color,co);
strcpy(woodmaterial,woodmat);
}
void display ()
{
base::display(); //调用基类base的display
cout<<setw(10)<<woodmaterial<<setw(10)<<color<<endl;
}
void input()
{
base::input(); //调用基类的input,输入“共性”数据
cin>>woodmaterial>>color;
}
};
////////////////////////////////////////////////////////////////////////////////////////
const int MAXSIZE = 100;
class shirt_storage //衬衣仓库类shirt_storage
{
int count; //库存量
shirt shelf[MAXSIZE]; //衬衣货架shelf,存放一批衬衣
public:
shirt_storage() //库存量初始化为0
{ count=0; }
void display ()//显示对象数据
{
for (int i=0; i<count; i++) //衬衣货架中现有count个数据
{
shelf[i].display();
cout<<endl;
}
}
void in_something()//商品的进库(增加库存量count)
{
int add_cnt;
cin>>add_cnt;
shelf[count++].input(); //一次性输入该批同品种衬衣的有关数据
for (int i=1; i<add_cnt; i++)//“放到”衬衣货架shelf 中(共add_cnt 件)
{
shelf[count]=shelf[count-1];
count++;
}
}
void out_something()//商品的出库(减少库存量count)
{
int dec_cnt;
cout<<"请输入出库产品的数量:";
cin>>dec_cnt;
count-=dec_cnt; //取走最后放入的del_cnt 件衬衣
}
double total_price()//货品总价格的计算
{
double total=0;
for (int i=0; i<count; i++) //衬衣货架shelf 中各单价求和
total+=shelf[i].get_price();
return total;
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////
class trousers_storage:public shirt_storage
{
};
////////////////////////////////////////////////////////////////////////////////////////////
class cap_storage:public shirt_storage
{
public:
void in_something()
{
shirt_storage::in_something();
cin>>style;
}
private:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -