我刚刚进入派生类,并且正在研究著名的Shape
类。Shape
是基类,那么我有三个派生类:Circle
、Rectangle
和Square
. Square
是 的派生类Rectangle
。我想我需要将派生类构造函数中的参数传递给基类构造函数,但我不确定该怎么做。我想在创建形状时设置它们的尺寸。这是我为基类和一个派生类所拥有的:
Shape(double w = 0, double h = 0, double r = 0)
{
width = w;
height = h;
radius = r;
}
class Rectangle : public Shape
{
public:
Rectangle(double w, double h) : Shape(double w, double h)
{
width = w;
height = h;
}
double area();
void display();
};
我在正确的轨道上吗?我收到以下编译器错误:expected primary expression before "double"
,在每个派生构造函数中。