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:virtual
.
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";
}
};
To avoid mistakes when overriding functions, C++ introduced the override keyword.
Why use override?virtual
function.
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.
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 PointersIn 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;
}
Display from derived classOverriding 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
The final keyword is used to prevent further overriding or inheritance of:
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?
virtual
and (optionally) override
Can Be Done Multiple Times Yes Yes, but must follow inheritance rules Summary
virtual
, and the derived function should ideally use override.
override
.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4