0

In my C++ project I have a 'Shape' class and its child class, 'Square' in the .h file:

class Shape {
    public:
    Shape();
    Shape(int, int, string);
    virtual ~Shape();

    virtual void draw();

private:
    int length, width;
    string colour;
};

class Square : public Shape {
public:
    Square(int, int, string);
    void draw();
};

In the .cpp file I implemented the constructors etc.

Shape::Shape() : length(0), width(0) {}
Shape::Shape(int len, int wid, string col) : length(len), width(wid), colour(col) {}
Shape::~Shape() {} //destructor

virtual void Shape::draw(){};

Square::Square(int len, int wid, string col) : Shape(len, wid, col) {}
void Square::draw() {cout << "I am drawing a square" << endl;};

I'm trying to test it in the main function as follows, just by pushing a single square.

int main() {

int count(0);
vector<Shape*> shapes;
shapes.push_back(new Square(10, 10, "orange"));

for (Shape* shp : shapes) {
    cout << "\nShape " << count++ << ":";
    shp->draw();
} // end for

for (Shape* shp : shapes)
    delete shp;

return 0;

}

Ideally, the output would look like this:

Shape 0: I am drawing a square

However, all it's outputting is

Shape 0:

If I put some output into void Shape::draw(){}; like void Shape::draw(){cout << "I am just a shape" <<endl;}; The output is Shape 0:I am just a shape.

This tells me that the function just ends at the Shape::draw() function without getting overwritten. The code does build fine, but what is happening in .cpp and .h that's stopping the inheritance? Edit: Declaring the draw function as virtual hammered in the nail for the coffin.

  • 1
    In C++, static typing means that if you call a method on a `Shape*`, you will always call a `Shape` method, nothing derived. _Unless_ you use `virtual` methods. See here: https://stackoverflow.com/questions/2391679/why-do-we-need-virtual-functions-in-c – alter_igel Nov 30 '18 at 04:53
  • Because shape contains no `virtual` functions you do not have polymorphism working for you. Change `void draw();` to `virtual void draw();`. You'll also need a virtual destructor so that `delete` can know how to correctly delete subclasses of `Shape`. `virtual ~Shape(){}` will help out with that. – user4581301 Nov 30 '18 at 04:53
  • I did try to add in virtual and it threw errors. Let me post the details in the main post. @alter igel – RosaryLightning X Nov 30 '18 at 04:54
  • Ok, I made sure the Shape::draw() is declared as virtual function, but result remained the same. – RosaryLightning X Nov 30 '18 at 05:01
  • 2
    Works for me: https://ideone.com/mVHbKO Could you give us a [mcve] so we can track down where things are going sideways on you? – user4581301 Nov 30 '18 at 05:02
  • That is weird...maybe I'll restart my IDE and see if that kicks it. – RosaryLightning X Nov 30 '18 at 05:05
  • Ah never mind, a simple clean project did the trick. Would you mind posting your solution as an answer so I can accept? Many thanks again @user4581301 – RosaryLightning X Nov 30 '18 at 05:08
  • No need. This is certainly a duplicate, so it's better if I leave this unanswered for the roomba to sweep up. – user4581301 Nov 30 '18 at 05:59
  • Makes sense, thank you. Wish I could've dug up that question before asking, but didn't find it. – RosaryLightning X Nov 30 '18 at 06:18

0 Answers0