基类的保护成员,在子类中直接访问的例程

源程序:

#include<iostream>
using namespace std;

class A
{
protected:
int x;
public:
A(int a)
{
x=a;
}
int get_x()
{
return x;
}
void show()
{
cout<<"x="<<x<<endl;
}
};
class B:public A
{
private:
int y;
public:
B(int b):A(b)
{
y=b;
}
void show()
{
cout<<"A::x="<<x<<endl;
}
};

int main()
{
A m(10);
B n(20);
//cout<<m.x<<endl;
cout<<m.get_x()<<endl;
n.show();
return 1;
}

posted @ 2021-12-05 11:54  bobo哥  阅读(68)  评论(0编辑  收藏  举报