A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/function-overriding-in-cpp/ below:

Function Overriding in C++ - GeeksforGeeks

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: How Does It Work? 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? 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
}
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? 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

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