Topic 27 of 91 4. Functions in C++ Interactive Tutorial

Function Overriding in C++: Redefining Inherited Behavior

16 mins read Last updated: 26 Sep 2026 23,800 views

Direct Answer & Core Concept

Function overriding is an important concept in C++ object-oriented programming. It allows a derived class to provide its own implementation of a function that is already defined in its base class.

This feature is especially useful when different classes need to perform the same operation in different ways. For example, different animals can make different sounds, even though they all share a common makeSound() function.

Detailed Explanation & Mechanics

1. What Is Function Overriding in C++?

Function overriding occurs when a derived class provides its own implementation of a virtual member function inherited from a base class.

The overriding function generally has the same name and matching parameter list as the base class function. Its return type must also be compatible with the base function's return type.

Simple example

main.cpp
Run Online
#include <iostream>
using namespace std;

class Animal {
public:
    virtual void makeSound() {
        cout << "Animal makes a sound" << endl;
    }
};

class Dog : public Animal {
public:
    void makeSound() override {
        cout << "Dog barks" << endl;
    }
};

int main() {
    Dog d;
    d.makeSound();

    return 0;
}

Output:

Explanation

  • Animal is the base class.

  • Dog is the derived class.

  • The base class defines the makeSound() function.

  • The derived class provides a different implementation.

  • The virtual keyword enables runtime dispatch through base-class pointers or references.

  • The override keyword tells the compiler that the derived function is intended to override a virtual base-class function.

2. Syntax of Function Overriding

The general syntax is:

main.cpp
Run Online
class Base {
public:
    virtual void display() {
        // Base class implementation
    }
};

class Derived : public Base {
public:
    void display() override {
        // Derived class implementation
    }
};

Important points:

  1. The base class function must be virtual for runtime overriding.

  2. The derived class provides a matching function.

  3. The override keyword is optional but strongly recommended.

  4. The overriding function can contain completely different statements.

  5. The function is selected at runtime when called through a base-class pointer or reference.

3. Understanding the virtual Keyword

The virtual keyword tells C++ that a member function can be overridden in a derived class.

It enables dynamic binding, also called runtime polymorphism.

Example

main.cpp
Run Online
#include <iostream>
using namespace std;

class Parent {
public:
    virtual void show() {
        cout << "Parent class function" << endl;
    }
};

class Child : public Parent {
public:
    void show() override {
        cout << "Child class function" << endl;
    }
};

int main() {
    Parent p;
    Child c;

    p.show();
    c.show();

    return 0;
}

Output:

main.cpp
Run Online
Parent class function
Child class function

The base-class object calls the base implementation, while the derived-class object calls the overridden implementation.

4. Function Overriding Using a Base Class Pointer

One of the most important benefits of overriding is that a base-class pointer can call the derived-class implementation.

This is an example of runtime polymorphism.

Example

main.cpp
Run Online
#include <iostream>
using namespace std;

class Animal {
public:
    virtual void sound() {
        cout << "Animal makes a sound" << endl;
    }
};

class Cat : public Animal {
public:
    void sound() override {
        cout << "Cat says Meow" << endl;
    }
};

int main() {
    Animal* ptr;

    Cat c;

    ptr = &c;

    ptr->sound();

    return 0;
}

Output:

How does it work?

  1. ptr is a pointer to the base class Animal.

  2. The pointer stores the address of a Cat object.

  3. The sound() function is virtual.

  4. C++ checks the actual object type at runtime.

  5. The Cat implementation is executed.

This behavior is called dynamic binding or dynamic dispatch.

5. Function Overriding Without the virtual Keyword

If a base-class function is not virtual, defining a function with the same signature in a derived class does not provide runtime polymorphism.

The derived function hides the base function for ordinary name lookup in the derived class.

Example

main.cpp
Run Online
#include <iostream>
using namespace std;

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

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

int main() {
    Base* ptr;

    Derived d;
    ptr = &d;

    ptr->display();

    return 0;
}

Output:

Why?

The base-class function is not virtual. Therefore, the function call through Base* is resolved using the pointer's declared type.

Remember: For runtime function overriding, declare the base-class function as virtual.

6. Understanding the override Keyword

The override keyword is available in modern C++ (C++11 and later).

It explicitly tells the compiler that a derived-class function must override a virtual function from a base class.

If the function does not match a virtual base-class function, the compiler reports an error.

Example

main.cpp
Run Online
#include <iostream>
using namespace std;

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

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

int main() {
    Derived d;
    d.display();

    return 0;
}

Output:

Why should you use override?

  • It catches mistakes in function signatures.

  • It makes the programmer's intention clear.

  • It helps prevent accidental function hiding.

  • It improves code readability and maintainability.

For example, if the base function accepts an int, but the derived function accidentally accepts a double, override helps the compiler identify the mismatch.

7. Multiple Classes Using Function Overriding

A base class can have multiple derived classes, and each derived class can implement the same virtual function differently.

Example: Animal sounds

main.cpp
Run Online
#include <iostream>
using namespace std;

class Animal {
public:
    virtual void sound() {
        cout << "Animal makes a sound" << endl;
    }
};

class Dog : public Animal {
public:
    void sound() override {
        cout << "Dog barks" << endl;
    }
};

class Cat : public Animal {
public:
    void sound() override {
        cout << "Cat meows" << endl;
    }
};

class Cow : public Animal {
public:
    void sound() override {
        cout << "Cow moos" << endl;
    }
};

int main() {
    Dog d;
    Cat c;
    Cow cow;

    Animal* ptr;

    ptr = &d;
    ptr->sound();

    ptr = &c;
    ptr->sound();

    ptr = &cow;
    ptr->sound();

    return 0;
}

Output:

main.cpp
Run Online
Dog barks
Cat meows
Cow moos

Each derived object provides its own implementation of the same function.

This is a practical example of runtime polymorphism.

8. Calling the Base Class Function From an Overridden Function

Sometimes a derived class needs to execute the base-class implementation before or after its own implementation.

You can use the scope resolution operator :: to call the base-class function.

Example

main.cpp
Run Online
#include <iostream>
using namespace std;

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

class Derived : public Base {
public:
    void display() override {
        Base::display();

        cout << "Derived class display" << endl;
    }
};

int main() {
    Derived d;
    d.display();

    return 0;
}

Output:

main.cpp
Run Online
Base class display
Derived class display

Here, Base::display() explicitly calls the base-class implementation.

9. Function Overriding With a Virtual Destructor

When a class is designed to be used polymorphically, its destructor should generally be virtual.

This ensures that deleting a derived object through a base-class pointer invokes the derived-class destructor as well as the base-class destructor.

Example

main.cpp
Run Online
#include <iostream>
using namespace std;

class Base {
public:
    virtual ~Base() {
        cout << "Base destructor" << endl;
    }
};

class Derived : public Base {
public:
    ~Derived() override {
        cout << "Derived destructor" << endl;
    }
};

int main() {
    Base* ptr = new Derived();

    delete ptr;

    return 0;
}

Output:

main.cpp
Run Online
Derived destructor
Base destructor

Important: If you plan to delete derived objects through base-class pointers, use a virtual base-class destructor. Otherwise, deleting through that pointer has undefined behavior.

10. Rules of Function Overriding in C++

Keep these important rules in mind:

  1. The base-class function must be virtual for runtime overriding.

  2. The derived function must have a matching parameter list.

  3. The overriding function must have a compatible return type.

  4. The function cannot be overridden if it is declared final.

  5. The override keyword is optional, but recommended.

  6. The overriding function cannot have a more restrictive access level as a general requirement; C++ permits different access levels, although public access is commonly used.

  7. Constructors cannot be virtual or overridden.

  8. Destructors can be virtual and are commonly made virtual in polymorphic base classes.

  9. A virtual function can be overridden through multiple levels of inheritance.

  10. A derived-class function can explicitly call the base implementation using Base::functionName().

Return type compatibility

An overriding function normally uses the same return type as the virtual base function. C++ also permits certain covariant return types for pointers and references to related class types.

Example:

main.cpp
Run Online
class Base {
public:
    virtual Base* create() {
        return this;
    }
};

class Derived : public Base {
public:
    Derived* create() override {
        return this;
    }
};

Here, Derived* is a valid covariant return type because Derived is publicly derived from Base.

11. The final Keyword in Function Overriding

C++11 introduced the final keyword to prevent further overriding of a virtual function.

Example

main.cpp
Run Online
#include <iostream>
using namespace std;

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

class Derived : public Base {
    // void display() override; // Compilation error
};

int main() {
    Derived d;
    d.display();

    return 0;
}

Output:

The display() function is marked final, so a derived class cannot override it.

You can also mark an entire class as final to prevent inheritance from that class.

12. Function Overriding vs Function Overloading

Function overriding and function overloading are two different C++ concepts.

FeatureFunction OverridingFunction Overloading
MeaningRedefines inherited virtual behaviorUses the same function name with different parameters
ClassesRequires inheritanceCan occur within one class
ParametersMust match the base function's parameter listMust differ in a valid way
Virtual keywordRequired for runtime overridingNot required
BindingRuntime when called virtuallyUsually compile time
PurposeRuntime polymorphismCompile-time polymorphism

Overloading example

main.cpp
Run Online
#include <iostream>
using namespace std;

class Calculator {
public:
    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }
};

int main() {
    Calculator c;

    cout << c.add(10, 20) << endl;
    cout << c.add(5.5, 2.5) << endl;

    return 0;
}

Output:

Overriding example

main.cpp
Run Online
#include <iostream>
using namespace std;

class Parent {
public:
    virtual void show() {
        cout << "Parent class" << endl;
    }
};

class Child : public Parent {
public:
    void show() override {
        cout << "Child class" << endl;
    }
};

int main() {
    Child c;
    c.show();

    return 0;
}

Output:

13. Advantages of Function Overriding

Function overriding offers several benefits in object-oriented programming.

  • Runtime polymorphism: The correct implementation can be selected at runtime.

  • Code reusability: Derived classes can reuse the base-class structure.

  • Flexibility: Different derived classes can implement the same operation differently.

  • Extensibility: New derived classes can be added without changing code that works with the base-class interface.

  • Maintainability: Shared behavior can be organized in a common base class.

14. Common Mistakes in Function Overriding

Beginners often make these mistakes:

1. Forgetting the virtual keyword

Without a virtual base-class function, calls through base pointers do not use runtime dispatch.

2. Using different parameter types

A function with a different parameter list may hide the base function instead of overriding it.

3. Forgetting override

The compiler may not catch an unintended function declaration as an overriding mistake unless override is specified.

4. Confusing overriding with overloading

Overriding involves inherited virtual functions, while overloading involves multiple function signatures.

5. Forgetting a virtual destructor

Deleting a derived object through a base-class pointer requires a virtual base-class destructor.

6. Assuming virtual functions are always expanded at runtime

Virtual dispatch is based on the object's dynamic type when called through a base pointer or reference. Calls on objects and some qualified calls can be resolved directly.

15. Best Practices for Function Overriding

  • Use virtual for base-class functions that should support runtime polymorphism.

  • Use override for derived-class functions intended to override a base function.

  • Use final when further overriding should not be allowed.

  • Give polymorphic base classes virtual destructors.

  • Keep function names and responsibilities clear.

  • Avoid unnecessary inheritance when composition is a better design.

  • Test derived classes through base-class pointers and references.

16. Frequently Asked Questions (FAQs)

Q1. What is function overriding in C++?

Function overriding is when a derived class provides its own implementation of an inherited virtual function from a base class.

Q2. Is the virtual keyword necessary for function overriding?

The virtual keyword is necessary in the base class for runtime polymorphic overriding. A derived function with the same name and signature can exist without it, but calls through a base pointer or reference will not use virtual dispatch.

Q3. What is the use of the override keyword?

The override keyword tells the compiler that a derived function is intended to override a virtual base-class function. It helps detect signature mistakes.

Q4. Can we override a non-virtual function in C++?

A derived class can define a function with the same name and signature as a non-virtual base function, but this is function hiding, not runtime overriding.

Q5. What is runtime polymorphism?

Runtime polymorphism allows C++ to select the appropriate overridden virtual function based on the actual object type at runtime.

Q6. Can constructors be overridden?

No. Constructors are not inherited as virtual functions and cannot be overridden. Destructors, however, can be virtual.

Q7. Can a virtual function be private?

Yes. A private virtual function can be overridden in a derived class. Access control affects whether a function can be called directly, not whether it can be overridden.

Q8. What is the difference between virtual and override?

virtual declares a base-class function as virtual. override marks a derived-class function as an intended override and makes the compiler check it.

Q9. Can we prevent function overriding?

Yes. Use the final keyword on a virtual function to prevent derived classes from overriding it.

Q10. Does function overriding support multiple inheritance?

Yes. C++ supports virtual function overriding with multiple inheritance, although ambiguous or repeated base-class structures require careful design.

Runnable Code Example: Function Overriding in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <memory>

class GraphicShape {
public:
    virtual ~GraphicShape() = default; // Virtual destructor mandatory for polymorphism

    virtual void draw() const {
        std::cout << "Drawing generic shape
";
    }
};

class Circle : public GraphicShape {
public:
    void draw() const override { // C++11 override specifier
        std::cout << "Drawing precise vector Circle (radius: 10)
";
    }
};

int main() {
    std::unique_ptr<GraphicShape> shape = std::make_unique<Circle>();
    shape->draw(); // Dynamically dispatches to Circle::draw()
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: Why is a virtual destructor mandatory when using function overriding?

A: If a base class pointer pointing to a derived object is deleted without a virtual destructor, only the base class destructor runs, causing resource leaks (slicing / leaked derived class members).
Q

Q: What is the difference between function overloading and function overriding?

A: Overloading occurs in the same scope with different parameters resolved at compile-time. Overriding occurs across inherited classes with identical signatures resolved dynamically at runtime via vtables.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

1. Always mark overridden member functions with the override specifier.
2. Always declare a virtual ~Base() = default; destructor in polymorphic base classes.
3. Use final when a class or virtual function is not intended to be further extended.