Javatpoint Logo
Javatpoint Logo

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. In this article, we will see the concept of Overriding Member functions in C++ in detail.

What is Overriding Member Function?

In C++, 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's function will Override 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.

C++ Code:

Output:

Drawing a circle
Drawing a square

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

Advantages of Overriding Member Function:

Polymorphism:

One of the major advantages of overriding is that it enables Polymorphism. Polymorphism allows a derived class to have multiple behaviors depending on the context in which it is used.

Code Reusability:

Overriding allows the reuse of code from the base class while allowing the derived class to modify the behavior of the base class's member functions.

Modularity:

Overriding makes it easier to maintain code because changes made in the derived class do not affect the base class.

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.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA