其他分享
首页 > 其他分享> > 在基类中公有,子类私有继承,main()中不能被访问

在基类中公有,子类私有继承,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;
}

 

标签:cout,show,int,子类,Derived,private,Base,基类,main
来源: https://www.cnblogs.com/duanqibo/p/15651693.html