在基类中公有,子类私有继承,main()中不能被访问

源程序:

#include <iostream>
using namespace std;

class Base
{
private:
int x;
public:
Base(int a)
{
x=a;
}
int getX()
{
return x;
}
void show()
{
cout<<x<<endl;
}
};

class Derived:private Base
{
private:
int y;
public:
Derived(int b):Base(b)
{
y=b;
}
void show()
{
cout<<y<<endl;
}
};

int main()
{
Base b(10);
cout<<b.getX()<<endl;
Derived d(20);
cout<<d.Base::getX()<<endl; //在基类中公有,但是子类私有继承,即getX()在子类中是私有的,类之外的main()中不能被访问

return 1;
}

 

posted @ 2021-12-06 20:28  bobo哥  阅读(34)  评论(0编辑  收藏  举报