有如下类声明: class Base{ protected: int amount; public: Base(int n=0):amount(n){} int getAmountconst{retum amount;} }; class Derived:public Base{ protected: int value; public: Derived(int m,int n):value(m),Base(n){} int getDataconst{return value+amount;} }: 已知

题目

有如下类声明: class Base{ protected: int amount; public: Base(int n=0):amount(n){} int getAmountconst{retum amount;} }; class Derived:public Base{ protected: int value; public: Derived(int m,int n):value(m),Base(n){} int getDataconst{return value+amount;} }: 已知x是一个Derived对象,则下列表达式中正确的是( )。

A.x.value+X.getAmount

B.x.getData一x.getAmount

C.x.getData一x.amount

D.x.value+X.amount


相似考题
更多“有如下类声明: class Base{ protected: int amount; public: Base(int n=0):amount(n){} in ”相关问题
  • 第1题:

    有如下类的声明: class Base{ protected: int amount; public: Base(int n=0):amount(n){} int getAmount()const{return amount;} }; class Derived:public Base{ protected: int value public: Derived(int m,int n):value(m),Base(n){} int getData()const{return value+amount;} };已知x是一个Derived对象,则下列表达式中正确的是

    A.x. value+x. getAmount()

    B.x. getData()-x. getAmount()

    C.x. getData()-x. amount

    D.x. value+x. amount


    正确答案:B

  • 第2题:

    有如下程序:include using namespace std;class Base{private:charc;public:Base(cha

    有如下程序:#include <iostream>using namespace std;class Base{private: char c;public: Base(char n) :c(n){} ~Base() { cout<<c; } };class Derived: public Base{private: char c; public: Derived(char n):Base(n+1),c(n) {} ~Derived() { cout<<c; }};int main (){ Derived obj ('x'); return 0;}执行上面的程序净输出

    A.xy

    B.yx

    C.x

    D.y


    正确答案:A
    解析:在C++中,由于析构函数不能被继承,因此在执行派生类的析构函数时,基类的析构函数也将被调用。执行顺序是先执行派生类的析构函数,再执行基类的析构函数,其顺序与执行构造函数的顺序正好相反。在此题的程序中,在主函数main结束时,派生类Derived对象obj将被删除,所以就会调用对象的析构函数。先调用派生类的析构函数,输出x,然后调用基类的析构函数,输出y。

  • 第3题:

    有如下程序:include using namespace std;class Base{private:char c;public:Base(ch

    有如下程序:#include <iostream>using namespace std;class Base{private: char c;public: Base(char n):c(n){} ~Base() { cout<<c; }};class Derived: public Base{private: char c;public: Derived(char n):Base(n+1),c(n){} ~Derived() { cout<<c; }};int main(){ Derived obj('x'); return 0; }执行上面的程序将输出( )。

    A.xy

    B.yx

    C.x

    D.y


    正确答案:A

  • 第4题:

    有如下程序: include using namespace std; class Amount{ int amount

    有如下程序: #include<iostream> using namespace std; class Amount{ int amount; public: Amount(int n=0):amount(n){} int getAmount()const{return amount;} Amount&operator9=(Amount a){ amount+=a. amount; return______; } }; int main(){ Amount x(3),y(7); x+=y, cout<<x. getAmount()<<endl; return 0; }已知程序的运行结果是10,则下划线处缺失的表达式是

    A.* this

    B.this

    C.&amount

    D.amount


    正确答案:A

  • 第5题:

    有下列程序:includeUsing namespace std;Class Amount{ int amount;public; Amount(i

    有下列程序: #include<iostream> Using namespace std; Class Amount{ int amount; public; Amount(int n=O):amount(n){} Int getAmount()const{return amount;} Amount &operator+=(AmountA) {

    A.*this

    B.this

    C.&amount

    D.amount


    正确答案:D
    解析: 此题考查的是“+”运算符重载和this指针。语句amount+=a.amount;实现3和7的求和,得到amount=10,要使程序的输出结果为10,需要把amount的值作为函数的返回值,所以横线处应填入amot。