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.
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:
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.
The syntax of function overriding is as follows:
In this syntax,
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.
The following rules should be remembered while using function overriding:
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 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.
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.
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:
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.
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.
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.
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.
We can use the scope resolution operators (::) to access the overridden function in C++.
The following example demonstrate the overridden function using scope resolution operators.
Output:
This is the Derived Function. This is the Base Function.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India