C++:继承2(建筑物)

继承2(建筑物)

Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByte
Total Submit:324 Accepted:233

Description

建立一个基类Building ,用来存储一座楼房的层数、房间数以及它的总平方英尺数。建立派生类Housing,继承Building,并存储下面的内容:卧室和浴室的数量,另外,建立派生类Office,继承Building,并存储灭火器和电话的数目。然后,编制应用程序,建立住宅楼对象和办公楼对象,并输出它们的有关数据。

Input

输入数据第一行为一个整数T,表示有T组数据。每组数据二行,每行包括五个数。

第一行:层数 房间数 总平方英尺数 卧室数 浴室数

第二行:层数 房间数 总平方英尺数 灭火器数 电话数

Output

对于每一组数据,分别输出有关数据。格式参照样例输出。

Sample Input

1
4 8 240.45 2 2
8 12 500.5 12 2

Sample Output

HOUSING:
Floors:4
Rooms:8
Total area:240.45
Bedrooms:2
Bathrooms:2
OFFICING:
Floors:8
Rooms:12
Total area:500.5
Extinguishers:12
Phones:2

代码块:

#include<iostream>
using namespace std;

class Building
{ 
   int Floors,Rooms;
   double Total_area;
public:
   Building(int f,int r,double t)
   {
   Floors=f;
   Rooms=r;
   Total_area=t;
   }
   void disp()
   {
   cout<<"Floors:"<<Floors<<endl;
   cout<<"Rooms:"<<Rooms<<endl;
   cout<<"Total area:"<<Total_area<<endl;
   }
};
class Housing:public Building
{
   int Bedrooms,Bathrooms;
public:
    Housing(int f,int r,double t,int be,int ba):Building(f,r,t)
    {
       Bedrooms=be;
       Bathrooms=ba;
    }
    void disp_1()
    { 
      cout<<"HOUSING:"<<endl;
      disp();
      cout<<"Bedrooms:"<<Bedrooms<<endl;
      cout<<"Bathrooms:"<<Bathrooms<<endl;}
};
class Office:public Building
{    
    int Extinguishers,Phones;
public:
     Office(int f,int r,double t,int ex,int ph):Building(f,r,t)
     {
      Extinguishers=ex;Phones=ph;
     }
     void disp_2()
    { 
     cout<<"OFFICING:"<<endl;
     disp();
     cout<<"Extinguishers:"<<Extinguishers<<endl;
     cout<<"Phones:"<<Phones<<endl;}
};
int main()
{ 
  int n;int a,b,d,e;double c;
  cin>>n;
  for(int i=0;i<n;i++)
  {
    cin>>a>>b>>c>>d>>e;
    Housing h(a,b,c,d,e);
    cin>>a>>b>>c>>d>>e;
    Office o(a,b,c,d,e);
    h.disp_1();
    o.disp_2();
    }
    return 0;

} 

你可能感兴趣的:(C++,继承)