Cover Image for C++ Overriding
122 views

C++ Overriding

The C++ function overriding is a fundamental concept in object-oriented programming (OOP) that allows a derived class (subclass or child class) to provide a specific implementation of a function that is already defined in its base class (superclass or parent class). Function overriding is achieved through inheritance and polymorphism and is used to implement the concept of runtime polymorphism. Here are the key aspects of function overriding in C++:

  1. Inheritance: Function overriding relies on the concept of inheritance. You have a base class with a virtual function, and one or more derived classes that inherit from the base class.
C++
 class Base {
 public:
     virtual void print() {
         std::cout << "Base class" << std::endl;
     }
 };

 class Derived : public Base {
 public:
     void print() override {
         std::cout << "Derived class" << std::endl;
     }
 };
  1. Virtual Functions: To enable function overriding, you declare a virtual function in the base class. A virtual function is a member function of a class that is expected to be overridden in derived classes. In the example above, print() is a virtual function in the Base class.
  2. override Keyword (C++11 and later): In C++11 and later versions, you can use the override keyword to indicate that a function in a derived class is intended to override a virtual function from the base class. This helps catch errors at compile time if there is a mismatch in the function signature.
C++
 void print() override {
     std::cout << "Derived class" << std::endl;
 }
  1. Function Signature Matching: When overriding a virtual function in a derived class, the function signature (return type, name, and parameter list) in the derived class must exactly match the function signature in the base class. The return type must be covariant, which means it can be the same or a derived class of the return type in the base class.
  2. Polymorphism: Function overriding enables polymorphism, which means that the appropriate function implementation is called at runtime based on the actual object type. This is achieved through pointers and references to the base class.
C++
 int main() {
     Base* ptr;
     Derived d;

     ptr = &d; // Pointer of base class type pointing to a derived class object
     ptr->print(); // Calls the print function of the derived class

     return 0;
 }

In this example, the print() function of the Derived class is called even though the pointer is of type Base.

  1. Destructor Override: When you have a base class with a virtual destructor, it’s good practice to override the destructor in derived classes if they manage resources. This ensures that the destructor of the derived class is properly called when objects are destroyed.
C++
 class Base {
 public:
     virtual ~Base() {}
 };

 class Derived : public Base {
 public:
     ~Derived() override {
         // Clean up resources specific to Derived
     }
 };

Function overriding is a powerful mechanism for building hierarchies of related classes with specialized behavior. It enables dynamic dispatch of functions based on the actual object type, allowing for flexible and extensible code.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS