类继承的合法调用
#include <iostream>
using namespace std;
class A1{
public:
void show1() {
cout << "class A1" << endl;
}
};
class A2 : public A1{
void show2() {
cout <<"class A2" << endl;
}
};
class A3 : protected A2 {
void show3() {
cout << "class A3"<< endl;
}
};
int main()
{
A1 obj1;
A2 obj2;
A3 obj3;
obj1.show1(); //合法
obj2.show1(); //合法
obj3.show1(); //非法
obj3.show3(); //非法
return 0;
}
A1 : show1() public
A2 : show1() public show2() private
A3 : show1() protected show2() private show3() private
众所周知,类的保护成员函数、私有成员函数不能直接从外部调用,所以最后两个调用是非法的