1477: 多重继承派生(1)--家具、床、沙发和沙发床
1477: 多重繼承派生(1)--家具、床、沙發和沙發床
Description
家具類Furniture包含3個私有成員數據:家具類型(string type),家具主材料(string mat)和家具價格(int price)。成員函數包括構造函數(家具類型默認為unknown),三個獲取私有成員數據的函數(GetType,GetMat,GetPrice)和輸出家具類各成員數據函數show(輸出格式詳見Sample Output)。 床類Bed由Furniture類公有派生而來,包含1個私有成員數據:床類型(string bedtype)。成員函數包括構造函數(家具類型默認為“床”),獲取私有成員數據床類型的函數GetBedType和輸出床的各成員數據值的函數Show(輸出格式詳見Sample Output)。 沙發類Sofa由Furniture類公有派生而來,包含1個私有成員數據:座位數(int seats)。成員函數包括構造函數(家具類型默認為“沙發”),獲取私有成員數據座位數的函數GetSeats和輸出沙發的各成員數據值的函數Show(輸出格式詳見Sample Output)。 沙發床類SofaBed由Sofa類和Bed類公有派生,包含2個成員函數:構造函數(家具類型默認為“沙發床”),輸出沙發床的各成員數據值的函數Show(輸出格式詳見Sample Output)。 完成上述各類的設計。main函數已給定(如下所示),提交時只需要提交main函數外的代碼部分。 int main() { string mat,bedtype; int price,seats,cas=0; while(cin>>mat>>price>>bedtype>>seats) { cas++; cout<<"Case #"<<cas<<":"<<endl; Furniture furniture(mat,price); Bed bed(bedtype,mat,price); Sofa sofa(seats,mat,price); SofaBed sofabed(seats,bedtype,mat,price); cout<<"Furniture:"; ? ?furniture.Show(); cout<<"Bed:"; ? ? ? ? ?bed.Show(); cout<<"Sofa:"; ? ? ? ? sofa.Show(); cout<<"SofaBed:"; ? ? ?sofabed.Show(); } return 0; }Input
包含多組數據(數據均正確合法) 每組測試數據1行,每行包含4個數據,第一個數據是字符串(表示家具主材料),第二個是整數(表示家具價格),第三個數據是字符串(表示床的類型),第4個數據是整數(表示座位數)。Output
每組測試數據輸出具體格式詳見Sample Output。
Sample Input?
木材 870 雙人床 2 鋼材 410 單人床 1Sample Output
Case #1: Function #1 is called! Function #1 is called! Function #3 is called! Function #1 is called! Function #5 is called! Function #1 is called! Function #5 is called! Function #3 is called! Function #7 is called! Furniture:unknown/木材/870 Function #2 is called! Bed:床/木材/雙人床/870 Function #4 is called! Sofa:沙發/木材/2/870 Function #6 is called! SofaBed:沙發床/雙人床/木材/2/870 Function #8 is called! Case #2: Function #1 is called! Function #1 is called! Function #3 is called! Function #1 is called! Function #5 is called! Function #1 is called! Function #5 is called! Function #3 is called! Function #7 is called! Furniture:unknown/鋼材/410 Function #2 is called! Bed:床/鋼材/單人床/410 Function #4 is called! Sofa:沙發/鋼材/1/410 Function #6 is called! SofaBed:沙發床/單人床/鋼材/1/410 Function #8 is called代碼:
#include <iostream>
#include <string>
using namespace std;
class Furniture
{
public:
Furniture(string m,int p,string t="unknown"):mat(m),price(p),type(t)
{
cout<<"Function #1 is called!"<<endl;
}
string GetType()
{
return type;
}
string GetMat()
{
return mat;
}
int GetPrice()
{
return price;
}
void Show()
{
cout<<type<<"/"<<mat<<"/"<<price<<endl;
cout<<"Function #2 is called!"<<endl;
}
private:
string type,mat;
int price;
};
class Bed:virtual public Furniture
{
public:
Bed(string b,string m,int p):Furniture(m,p,"床"),bedtype(b)
{
cout<<"Function #3 is called!"<<endl;
}
string GetBedType()
{
return bedtype;
}
void Show()
{
cout<<"床"<<"/"<<GetMat()<<"/"<<GetBedType()<<"/"<<GetPrice()<<endl;
cout<<"Function #4 is called!"<<endl;
}
private:
string bedtype;
};
class Sofa:virtual public Furniture
{
public:
Sofa(int s,string m,int p):Furniture(m,p,"沙發"),seats(s)
{
cout<<"Function #5 is called!"<<endl;
}
int GetSeats()
{
return seats;
}
void Show()
{
cout<<"沙發"<<"/"<<GetMat()<<"/"<<GetSeats()<<"/"<<GetPrice()<<endl;
cout<<"Function #6 is called!"<<endl;
}
private:
int seats;
};
class SofaBed:public Sofa,public Bed
{
public:
SofaBed(int s,string b,string m,int p):Furniture(m,p),Sofa(s,m,p),Bed(b,m,p)
{
cout<<"Function #7 is called!"<<endl;
}
void Show()
{
cout<<"沙發床"<<"/"<<GetBedType()<<"/"<<Furniture::GetMat()<<"/"<<GetSeats()<<"/"<<Furniture::GetPrice()<<endl;
cout<<"Function #8 is called!"<<endl;
}
};
int main()
{
? ? string mat,bedtype;
? ? int price,seats,cas=0;
? ? while(cin>>mat>>price>>bedtype>>seats)
? ? {
? ? ? ? cas++;
? ? ? ? cout<<"Case #"<<cas<<":"<<endl;
? ? ? ? Furniture furniture(mat,price);
? ? ? ? Bed bed(bedtype,mat,price);
? ? ? ? Sofa sofa(seats,mat,price);
? ? ? ? SofaBed sofabed(seats,bedtype,mat,price);
? ? ? ? cout<<"Furniture:";? ? furniture.Show();
? ? ? ? cout<<"Bed:";? ? ? ? ? bed.Show();
? ? ? ? cout<<"Sofa:";? ? ? ? ?sofa.Show();
? ? ? ? cout<<"SofaBed:";? ? ? sofabed.Show();
? ? }
? ? return 0;
}
總結
以上是生活随笔為你收集整理的1477: 多重继承派生(1)--家具、床、沙发和沙发床的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 喷管烧蚀仿真过程中的常见问题
- 下一篇: Torch和torchvision的安装