C++ Function Overriding

In C++, function overriding, also known as “runtime polymorphism” or “dynamic polymorphism”, is a feature that allows a derived class to provide a different implementation of a method that is already provided by its base class. This allows for more flexibility and code reusability, as the derived class can provide a specialized implementation of the base class method that is more appropriate for its needs.

Example of Function Overriding

Here is an example of function overriding:

class Shape {
public:
    virtual void draw() {
        cout << "Drawing a shape." << endl;
    }
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing a circle." << endl;
}
};

In this example, the base class “Shape” has a method called “draw()” that simply outputs “Drawing a shape.”. The derived class “Circle” overrides this method to provide a more specialized implementation that outputs “Drawing a circle.”. This is done by using the “virtual” keyword before the function declaration in the base class, and by providing a new implementation of the same function in the derived class with the same function signature.

Example of Function Overriding with Parameters

We can also override the function with different parameters:

class Shape {
public:
    virtual void setColor(string color) {
        this->color = color;
    }
    string getColor() {
        return color;
    }
private:
    string color;
};
class Circle : public Shape {
public:
void setColor(string color, int transparency) {
this->color = color;
this->transparency = transparency;
}
int getTransparency() {
return transparency;
}
private:
int transparency;
};

In this example, the base class “Shape” has a method called “setColor” that takes one parameter and sets the color of the shape. The derived class “Circle” overrides this method to provide a more specialized implementation that takes two parameters, color and transparency. This allows the user to set the color and transparency of the circle shape. Also, we can see that the derived class “Circle” has a new method “getTransparency” which is not present in the base class “Shape”.

Example of Function Overriding with Return Types

We can also override the function with different return types:

class Shape {
public:
    virtual string getName() {
        return "Shape";
    }
};
class Circle : public Shape {
public:
char* getName() {
return "Circle";
}
};

In this example, the base class “Shape” has a method called “getName” that returns a string “Shape”. The derived class “Circle” overrides this method to provide a more specialized implementation that returns a char pointer “Circle”. Though, it is not a recommended practice to use different return types while overriding functions as it may lead to unexpected behaviors and runtime errors. It is always better to use the same return type as the base class method for consistency and easier debugging.

Final thoughts

Function overriding is a powerful feature in C++ that allows derived classes to provide specialized implementations of methods already provided by the base class. This allows for more flexibility and code reusability, and can lead to a cleaner and more efficient overall design. However, it is important to use this feature with caution and to always ensure that the return types and function signatures are the same in both the base and derived classes to avoid unexpected behaviors and runtime errors.

Leave a Reply

Your email address will not be published. Required fields are marked *