AI题库 · GESP-C++六级题库
题目分享 #20445
关于下面代码,说法【错误】的是?( )
class Shape {
protected:
string name;
public:
Shape(const string& n) : name(n) {}
virtual double area() const {
return 0.0;
}
};
class Circle : public Shape {
private:
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 {
private:
double width; // 宽度
double 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 << "Area: " << shapePtr->area() << endl;
shapePtr = &rectangle;
cout << "Area: " << shapePtr->area() << endl;
return 0;
}
A
语句 Shape* shapePtr = &circle; 和 shapePtr = &rectangle; 出现编译错误
B
Shape 为基类, Circle 和 Rectangle 是派生类
C
通过继承, Circle 和 Rectangle 复用了 Shape 的属性和方法,并扩展了新的功能
D
Circle 和 Rectangle 通过重写(override)基类的虚函数 area 和基类指针,实现了运行时多态
AI题库 · 编程考级与信奥赛真题库
查看答案 →
复制链接后可在微信、QQ、小红书等平台粘贴分享;答案与解析在题目页。