单选题 若有以下程序: #include <iostream> using namespace std; class Base { private: int x; protected: int y; public: int z; void setx(int i) { x=i; int getx () { return x; } }; class Inherit : private Base { private: int m; public: int p; void setvalue(int a, int b, intc, int d) { setx (A) ; y=b; z=c; m=d; } void display() { cout<<getx () <<","<<y<<","<<z<<","<<m<<end1; } }; int main ( ) { Inherit A; A.setvalue(1,2,3,4); A.display (); return 0; }
【正确答案】 A
【答案解析】[解析] 本题考核私有继承中类成员的访问权限。当类的继承方式为私有继承时,基类公有成员和保护成员都以私有成员属性出现在派生类中。私有派生类的成员对其基类成员的访问权和公共派生的方式相同,但是,由私有派生的类声明的对象,不能访问任何基类的成员。 本题中,基类Base中的保护成员y和公有成员setx和getx,经过私有继承以后,称为派生类Inherit的私有成员,所以可以在派生类Inherit的函数成员中对它们进行访问。 类Inherit中的函数成员setvalue和display都是公有成员,所以可以通过Inherit的对象对它们进行访问。 本程序的功能是对类中各数据成员进行赋值,然后查看赋值是否正确。