Overriding member functions is an important concept in C++ inheritance and runtime polymorphism. It allows a derived class to provide a new implementation of a function inherited from the base class.
In this chapter, you will learn about overriding member functions in C++, their working, importance, and how derived classes redefine base class functions with suitable examples.
In Object-Oriented Programming, inheritance is one of the most powerful concepts. It enables a class to inherit characteristics and behaviors from another class. Overriding is a technique used in C++ programming to modify the behavior of an inherited member function in a derived class.
When a derived class inherits a member function from its base class, it can redefine the behavior of that function in the derived class. This process of redefining a base class member function in a derived class is called overriding, and the redefined function is referred to as an overridden member function.
In other words, when a derived class defines a member function with the same name and signature as a member function in its base class, the derived class function overrides the base class function.
To Override a Member Function in C++, we need to follow the steps given below:
A Virtual Function is a type of member function declared with the keyword "Virtual" in the base class. This indicates that the function can be Overridden in the derived class. The syntax for declaring a Virtual Function is as follows:
C++ code:
To Override a Virtual Function, we need to define a derived class that inherits from the base class and provides a new implementation of the Virtual Function. The syntax for defining a derived class that overrides the Virtual Function is as follows:
C++ Code:
We can create objects of the derived class and call the Virtual Function using a Pointer to the base class. The syntax for creating objects of the derived class and calling the Virtual Function is as follows:
C++ Code:
When we call the Virtual Function using a Pointer to the base class, the implementation of the Virtual Function in the derived class will be called.
Let's look at an example that demonstrates the concept of Overriding Member Functions in C++. In the above example, we have a base class or parent class called, which has a virtual function. We also have two derived classes called Circle and Square, which override the draw() function to provide their own implementation.
Output:
Drawing a circle Drawing a square
Explanation:
In this example, we create objects of the Circle and Square classes and call the draw() function using a pointer to the base class Shape. Since the draw() function is Virtual, the implementation in the derived classes is called, and we get the output "Drawing a circle" and "Drawing a square".
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