C++ Overriding

Introduction to Function Overriding in C++

In C++, function overriding is a feature that allows a derived class to provide a different implementation for a function that is already defined in its base class. This allows for polymorphism, where objects of different classes can be treated as objects of a common base class.

How Function Overriding Works

When a derived class overrides a function from its base class, it must have the same name, return type, and parameters as the function in the base class. The function in the derived class will replace the base class function when called using a derived class object.

To override a function, the base class function must be declared as virtual. This indicates that the function can be overridden by derived classes. If a function in the base class is not declared as virtual, it cannot be overridden.

Example of Function Overriding

Let’s consider a simple example to understand function overriding in C++. We have a base class called Shape which has a virtual function called area(). The derived classes, Rectangle and Circle, override the area() function to calculate the area specific to each shape.


#include <iostream>

class Shape {
public:
    virtual double area() {
        return 0;
    }
};

class Rectangle : public Shape {
private:
    double length;
    double width;
public:
    Rectangle(double l, double w) : length(l), width(w) {}
    double area() override {
        return length * width;
    }
};

class Circle : public Shape {
private:
    double radius;
public:
    Circle(double r) : radius(r) {}
    double area() override {
        return 3.14 * radius * radius;
    }
};

int main() {
    Shape* shape1 = new Rectangle(5, 3);
    Shape* shape2 = new Circle(4);

    std::cout << "Area of Rectangle: " << shape1->area() << std::endl;
    std::cout << "Area of Circle: " << shape2->area() << std::endl;

    delete shape1;
    delete shape2;

    return 0;
}

In the above example, the Shape class is the base class, and the Rectangle and Circle classes are the derived classes. Both derived classes override the area() function to provide their specific implementations.

In the main() function, we create two shape objects, one as a rectangle and another as a circle. We then call the area() function on each object. Since the area() function is declared as virtual in the base class, the appropriate implementation is called based on the actual object type.

The output of the above program will be:


Area of Rectangle: 15
Area of Circle: 50.24

As you can see, the area calculation is specific to each shape, and the correct implementation is called based on the object type.

Conclusion

Function overriding in C++ allows derived classes to provide their own implementation of a function defined in the base class. This feature enables polymorphism and allows objects of different classes to be treated as objects of a common base class. By using the virtual keyword in the base class function declaration, we indicate that the function can be overridden by derived classes.

Through the example provided, we can see how function overriding works in practice. By overriding the area() function in the derived classes, we are able to calculate the area specific to each shape and achieve the desired behavior.

Function overriding is an important concept in C++ programming, and understanding its usage and implementation is crucial for building flexible and extensible code.

Scroll to Top