C++之继承性和派生类(练手题)

继承性C++的一个重要特性,这个性质在C语言中是没有的

建立一个楼房基类Building为基类,用于存储地址和楼号,建立住宅类House继承Building,
用来存储房号和面积,另外建立办公室类Office继承Building,存储办公室名称和电话号码。
基本要求:
编制应用程序,建立住宅和办公室对象测试之并输出有关数据提交程序。

  • 代码:

#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;

class Building
{
public:
	void Buildings(string ad, int h_num)
	{
		address = ad;
		house_num = h_num;
	}
	void output()
	{
		cout <<"地址为:"<< address << endl;
		//getline(cin, address);
		cout <<"楼号为:"<<house_num << endl;
	}
protected:
	string address;             //地址
	int house_num;              //楼号
};


class House:public Building
{
public:
	void Houses(int r_num, int ar)
	{
		room_num = r_num;
		area = ar;
	}
	void output()
	{
		cout <<"房号:"<<room_num << endl;
		cout <<"面积:"<<area <<"平方米"<< endl;
	}
protected:
	int room_num;          //房号
	int area;               //面积
};

class Office:public Building
{
public:
	void Offices(string of_name, string tel)
	{
		office_name = of_name;
		tel_num = tel;
	}
	void output()
	{
		cout << "办公室名称:" << office_name << endl;
		cout << "电话号码:" << tel_num << endl;
	}
protected:
	string office_name;		//办公室名称
	string tel_num;			//电话号码
};


int main()
{
	Building building;
	House house;
	Office office;

	building.Buildings("黄赤交角", 4);
	building.output();

	house.Houses(308,149);
	house.output();

	office.Offices("退学办公室","123456");
	office.output();

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40846862/article/details/89054409