??
字號:
/*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 + -