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

GESP 2025年3月 C++六级 单选题 第15题

C++六级单选题2025年3月第15题

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

关于下面 Shape/Circle/Rectangle 多态代码,说法错误的是:
class Shape { protected: string name;
public:
    Shape(const string& n) : name(n) {}
    virtual double area() const { return 0.0; }
};
class Circle : public Shape { double radius;
public:
    Circle(const string& n, double r) : Shape(n), radius(r) {}
    double area() const override { return 3.14159*radius*radius; }
};
class Rectangle : public Shape { double width, height;
public:
    Rectangle(const string& n, double w, double h) : Shape(n), width(w), height(h) {}
    double area() const override { return width*height; }
};
int main() {
    Circle circle("MyCircle", 5.0);
    Rectangle rectangle("MyRectangle", 4.0, 6.0);
    Shape* shapePtr = &circle; cout << shapePtr->area() << endl;
    shapePtr = &rectangle; cout << shapePtr->area() << endl;
}

正确答案:A

题目解析

A 错误:基类指针指向派生类对象是合法的(多态的基础),不会编译错误。B、C………

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

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

进入 GESPPASS 开始练习