码图:151 继承Building类(C++)

题目描述:

引入Building.h头文件,它的内容如下:

#include <iostream>
using namespace std;
class Building{
public:
	Building(char *name,int floor,int room,int area){
		strcpy(this->name,name);
		this->floor = floor;
		this->room = room;
		this->area = area;
	}
	virtual void display(){
		cout<<"Name:"<<name<<endl;
		cout<<"Floor:"<<floor<<endl;
		cout<<"Room:"<<room<<endl;
		cout<<"Area:"<<area<<endl;
	}
	Building * createTeachBuilding(char *name,int floor,int room,int area,char *function);
	Building * creatDormBuilding(char *name,int floor,int room,int area,int peoples);
protected:
	char name[20];
	int floor;
	int room;
	float area;

};

实现TeachBuilding类和DormBuilding类,这两个类继承至Building类,
除了Building的属性,TeachBuilding类增加代表教学楼功能的char function[20]属性
DormBuilding类增加代表容纳学生总人数的int peoples属性

完成TeachBuilding类和DormBuilding类的display方法,display方法输出类的全部属性。属性的输出格式参照Building类的dispaly方法。

必须实现Building类的createTeachBuilding和creatDormBuilding方法,它们的实现如下:

Building* Building::createTeachBuilding(char *name,int floor,int room,int area,char *function){
	return  new TeachBuilding(name,floor,room,area,function);
}

Building * Building::creatDormBuilding(char *name,int floor,int room,int area,int peoples){
	return new DormBuilding(name,floor,room,area,peoples);
}

它们的实现必选在TeachBuilding类和DormBuilding类的定义之后。

实现:

#include<iostream>
#include"Building.h"
using namespace std;

class TeachBuilding :public Building
{
private:
	char function0[20];
public:
	TeachBuilding(char* name, int floor, int room, int area,char* function) :Building(name, floor, room,area)
	{
		strcpy(function0, function);
	}
	void display() 
	{
		cout << "Name:" << name << endl;
		cout << "Floor:" << floor << endl;
		cout << "Room:" << room << endl;
		cout << "Area:" << area << endl;
		cout << "Function:" << function0 << endl;
	}
};
class DormBuilding :public Building
{
private:
	int people;
public:
	DormBuilding(char* name, int floor, int room, int area, int peoples) :Building(name, floor, room, area)
	{
		people = peoples;
	}
	void display()
	{
		cout << "Name:" << name << endl;
		cout << "Floor:" << floor << endl;
		cout << "Room:" << room << endl;
		cout << "Area:" << area << endl;
		cout << "Peoples:" << people << endl;
	}

};

Building* Building::createTeachBuilding(char* name, int floor, int room, int area, char* function) {
	return  new TeachBuilding(name, floor, room, area, function);
}

Building* Building::creatDormBuilding(char* name, int floor, int room, int area, int peoples) {
	return new DormBuilding(name, floor, room, area, peoples);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值