#include using namespace std; /* 继承方式分为三种:公共继承 保护继承 私有继承 */ class basepage1 {
public: int m_a; protected: int m_b;
private: int m_c; }; //公有继承 class g :public basepage1 { public: void func() {
m_a = 10;//父类中的公共权限成员 到子类中依然是公共权限
m_b = 10;//父类中的保护权限成员 到子类中依然是保护权限
//m_c = 10;//父类中的私有权限成员 子类访问不到
// cout << this->m_a << endl;
}
}; //保护继承 class basepage2 {
public: int m_a; protected: int m_b;
private: int m_c; };
class bh :protected basepage2 { public: void func() { m_a = 100; m_b = 100; //m_c = 100;父类中的私有成员访问不到 } };
void test01() { g gg; gg.m_a = 100; //gg.m_b = 100; 到gg中的g中的m_b是保护权限 类外访问不到 }
void test02() { bh b; //b.m_a = 10; 算然在父类中是公共权限但在bh中变为了保护权限,不能访问 //b.m_b = 10;
} class basepage3 { public: int m_a; protected: int m_b; private: int m_c;
}; class si :private basepage3 { public: void func() { int m_a = 1000;//父类共有权限变为私有权限 int m_b = 1000;//父类保护权限变为私有权限 //m_c = 1000; 父类私有属性依然不能访问
}
}; void test03() { si siyou; /siyou.m_a = 10; siyou.m_a = 10; siyou.m_b = 10; //均发生错误,因为在 子类中全部变为了私有权限 siyou.m_c = 10;/
} int main1() {
test01();
system("pause");
return 0;
}
标签:10,权限,方式,继承,void,int,父类,public From: https://blog.51cto.com/u_15729005/5738406