有如下程序: #include<iostream> using namespace std; class TestClass { int n; public: TestClass(int k):n(k)

admin2020-07-23  11

问题 有如下程序:
    #include<iostream>
   using namespace std;
    class TestClass
    {
    int n;
    public:
    TestClass(int k):n(k){}
    int get(){return n;}
    im getOconst{return n+l;}
    };
    int main()
    {
    TestClass p(5);
    const TestClass q(6);
    cout<<p.get()<<q.get();
    return 0:
    }
    执行后的输出结果是(    )。

选项 A、55
B、57
C、75
D、77

答案B

解析 C++中对常对象的成员函数调用,将自动调用其常成员函数,程序中调用原型为"int got()const;"的函数,对于非常对象将调用原型为"int get();’’的函数。因为首先用5对对象p进行了初始化,所以执行p.get()时直接返回5,而对于常对象则以6对q进行初始化,在调用q.get()时,将调用原型为"int got()const;"的函数,该函数将返回n+1,第二个输出应为7,所以本题答案为57。
转载请注明原文地址:https://kaotiyun.com/show/TQyp777K
0

最新回复(0)