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

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

 基类 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;
}

#输出
在这里插入图片描述

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~祝今在

喝个茶水

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值