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:
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:
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:
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.
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:
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.
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.
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:
Next TopicPassword Validation in C++