Search code examples
c++default-valueoverridingmethod-signature

Changing signature while Overriding in C++


I have a base class

class Shape{
    public:
        virtual int getArea()=0;
}

I want to change the signature while overriding like this:

class Rectangle : class Shape{
    public:
        int getArea(int someParameter = 0){
            return 0;
        }
}

Is it possible somehow to achieve this, as I am using default value for newly added parameter?


Solution

  • The only solution I can see is that you implement both int getArea() and int getArea(int), where one function can call the other (for example int getArea() { return getArea(0); }). You can not have the overload taking an argument have a default argument then.