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

 

上一篇:第十五届吉林省大学生程序设计竞赛


下一篇:chromium base::bind 用法