单选题
有如下程序:
#include<iostream>
using namespace std;
class TlestClass
{
int n;
public:
TestClass(int k):n(k){}
int get(){return n;}
int get()const{return n+1;}
};
int main()
{
TestClass p(5);
colast TestClass q(6);
cout<<p.get()<<q.get();
return 0;
}
执行后的输出结果是
____
。
【正确答案】
B
【答案解析】[解析] C++中对常对象的成员函数调用,将自动调用其常成员函数,程序中调用原型为"intget()const;"的函数,对于非常对象将调用原型为"int get();"的函数。因为首先用5对对象p进行了初始化,所以执行p.get()时直接返回5,而对于常对象则以6对q进行初始化,在调用q.get()时,将调用原型为"int get()const;"的函数.该函数将返回n+1,第二个输出应为7,所以本题答案为57。