所以我有一个基类(Shape)和三个派生类,Circle、Rectangle 和 Square(Square 是从 Rectangle 派生的)我正在尝试实现 operator<<,它只是调用正确的显示函数来调用它。但是,我认为我的语法不正确。这是一个片段——我哪里出错了?
class Shape
{
public:
Shape(double w = 0, double h = 0, double r = 0)
{
width = w;
height = h;
radius = r;
}
virtual double area() = 0;
virtual void display() = 0;
protected:
double width;
double height;
double radius;
};
ostream & operator<<(ostream & out, const Shape & s)
{
s.display(out);
return out;
}
class Rectangle : public Shape
{
public:
Rectangle(double w, double h) : Shape(w, h)
{
}
virtual double area() { return width * height; }
virtual void display()
{
cout << "Width of rectangle: " << width << endl;
cout << "Height of rectangle: " << height << endl;
cout << "Area of rectangle: " << this->area() << endl;
}
};