Super and Override Keywords in C++


In C++, the super and override keywords do not exist in the same way they do in languages like Java. However, similar functionality can be achieved using other mechanisms such as the virtual keyword for method overriding, and using explicit calls to base class methods to simulate the concept of super. This article will explain how C++ handles these concepts.

Override Functionality in C++

In C++, method overriding is done using the virtual keyword. When a base class function is declared as virtual, the function in the derived class can override it. C++ provides the override keyword (introduced in C++11) to explicitly indicate that a method is intended to override a base class method. While the override keyword is not required, it helps catch errors at compile time if the method does not properly override a base class method.

Example of Method Overriding using Virtual and Override

    #include <iostream>

    class Base {
    public:
        virtual void display() {
            std::cout << "Base class display method" << std::endl;
        }
    };

    class Derived : public Base {
    public:
        void display() override {
            std::cout << "Derived class display method" << std::endl;
        }
    };

    int main() {
        Base* obj = new Derived();
        obj->display();  // Calls Derived class method due to polymorphism
        delete obj;
        return 0;
    }
        

Output:

    Derived class display method
        

Super Functionality in C++

The super keyword in other languages (like Java) is used to refer to the superclass of the current object. In C++, the same behavior can be achieved using explicit calls to the base class methods. While there is no super keyword, we can access base class members directly by qualifying them with the base class name.

Example of Using Base Class Methods Explicitly

In the following example, we simulate the super behavior by calling the base class method directly:

    #include <iostream>

    class Base {
    public:
        void display() {
            std::cout << "Base class display method" << std::endl;
        }
    };

    class Derived : public Base {
    public:
        void display() {
            std::cout << "Derived class display method" << std::endl;
            Base::display();  // Explicitly calling the base class method
        }
    };

    int main() {
        Derived obj;
        obj.display();  // Calls Derived class method, then calls Base class method
        return 0;
    }
        

Output:

    Derived class display method
    Base class display method
        

Why Use the Override Keyword?

The override keyword in C++ serves a crucial role in preventing bugs during code refactoring or when modifying the inheritance hierarchy. Without override, if the base class method is changed (e.g., a parameter list is modified), the derived class method will no longer properly override the base class method. The compiler will not give an error, and the code might still compile and run incorrectly. The override keyword helps the compiler catch such errors at compile time.

Example of Error without Override

    #include <iostream>

    class Base {
    public:
        virtual void display(int x) {
            std::cout << "Base class display method with value: " << x << std::endl;
        }
    };

    class Derived : public Base {
    public:
        // Missing override keyword and mismatched signature (parameter)
        void display() {  // This should cause an error but will silently compile without override
            std::cout << "Derived class display method" << std::endl;
        }
    };

    int main() {
        Derived obj;
        obj.display();  // This will not call the correct base method as intended
        return 0;
    }
        

With the override keyword:

    #include <iostream>

    class Base {
    public:
        virtual void display(int x) {
            std::cout << "Base class display method with value: " << x << std::endl;
        }
    };

    class Derived : public Base {
    public:
        void display(int x) override {  // Correctly overridden method
            std::cout << "Derived class display method with value: " << x << std::endl;
        }
    };

    int main() {
        Derived obj;
        obj.display(10);  // Correctly calls the overridden method
        return 0;
    }
        

Output:

    Derived class display method with value: 10
        

Conclusion

In C++, while there is no super keyword, similar functionality can be achieved by explicitly using the base class name. The override keyword, introduced in C++11, helps ensure that a method properly overrides a base class method, preventing subtle bugs. Using these features, developers can take advantage of polymorphism while ensuring their code is correct and maintainable.





Advertisement