#include<iostream>
using namespace std;
class Building{
public:
void get_value()
{
cout<<"Input floor_num ,room_num ,total_Area "<<endl;
cin>>floor_num>>room_num>>total_Area;
}
void display()
{
cout<<"floor_num :"<<floor_num<<endl;
cout<<"room_num:"<<room_num<<endl;
cout<<"total_Area:"<<total_Area<<endl;
}
private :
int floor_num ,room_num ;
double total_Area;
};
class House : private Building{
public:
void get_value1()
{
get_value();
cout<<"Input bedroom_num, bathroom_num"<<endl;
cin>>bedroom_num>>bathroom_num;
}
void display1()
{
display(); //在继承类内调用基类的公有函数操作基类的私有变量
cout<<"bedroom_num:"<<bedroom_num<<endl;
cout<<"bathroom_num:"<<bathroom_num<<endl;
}
private :
int bedroom_num, bathroom_num;
};
class Office: private Building{
public:
void get_value2()
{
get_value();
cout<<"Input fire_num, telephone_num"<<endl;
cin>>fire_num>>telephone_num;
}
void display2()
{
display();
cout<<"fire_num:"<<fire_num<<endl;
cout<<"telephone_num"<<telephone_num<<endl;
}
private:
int fire_num, telephone_num;
};
int main()
{
House house;
house.get_value1();
house.display1();
Office office;
office.get_value2();
office.display2();
}
C++类实现公有继承
最新推荐文章于 2024-07-04 09:17:35 发布