Overriding a class member does not override a base class member

2 weeks ago 19
ARTICLE AD BOX

I am trying to solve an exercise but I keep hitting an error with my last toString() and I cannot understand what the problem is with it.

#include <iostream> using namespace std; class Shape { protected: string color; bool filled; public: Shape() { color = "white"; filled = false; } Shape(string c, bool f) { color = c; filled = f; } string toString() { return "Shape[color= " + color + "filled = " + (filled ? "true" : "false") + "]"; } double getArea = 0; }; class Rectangle : public Shape { protected: double width; double height; public: Rectangle() { width = 1.0; height = 1.0; } Rectangle(string c, bool f, double w, double h) :Shape(c , f) { width = w; height = h; } string toString() const override { return "Rectangle[width = " + width + "height = " + height + "]"; } };

On line 49, the compiler gives this error:

image

I am asked to do the following:

Override the toString() method to display the rectangle information in the following format: Rectangle[width=..., height=..., Shape[color=..., filled=...]].

But, it seems that the compiler is not happy with the way I am returning the width.

Remy Lebeau's user avatar

Remy Lebeau

612k36 gold badges525 silver badges882 bronze badges

Georges_ABG's user avatar

New contributor

Georges_ABG is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

10

Read Entire Article