有如下程序: #include using namespace std; class AA { int k; protected: int n; void setK(int k) { this->k=k; } public: void setN(int n

admin2021-02-22  21

问题 有如下程序:
#include
using namespace std;
class AA {
int k;
protected:
int n;
void setK(int k) { this->k=k; }
public:
void setN(int n) { this->n=n; }
};
class BB: public AA { /*类体略*/ };
int main()
{
BB x;
x.n=1;              //1
x.setN(2);          //2
x.k=3;              //3
x.setK(4);          //4
return 0;
}
在标注号码的四条语句中正确的是(     )。

选项 A、1
B、2
C、3
D、4

答案B

解析 本题考查公用继承对基类成员的访问属性。在公用继承中,基类的公用成员和保护成员在派生类中保持原有访问属性,其私有成员仍为基类私有。所以本题中n、k和setK变为了派生类的保护成员,不能直接使用,即1、3、4错误,所以B选项正确。
转载请注明原文地址:https://kaotiyun.com/show/Cbfp777K
0

随机试题
最新回复(0)