C++ Function Overriding

C++ Function Overriding is a feature in C++ that allows a derived class to provide a new implementation for a virtual function that is already provided by the base class. The new implementation provided by the derived class is said to override the implementation provided by the base class.

To understand function overriding, let's consider the following C++ example program.

Function overriding is a key concept in object-oriented programming (OOP) that allows a derived class to provide a specific implementation for a function that is already defined in its base class. This enables polymorphism, where objects of different classes can be treated as objects of a common base class. In C++, function overriding is achieved through virtual functions and plays a crucial role in creating flexible and extensible code. This tutorial explores the principles, syntax, and best practices associated with function overriding in C++.

#include <iostream>

using namespace std;

class Shape {
public:
  void calculateArea() {
    cout << "Calculating area of Shape" << endl;
  }
};

class Rectangle : public Shape {
public:
  void calculateArea() {
    cout << "Calculating area of Rectangle" << endl;
  }
};

int main() {
  Shape shape;
  shape.calculateArea();

  Rectangle rectangle;
  rectangle.calculateArea();

  return 0;
}
Output
Calculating area of Shape
Calculating area of Rectangle

In this example, we have a Shape class with a calculateArea method that simply outputs a message. We also have a Rectangle class that derives from the Shape class. The Rectangle class provides its own implementation for the calculateArea method.

As we can see from the output, the implementation of the calculateArea method in the Rectangle class overrides the implementation in the Shape class. When we create a Shape object, its calculateArea method is called, and the output is "Calculating area of Shape". When we create a Rectangle object, its calculateArea method is called, and the output is "Calculating area of Rectangle".

In this way, C++ Function Overriding allows us to provide new implementations for Overridden functions in derived classes, allowing for better code reusability and more efficient use of inheritance.


Key Concepts in Function Overriding

  • Virtual Functions : Function overriding is achieved through the use of virtual functions. A virtual function is a function declared in the base class using the virtual keyword, indicating that it may be overridden by derived classes.
    class Base {
    public:
        virtual void functionToOverride() {
            // Base class implementation
        }
    };
    

  • Override Keyword : The override keyword is used in the derived class to explicitly indicate that the function is intended to override a virtual function from the base class. This helps catch errors at compile-time if the function signature in the derived class does not match that of the base class.
    class Derived : public Base {
    public:
        void functionToOverride() override {
            // Derived class implementation
        }
    };
    

  • Late Binding (Dynamic Binding) : Function overriding involves late binding or dynamic binding, where the actual function to be called is determined at runtime. This allows polymorphism, as the appropriate function is selected based on the type of the object.

Why Use Function Overriding ?

Function overriding is a fundamental OOP concept that brings several benefits.
  • Polymorphism : Objects of different derived classes can be treated as objects of a common base class, and the correct function is called based on the actual type of the object.

  • Extensibility : New functionality can be added to existing code without modifying the base class, promoting a modular and extensible design.

  • Flexibility : Derived classes can provide specialized implementations of functions, tailoring behavior to their specific needs.

Polymorphism and Function Overriding

  • Polymorphism in Action : Polymorphism allows objects of different types to be treated as objects of a common base type. Function overriding plays a central role in achieving polymorphism in C++.
    class Shape {
    public:
        virtual void draw() {
            cout << "Drawing a shape." << endl;
        }
    };
    
    class Circle : public Shape {
    public:
        void draw() override {
            cout << "Drawing a circle." << endl;
        }
    };
    
    void displayDrawing(Shape* shape) {
        // Calls the appropriate draw() based on the actual object type
        shape->draw();  
    }
    
    int main() {
        Circle circle;
        // Calls the draw() function specific to Circle
        displayDrawing(&circle); 
    
        return 0;
    }
    
    In this example, the displayDrawing function accepts a pointer to a Shape. Despite the actual type of the object being a Circle, the correct draw function is called at runtime, demonstrating polymorphic behavior.

  • Pure Virtual Functions and Abstract Classes : Pure virtual functions are virtual functions without an implementation in the base class. They are meant to be overridden by derived classes. A class containing at least one pure virtual function becomes an abstract class, and objects of abstract classes cannot be created.
    class AbstractShape {
    public:
        virtual void draw() = 0;  // Pure virtual function
    };
    
    class ConcreteCircle : public AbstractShape {
    public:
        void draw() override {
            cout << "Drawing a circle." << endl;
        }
    };
    
    In this example, AbstractShape is an abstract class with a pure virtual function draw. The ConcreteCircle class inherits from AbstractShape and provides a concrete implementation of the draw function.


Conclusion

Function overriding is a fundamental concept in C++ that enables polymorphism, extensibility, and flexibility in object-oriented design. By allowing derived classes to provide their own implementations for virtual functions declared in base classes, C++ supports late binding, allowing the actual function to be determined at runtime.

Understanding the syntax, rules, and best practices associated with function overriding is crucial for creating maintainable and robust C++ code. Polymorphism, achieved through function overriding and virtual functions, is a powerful tool for creating modular and extensible software systems.

As you continue to develop in C++, consider the principles of function overriding in the context of your projects. By following best practices and leveraging the capabilities of C++, you can create code that is not only efficient but also easy to understand and extend.