Function Overriding in C++
Last Updated :
11 Aug, 2025
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;
}
OutputDisplay 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
}
Advantages of Function Overriding
- Dynamic behavior at runtime.
- Promotes code reuse through inheritance.
- Changes in base class can affect derived classes easily.
- Enables design patterns like Strategy, Command, etc.
- Encourages loose coupling by coding to base class interfaces.
Limitations of Function Overriding
- Slightly slower than normal function calls due to virtual table lookup.
- Adds memory overhead (vtable, vptr).
- Complexity increases in large inheritance hierarchies.
- Mistakes in overriding may cause bugs if signatures don't match.
- Requires careful design to avoid maintenance issues.
What is final in C++?
The final keyword is used to prevent further overriding or inheritance of:
- Virtual functions — to stop them from being overridden in derived classes.
- 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
Feature | Function Overloading | Function Overriding |
---|
Type of Polymorphism | Compile-time | Mostly Runtime (with virtual) |
Inheritance Required | No | Yes |
Function Signature | Must be different | Must be same |
Scope | Same class | Base and Derived class |
Execution Time Binding | Early binding (compile time) | Late binding (runtime) |
Keyword Required | No special keyword | Needs virtual and (optionally) override |
Can Be Done Multiple Times | Yes | Yes, 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
.
Explore
C++ Programming Language
5 min read
C++ Overview
C++ Basics
C++ Variables and Constants
C++ Data Types and Literals
C++ Operators
C++ Input/Output
C++ Control Statements
C++ Functions
C++ Pointers and References