Inheritance and derivation of C + +

Inheritance is an important feature of C + +, which is not available in C language

Build a Building base class Building as the base class, which is used to store address and Building number. Build a residential class House to inherit Building,
It is used to store room number and area. In addition, Office class Office inherits Building and stores Office name and phone number.
Basic requirements:
Develop application program, establish residential and office object test and output relevant data submission program.

  • Code:

#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 <<"The address is:"<< address << endl;
		//getline(cin, address);
		cout <<"The building number is:"<<house_num << endl;
	}
protected:
	string address;             //address
	int house_num;              //Building number
};


class House:public Building
{
public:
	void Houses(int r_num, int ar)
	{
		room_num = r_num;
		area = ar;
	}
	void output()
	{
		cout <<"Room number:"<<room_num << endl;
		cout <<"The measure of area:"<<area <<"Square meter"<< endl;
	}
protected:
	int room_num;          //room number
	int area;               //The measure of 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:" << office_name << endl;
		cout << "phone number:" << tel_num << endl;
	}
protected:
	string office_name;		//Office name
	string tel_num;			//Phone number
};


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

	building.Buildings("Huang Chi intersection", 4);
	building.output();

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

	office.Offices("Drop out office","123456");
	office.output();

	system("pause");
	return 0;
}

 

Tags: C

Posted on Mon, 02 Dec 2019 01:23:24 -0800 by cool-palace-ceo