有如下程序: class Base { public: int data; }; class Derived1 : public Base {}; class Derived2 : protected Base {}; int main() { Deriv

admin2020-06-29  34

问题 有如下程序:
class Base {
public:
int data;
};
class Derived1 : public Base {};
class Derived2 : protected Base {};
int main()
{
Derived1 d1;
Derived2 d2;
d1.data = 0;        // ①
d2.data = 0;        // ②
return 0;
}
下列关于程序编译结果的描述中,正确的是(     )。

选项 A、①②皆无编译错误
B、①有编译错误,②无编译错误
C、①无编译错误,②有编译错误
D、①②皆有编译错误

答案C

解析 本题考查公用继承和保护继承对基类成员的访问属性。在公用继承中,基类的公用成员和保护成员在派生类中保持原有访问属性,其私有成员仍为基类私有。在受保护继承中,基类的公用成员和保护成员在派生类中成了保护成员,其私有成员仍为基类私有。本题中Derived1公用继承Base,所以①编译正确,Derived2保护继承Base,所以②编译不正确。
转载请注明原文地址:https://kaotiyun.com/show/WZ8p777K
0

随机试题
最新回复(0)