Overriding Member Function in C++

Last Updated : 6 May 2026

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.

What is Overriding Member Function in C++?

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.

How to Override a Member Function in C++?

To Override a Member Function in C++, we need to follow the steps given below:

Step 1: Define a Base Class with a Virtual Function

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:

Step 2: Define a Derived Class that Overrides the Virtual Function

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:

Step 3: Create Objects of the Derived Class and Call the Virtual Function

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.

Example

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".

Rules for Overriding Member Functions

  • The Overridden Function in the derived class must have the same name and arguments as the base class function.
  • The Overridden Function must have the same return type as the base class function or a covariant return type.
  • The access level of the Overridden Function in the derived class cannot be more restrictive than the access level of the base class function.
  • The Virtual keyword must be used in the base class function declaration.
  • The function in the base class must be declared with the same access level or higher than the derived class.