Open In App

Function Overriding in C++

Last Updated : 11 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A function is a block of code that performs a specific task. It takes input, processes it, and returns an output. Function Overriding in C++ is a type of polymorphism where a derived class redefines a function from its base class using the same name, return type, and parameters (i.e., the same function signature).

Conditions:

  • The base class function must be virtual.
  • The derived class must use the same signature.
  • It's a form of runtime polymorphism (dynamic binding).

How Does It Work?

  • If you call the function using a base class object, the base class version is executed.
  • If you call it using a derived class object, the derived class version is executed.

Example:

C++
class Animal {
public:
    virtual void sound() {
        cout << "Animal makes a sound\n";
    }
};

class Dog : public Animal {
public:
    void sound() override {  // Correct override
        cout << "Dog barks\n";
    }
};


Override Keyword in C++

To avoid mistakes when overriding functions, C++ introduced the override keyword.

Why use override?

  • Ensures you're actually overriding a virtual function.
  • If the signature doesn't match any base class function, the compiler gives an error.

Mistake Caught Example:

C++
class Dog : public Animal {
public:
    void Sound() override {  // Error: No matching function in base class
        cout << "Dog barks\n";
    }
};


Without override, this would compile but not override the base function - leading to unexpected behviour.

Real-Life Example of Function Overriding

Constitution of India:

India adopted principles from other countries and redefined them in its own context—just like overriding base behavior with derived custom logic.

Function Overriding with Pointers

In C++, function overriding with pointers is a common use case of polymorphism. When a base class has a virtual function, and a derived class overrides it, the function call is resolved at runtime based on the type of the object being pointed to, not the type of the pointer. Example:

C++
#include <iostream>
using namespace std;
class Base{
    public:
        virtual void display(){
            cout<<"Display from Base class"<<endl;
        }
};
class Derived: public Base{
    public:
        void display() override{
            cout<<"Display from derived class"<<endl;
        }
};
int main() {
    Base * basePtr;
    Derived derivedObj;
    basePtr=&derivedObj;
    basePtr->display();
    return 0;
}

Output
Display from derived class

Overriding Without Virtual Function

If the base function is not virtual, the derived function with the same name just hides the base function. This is called function hiding, not overriding.

C++
#include<iostream>
using namespace std;

class Animal {
public:
    void sound() {
        cout << "Animal sound\n";
    }
};

class Dog : public Animal {
public:
    void sound() {  // Not overriding, just hiding
        cout << "Dog barks\n";
    }
};

int main() {
    Animal* a = new Dog();
    a->sound();  // Output: Animal sound
}

Output
Animal sound

Advantages of Function Overriding

  1. Dynamic behavior at runtime.
  2. Promotes code reuse through inheritance.
  3. Changes in base class can affect derived classes easily.
  4. Enables design patterns like Strategy, Command, etc.
  5. Encourages loose coupling by coding to base class interfaces.

Limitations of Function Overriding

  1. Slightly slower than normal function calls due to virtual table lookup.
  2. Adds memory overhead (vtable, vptr).
  3. Complexity increases in large inheritance hierarchies.
  4. Mistakes in overriding may cause bugs if signatures don't match.
  5. Requires careful design to avoid maintenance issues.

What is final in C++?

The final keyword is used to prevent further overriding or inheritance of:

  1. Virtual functions — to stop them from being overridden in derived classes.
  2. Classes — to stop them from being inherited.

Usage of final

1. Final with Functions

Used to stop a derived class from overriding a virtual function.

C++
class Base {
public:
    virtual void show() final {  // Cannot be overridden
        cout << "Base show\n";
    }
};

class Derived : public Base {
public:
    void show() override {       // Error: cannot override final function
        cout << "Derived show\n";
    }
};

2. final with Classes

Used to stop a class from being inherited.

C++
class FinalClass final {
public:
    void display() {
        cout << "This is a final class\n";
    }
};

class SubClass : public FinalClass {  // Error: cannot inherit from final class
};

Why use final?

  • Safety: Prevent accidental overriding or inheritance.
  • Optimization: Helps compiler make better decisions for inlining and performance.
  • Design clarity: Clearly communicates that "this method/class should not be changed".

Function Overloading vs Function Overriding

FeatureFunction OverloadingFunction Overriding
Type of PolymorphismCompile-timeMostly Runtime (with virtual)
Inheritance RequiredNoYes
Function SignatureMust be different Must be same
ScopeSame classBase and Derived class
Execution Time BindingEarly binding (compile time)Late binding (runtime)
Keyword Required No special keywordNeeds virtual and (optionally) override
Can Be Done Multiple Times YesYes, but must follow inheritance rules

Summary

  • Function Overriding allows redefining inherited behavior to suit derived class needs.
  • The base function should be marked as virtual, and the derived function should ideally use override.
  • It enables runtime polymorphism, which is useful in building flexible, reusable, and loosely coupled code.
  • Avoid function hiding by always matching the exact signature and using override.

Article Tags :
Practice Tags :

Explore