Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Marked as override but does not override" Problem in OOP Code

I am trying to practice OOP in C++ but I am running into an issue regarding overriding of functions. In my Shape2D and Shape3D classes, I have virtual functions which I redefine in the Square and Sphere classes (ShowArea() and ShowVolume() respectively). However, when I redefine the function and try to run the main, it outputs the errors:

Shapes.cpp:88:14: error: 'void Square::ShowArea() const' marked 'override', but does not override
         
void ShowArea() const override{

Shapes.cpp:353:14: error: 'void Sphere::ShowVolume() const' marked 'override', but does not override
         
void ShowVolume() const override {

Below is a snippet of relevant code from both the Shape2D, Square, Shape3D, and Sphere classes.

class Shape2D : virtual public Shape {

public:

    virtual float Area() const = 0;

    void ShowArea() const;

    virtual string GetName2D() const = 0;
}



class Square: public Shape2D {

private:
    float squareLen;

public:

    // Constructors
    Square() {
        squareLen = 0;
    }

    Square(float len) {
        squareLen = len;
    }

    string GetName2D() const override {
        string res;

        return res;
    }

    // Returns the area of the shape
    float Area() const override {
        return (squareLen * squareLen);
    }

    void ShowArea() const override{
        cout << "Square Area: " << endl;
    }
}


class Shape3D : virtual public Shape {
    public:
        virtual float Volume() const = 0;
        void ShowVolume() const;
        virtual string GetName3D() const = 0;
}



class Sphere: public Shape3D {

private:
    Circle* SphereBase;

public:
    Sphere() {
        SphereBase = new Circle();
    }

    Sphere(float radius) {
        SphereBase = new Circle(radius);
    }

    float Volume() const {
        return (1.3333 * pi * pow(SphereBase->GetRadius(), 3));
    }

    void ShowVolume() const override {

    }

Why is this the case when I am redefining the function in the subclasses and the function is virtual in its original definition? It does not work for any of my shapes (I have 6 shapes but only included 2 in this post) so I dont think its a typo and its crashes across both 2D and 3D shapes so its not an issue with those specific classes.

like image 765
LeoChiappini Avatar asked Oct 15 '25 20:10

LeoChiappini


2 Answers

The function ShowArea declared in the class Shape2D

void ShowArea() const;

is not a virtual function. So this declaration in the derived class Square

void ShowArea() const override{
    cout << "Square Area: " << endl;
}

is incorrect.

Also the function ShowVolume declared in the class Shape3D is not a virtual function

void ShowVolume() const;

It may not be overridden in a derived class.

You need to declare the functions to be virtual in base classes that they could be overridden.

like image 85
Vlad from Moscow Avatar answered Oct 17 '25 08:10

Vlad from Moscow


The problem is that currently the member functions showArea and showVolume are not virtual member functions and we can use override keyword only when overriding a virtual member function.

To solve this you need to make showArea and showVolume virtual member functions by adding the keyword virtual as shown below:

class Shape2D : virtual public Shape {

public:

//--vvvvvvv------------------------->virtual added here
    virtual void ShowArea() const;
    //other code here 
};
class Shape3D : virtual public Shape {
    public:
//------vvvvvvv------------------------------>virtual  added here        
        virtual void ShowVolume() const;
        //other code here
};
//other code here
like image 20
Anoop Rana Avatar answered Oct 17 '25 10:10

Anoop Rana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!