GESP C++ 真题 · 逐题精解
首页C++六级真题 › 2024年6月 › 第3题

GESP 2024年6月 C++六级 单选题 第3题

C++六级单选题2024年6月第3题

所属知识点:继承·多态·虚函数 难度要求:— 考频:—

运行下列代码,屏幕输出( )。
class shape { protected: int width, height;
public:
    shape(int a=0,int b=0){width=a;height=b;}
    virtual int area(){ cout<<"parent class area: "<<endl; return 0; }
};
class rectangle: public shape { public:
    rectangle(int a=0,int b=0):shape(a,b){}
    int area(){ cout<<"rectangle area: "; return width*height; }
};
class triangle: public shape { public:
    triangle(int a=0,int b=0):shape(a,b){}
    int area(){ cout<<"triangle area: "; return width*height/2; }
};
int main(){
    shape *pshape; rectangle rec(10,7); triangle tri(10,5);
    pshape=&rec; pshape->area();
    pshape=&tri; pshape->area();
}

正确答案:A

题目解析

area() 是虚函数,基类指针 pshape 指向谁就调用谁的 area(………

完整解析为会员内容二级及以上的逐题精讲需开通 VIP。一级解析全部免费。前往 GESPPASS 解锁

想系统刷完 GESP C++ 1~8 级真题,并查看每道题的逐题精讲?

进入 GESPPASS 开始练习