C++--建立一个建筑物类的层次体系

    技术2022-07-11  76

    题目:建立一个建筑物类的层次体系。

     基类 Building 包括保护数据成员 name(建筑物名称)、 floors (层数)和 areas (总面积)  由 building 类派生住宅类 House(10分)  住宅类 House 包括私有数据成员 rooms(房间数) 和 balcony(阳台数);静态私有数据成员roomsCount,balconyCount,用于统计House类不同对象的房间总数和阳台总数。  公有函数成员:  带参构造函数House(int rooms1, int balcony1, string name1, int floors1, float areas1)  返回值为void的print()成员函数,输出格式如下:  返回值为void的静态成员函数count(),统计House类的不同对象的房间数和阳台数之和。输出格式如下:

     办公楼类 Office要求和House类似:(10分)  办公楼类 Office 包括私有数据成员 offices (办公室数)和 meetingrooms(会议室数),静态私有数据成员officesCount和meetingroomsCount。  公有成员如下:  构造函数。  print()函数,输出格式可参考后面的输出要求。  count()静态成员函数。

     在主函数中,实例化 House 类和 Office 类对象house1和house2以及office1和office2并且将其数据输出,输出时要由类的对象调用成员函数完成输出操作,得到输出结果。

    #include <iostream> #include <stdio.h> using namespace std; class Building { protected: string name; int floors; float areas; public: Building(string name1, int floors1, float areas1){ name=name1; floors=floors1; areas=areas1; } }; class House:public Building { private: int rooms; int balcony; static int roomsCount; static int balconyCount; public: House(int rooms1, int balcony1,string name1, int floors1, float areas1):Building(name1,floors1,areas1){ rooms=rooms1; balcony=balcony1; } void print(){ cout<<"大楼名:"<<name<<":"<<endl <<"楼层总数:"<<floors<<endl <<"总面积:"<<areas<<endl <<"House对象基本信息:"<<endl <<"房间数:"<<rooms<<endl <<"阳台数:"<<balcony<<endl; } int coun(){ return rooms*floors; } static void count(House a){ roomsCount=a.rooms*a.floors; balconyCount=a.balcony*a.floors; cout<<"数信大楼住宅统计:"<<endl <<"房间总数:"<<roomsCount<<endl <<"阳台总数:"<<balconyCount<<endl; } }; int House::roomsCount=0; int House::balconyCount=0; class Office:public Building { private: int offices; int meetingrooms; static int officesCount; static int meetingroomsCount; public: Office(int offices1,int meetingrooms1,string name1, int floors1, float areas1):Building(name1,floors1,areas1){ offices=offices1; meetingrooms=meetingrooms1; } void print(){ cout<<"大楼名:"<<name<<":"<<endl <<"楼层总数:"<<floors<<endl <<"总面积:"<<areas<<endl <<"Office对象基本信息:"<<endl <<"办公室数:"<<offices<<endl <<"会议室数:"<<meetingrooms<<endl; } static void count(Office a){ officesCount=a.offices*a.floors; meetingroomsCount=a.meetingrooms*a.floors; cout<<"数信大楼办公楼统计:"<<endl <<"办公室总数:"<<officesCount<<endl <<"会议室总数:"<<meetingroomsCount<<endl; } }; int Office::officesCount=0; int Office::meetingroomsCount=0; int main() { House house1(5,10,"数信大楼",1,100),house2(7,7,"数信大楼",1,100); Office office1(3,5,"数信大楼",1,100),office2(2,5,"数信大楼",1,100); house1.print(); house2.print(); office1.print(); office2.print(); cout<<endl; Office::count(office1); House::count(house1); return 0; }

    #输出

    Processed: 0.014, SQL: 9