Function Overriding in C++

Last Updated : 9 May 2026

Function overriding that allows a derived class to redefine a function of its base class. It is mainly used to achieve runtime polymorphism in C++ programs.

In this chapter, you will learn about function overriding in C++, its syntax, rules, working process, and examples using inheritance and virtual functions.

What is Function Overriding in C++?

Function overriding in C++ is a feature of Object-Oriented Programming (OOP) in which a derived class provides its own implementation of a function that is already defined in the base class.

The overridden function in the derived class must have:

  • The same function name
  • The same parameters
  • The same return type

Function overriding is useful when we want to change or extend the behavior of a base class function in the derived class without modifying the original base class.

It is commonly used with inheritance and virtual functions to achieve runtime polymorphism. When a virtual function is called using a base class pointer or reference, the function of the actual object type is executed at runtime.

Syntax

The syntax of function overriding is as follows:

In this syntax,

  • The derived class inherits the base class.
  • The function name is the same in both classes.
  • The parameter list is the same in both classes.
  • The return type is also the same.
  • The virtual keyword enables runtime polymorphism.
  • The override keyword indicates that the function overrides a base class function.

Example of Function Overriding

In the following example, the eat() function of the Animal class is overridden in the Dog class.

Output:

Eating bread...

Explanation:

In this example, the Dog class overrides the eat() function of the Animal class. When the eat() function is called using the Dog object, the derived class version of the function is executed.

Rules for Function Overriding

The following rules should be remembered while using function overriding:

  • The function name in the derived class must be the same as in the base class.
  • The parameter list must be the same in both classes.
  • The return type should also be the same.
  • The derived class must inherit from the base class.
  • The base class function should be declared as virtual for runtime polymorphism.
  • Access specifiers can be changed, but it is generally not recommended.

How Function Overriding Works in C++?

Function overriding works with inheritance and virtual functions. When a function in the base class is declared as virtual, the compiler enables dynamic binding.

If a derived class overrides the virtual function and the function is called using a base class pointer or reference, the function call is resolved at runtime according to the actual object type.

Internally, C++ uses a mechanism called a virtual table (vtable) to manage virtual functions and runtime polymorphism.

Function Overriding with Access Specifiers

Function overriding can also be used with access specifiers. Where, a derived class overrides a virtual function of the base class while maintaining proper access control using access specifiers such as public.

Example

In the following example, the Car class overrides the startEngine() function of the Vehicle class.

Output:

Starting a car with a push button...

Explanation:

In this example., we have taken a vehicle class (base class) that contains a startEngine() function, which shows "the starting a generic vehicle". After that, the car class (derived class) inherits from the vehicle and overrides the startEngine() function.

After that, we create a pointer of type Vehicle* and assign it to a car object. As the startEngine() function is virtual, the overridden function in the Car class is called dynamically at runtime instead of the base class function.

Accessing Overridden Function in C++

There are several methods to access an overridden function in the derived class, such as directly from the object and from the base class via pointers and references. Some of the methods are as follows:

1. Calling Overridden Function from Derived Class Directly

We can directly call the overridden function from the derived class by using the derived class object. It calls the function that was redefined in the derived class.

Example

Let us take an example to demonstrate the overridden function from derived class directly.

Output:

It is the derived class display function
It is the base class display function

Explanation:

In this example, the object.display() function calls the overridden function in the derived class, while the obj.callBaseFunction() function calls the base class's version of the display() function directly within the derived class.

2. Calling Overridden Function via Base Class Pointer or Reference

When we call a function using a base class pointer or reference, and the function is overridden in the derived class, the derived class version of the function is called in the program.

Example

The following example demonstrate the overridden function via base class pointer and reference.

Output:

Brand Name: Mahindra
Brand Name: Kia
Brand Name: Kia

Explanation:

In this example, the base class Car defines a virtual function as showBrand(). Mahindra and Kia brands override the function to provide brand-specific output. The use of base class pointer and reference allows us to call the overridden function at runtime based on the actual object, not the pointer and reference type.

3. Calling Overridden Function using Scope Resolution Operators

We can use the scope resolution operators (::) to access the overridden function in C++.

Example

The following example demonstrate the overridden function using scope resolution operators.

Output:

This is the Derived Function.
This is the Base Function.