Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Welcome Ninjas! C++ is a powerful object-oriented programming language. It provides developers with features to create efficient and effective code. Have you heard about the concept of function overriding? Well, it allows a subclass to provide its own implementation of a method that is defined in superclass. In C++ function overriding is achieved through inheritance. It also allows for the reuse and extension of code functionality.
We will explore the concept of C++ function overriding in depth in the article. Let us first start with OOP paradigm before starting the function overriding. Further, we will look at how function overriding is important and look at the syntax and common examples. So, let us get started with our topic.
What is Function Overriding in C++?
In function overriding object-oriented concepts, using inheritance child class can inherit features from the parent class. Now, if the same function is defined in both the child and parent class and the user calls this function using the child object the function of the child class is executed. The method of the parent class is overridden by this child class.
Dynamic polymorphism is another term for run-time polymorphism. Function overriding is an example of runtime polymorphism. The child class object overrides the inherited implementation of the parent class and executes on its own. This call to the function of which method will be called is decided at runtime, so it is known as runtime polymorphism.
Syntax
Now quickly look at the syntax of C++ function overriding:
#include<bits/stdc++.h>
using namespace std;
class Base_class{
public:
fun_def() {
// Function definition in parent class
}
};
class Derived_class: public Base_class {
public:
func_def() {
// Function definition derived in child class
}
};
Explanation
In the above syntax, the Base_class is the parent class. The fun_def is the function defined inside the class. The Derived_class is the child class that inherits the parent class fun_def function.
Real-Life Example of Function Overriding
Function overriding in object-oriented programming allows a subclass to provide a specific implementation of a method that is already defined in its superclass. A real-life example can be seen with a Vehicle class and its subclasses, such as Car and Truck.
For instance, both cars and trucks can have a startEngine() method, but each vehicle might start its engine in a different way. In this case, the startEngine() method is overridden in both the Car and Truck classes to provide specific implementations.
C++
C++
class Vehicle { public void startEngine() { System.out.println("Vehicle engine starts."); } }
class Car extends Vehicle { @Override public void startEngine() { System.out.println("Car engine starts with a key."); } }
class Truck extends Vehicle { @Override public void startEngine() { System.out.println("Truck engine starts with a button."); } }
public class Main { public static void main(String[] args) { Vehicle v1 = new Car(); Vehicle v2 = new Truck();
v1.startEngine(); v2.startEngine(); } }
You can also try this code with Online C++ Compiler
Car engine starts with a key.
Truck engine starts with a button.
This example demonstrates how function overriding allows specific behavior for different objects, even though they share the same method name.
Types of Function Overriding in C++
In C++, function overriding can be categorized into two types:
1. Virtual Function Overriding
This type occurs when a function in a base class is declared as virtual, and a derived class provides its own implementation of that function. The function is resolved at runtime using dynamic polymorphism. Example:
2. Pure Virtual Function Overriding
A pure virtual function is a function that has no implementation in the base class and must be implemented in any derived class. It is declared by assigning = 0 to the function in the base class. This type enforces that derived classes must provide a specific implementation. Example:
In both cases, function overriding enables polymorphism, allowing a function to behave differently depending on the object type.
Object Oriented Programming Paradigm
Object Oriented programming, commonly known as OOP paradigm, is a programming paradigm. Programming paradigm means the style of programming. Here, we’ll use the object-oriented concept of real life, which helps us to write codes in specific orders.
OOPs is an imperative programming paradigm. In this, the user provides step-by-step instructions and stores the result at the end. The order of these steps is important in OOPs as they may execute differently based on variable values. It is because steps will execute differently depending on the values of variables when they are executed.
We have four fundamental pillars of OOPs, they are:
Inheritance
Polymorphism
Data Abstraction
Encapsulation
From the reference of function overriding, it is also called run-time polymorphism. Before diving into what function overriding is, let’s have a quick look about polymorphism.
Polymorphism
Polymorphism is one of the pillars of the object-oriented programming. The word polymorphism comes from two Greek words poly means many, and morphism means forms. The term polymorphism means that the same function with the same name can be used in different forms. Polymorphism inOOPS makes our code more flexible and reusable.
The parent class passes on all its methods to the child class in polymorphism. Polymorphism can be seen in real-life examples. A boy who can be a student, son, or friend. Polymorphism are of two types Compile-Time polymorphism and Run-Time Polymorphism. Function overriding is also called run-time polymorphism.
Example of Function Overriding
Now, let’s look at some common examples of C++ function overriding:
In the above example, the show() function inside the parent class is being overridden by the child class. When the show function is called, the content of the derived class is displayed. It is due to the use of a scope resolution operator. This is called C++ function overriding because the child class overrides the parent class. The output will be Coding Ninjas Derived Class from the child class.
Advantages of Function Overriding
Following are some advantages of function overriding:
As we had discussed, function overriding helps us use polymorphism. We can use a function to behave in a different manner depending on the context.
Function overriding can also help us in reusing code. We can simply customize it to meet the need for the subclass.
With the help of function overriding, customization is also possible. Customization means changing the behaviour of the method. Thus, it meets the requirements of its superclass.
Function overriding can also help us extend a superclass's functionality. We can modify the behaviour and extend its functions accordingly.
Disadvantages of Function Overriding
Following are some disadvantages of function overriding:
Function overriding can sometimes be confusing for developers. It is because of the class hierarchy.
In function overriding, if a class is customized and not compatible with another superclass, it will result in an error.
The function overriding can also impact performance in a negative way. If the overridden method is called again and again, it will impact performance.
Because of function overriding, your code has a possibility of becoming fragile. It is due to a change of implementation that will make the code fragile.
Limitations of Runtime Function Overriding
While runtime function overriding provides flexibility and polymorphism in object-oriented programming, it has several limitations:
Cannot Override Static Methods: Static methods are resolved at compile time, so they cannot be overridden at runtime. They can only be hidden in a subclass.
Access Modifiers: The access level of the overridden method cannot be more restrictive than the method in the base class. For example, if the base class method is public, the overridden method cannot be private.
No Constructor Overriding: Constructors cannot be overridden in derived classes. They can only be inherited and specialized through constructor chaining.
Performance Overhead: Runtime polymorphism introduces a slight performance overhead due to dynamic method lookup and dispatch, especially in cases involving large numbers of object method calls.
Inheritance Dependency: Function overriding requires inheritance. Thus, it cannot be used for classes that do not follow an inheritance hierarchy, limiting its applicability.
Can Be Overridden Only in Derived Classes: Overriding can only occur in subclasses or derived classes. It is not possible to override a method in a class that does not inherit from the base class.
Frequently Asked Questions
What are the types of polymorphism?
Polymorphism are of two types Compile-Time polymorphism and Run-Time Polymorphism. Compile-Time polymorphism is achieved using operator overloading and function overloading. Run-Time polymorphism is achieved using function overriding.
What is the significance of inheritance in C++?
It helps us determine a class in another class, making application development and maintenance easier. It also allows for the reuse and extension of code functionality and a quick implementation time.
What are the local class and global class?
Global classes are defined inside the main function and not in any particular function, so all the functions in the code can access them. In comparison, local classes are declared inside a specific function and are local to that function itself.
Conclusion
This article briefly discussed the concept of C++ function overriding. We have discussed the polymorphism and then function overriding. Further, we looked at some common examples of C++ function overriding. You can check out our other blogs to enhance your knowledge: