C++实验三 继承性和派生类

内容提要

建立一个楼房基类Building为基类,用于存储地址和楼号,建立住宅类House继承Building,用来存储房号和面积,另外建立办公室类Office继承Building,存储办公室名称和电话号码。

基本要求

编制应用程序,建立住宅和办公室对象测试之并输出有关数据提交程序。

源程序及运行结果

#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
 
class Building
{
public:
	void Buildings(string ad, int hnum)
	{
		address = ad;
		housenum = hnum;
	}
	
	string address;             
	int housenum;              
};
 
 
class House:public Building
{
public:
	void Houses(Building b,int rnum, int ar)
	{
		this->address = b.address;
		this->housenum = b.housenum;
		roomnum = rnum;
		area = ar;
	}
	void printfout()
	{
		cout <<"地址为:"<< address << endl;
		cout <<"楼号为:"<<housenum << endl;
		cout <<"房号:"<<roomnum << endl;
		cout <<"面积:"<<area <<"平方米"<< endl;
	}
protected:
	int roomnum;          
	int area;               
};
 
class Office:public Building
{
public:
	void Offices(Building b,string name, string tel)
	{
		this->address = b.address;
		this->housenum = b.housenum;
		officename = name;
		telnum = tel;
	}
	void printfout()
	{
		cout <<"地址为:"<< address << endl;
		cout <<"楼号为:"<<housenum << endl;
		cout << "办公室名称:" << officename << endl;
		cout << "电话号码:" << telnum << endl;
	}
protected:
	string officename;		
	string telnum;			
};
 
 
int main()
{
	Building building;
	House house;
	Office office;
 
	building.Buildings("金钻豪庭", 6);
 
	house.Houses(building,603,127);
	house.printfout();
 
	office.Offices(building,"逸夫办公室","12581");
	office.printfout();

	return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值