【发布时间】:2013-05-11 01:36:59
【问题描述】:
所以我有一个基类(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;
}
};
【问题讨论】:
-
显然,
display()应该将ostream & out作为参数,写入out,而不是cout。
标签: c++ class operators derived