第13周-项目1-基类中成员的访问限定符和派生类的继承方式

来源:互联网 发布:图片调整软件 编辑:程序博客网 时间:2024/06/06 05:18
In member function 'void StudentB::showB()':|error: 'int StudentA::num' is private|error: within this context|error: 'std::string StudentA::name' is private|error: within this context|error: 'char StudentA::sex' is private|error: within this context|num,sex,name在StudentA中是私有的不能再StudentA类外用,当然也包括StudentBIn function 'int main()':|error: 'void StudentA::showA()' is inaccessible|error: within this context|error: 'StudentA' is not an accessible base of 'StudentB'|stud2是StudentB定义的,因为是保护继承,stud2.showA( )只能在StudentB类中用私有继承也只能在类内调用总结:(1)采用公共继承,基类的公用成员和保护成员在派生类中仍然保持其公用成员和保护成员的属性,而基类的私有成员在派生类中并没有成为派生类的私有成员,它仍然是基类的私有成员,只要基类的成员函数可以引用,而不能被派生类的成员函数引用,因此就成为派生类的不可访问的成员。(2)采用私有继承,私有基类的公共成员和保护成员在派生类中的访问属性相当于派生类的私有成员,即派生类的成员函数能访问他们,而在派生类外不能访问它们。私有基类的私有成员在派生类中成为不可访问的成员。(3)采用保护继承,保护基类的共有成员和保护成员在派生类中都成了保护成员,其私有成员仍为基类私有,也就是把基类原有的公有成员也保护起来,不让类外任意访问。

原创粉丝点击