#include <iostream>
using namespace std;
class father
{
public: //可以被派生类访问,也能被派生类的对象直接访问
void room()
{cout<<"room:父亲的大房子我可以享受\n"<<endl;}
protected://只能被派生类的公有成员函数访问,不能被派生类的对象直接访问
void home()
{cout<<"home:父亲的大房子我可以享受\n"<<endl;}
private:
void secret()
{cout<<"父亲的秘密"<<endl;}
};
class son:public father
{
public: //未标明私有、保护、公有时,默认为私有
void enjoy()
{home();}
//void show()
//{secret();} //编译报错,间接也不能访问父类的private成员
};
int main()
{
son a;
a.room(); //直接访问
//a.home();//编译报错,不能直接访问protected成员
a.enjoy(); //间接访问
//a.show(); //编译报错,间接也不能访问父类的private成员
return 0;
}
运行结果:
room:父亲的大房子我可以享受
home:父亲的大房子我可以享受
Press any key to continue
C++ 继承 1--公有派生成员的公有、私有、保护成员的访问
最新推荐文章于 2024-05-06 18:25:27 发布