1477: 多重继承派生(1)--家具、床、沙发和沙发床
Description
Input
Output
每组测试数据输出具体格式详见Sample Output。
Sample Input
木材 870 双人床 2
钢材 410 单人床 1
Sample 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;
}