0
点赞
收藏
分享

微信扫一扫

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

alonwang 2022-06-08 阅读 46

源程序: 

#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;
}


举报

相关推荐

0 条评论