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 Output
代码:
#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;
}
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!