Understanding Function Overriding in C++

Understanding Function Overriding in C++

4 mins read4.6K Views Comment
Updated on Sep 27, 2023 11:51 IST

A derived class inherits the features of base class. If the same function is defined in both base and derived class and we call the function through the object of derived class. In such a case, the function of derived class is executed. This is known as function overriding in C++.

2022_08_Untitled-design-12.jpg

We have already discussed C++ inheritance in the previous tutorial. Inheritance is an OOP concept that allows us to create subclasses that derive their attributes from existing classes known as the base classes. 

Now, when the derived class defines a function already defined in the base class, it is called function overriding in C++. Function overriding allows us to perform the specific implementation of a function already used in the base class. Let’s understand how function overriding occurs in C++.

Table of Content

What is Function Overriding in C++?

When a function defined in an object-oriented programming language has the same name and the same function signature (arguments and their data types) in both – the base class and the derived class(es) with a different function definition, this is known as Function Overriding. Through function overriding, you can redefine a function of the base class inside the derived class, and thus override the base class function. 

In C++, function overriding represents the run-time polymorphism or late binding as overriding occurs during run time.

C++ Syntax to implement function overriding:

//Function Overriding in C++

public class baseClass {
  access_modifier:
    //overridden function
    return_type functionName(){ }
  };
}

public class derivedClass : public baseClass {
  access_modifier:
    //overriding function
      return_type functionName(){ }
  };
}

To achieve function overriding, the function name, its arguments, and the return type should all be the same.

Learn more about OOPs in C++

How Does Function Overriding in C++ Work?

Let’s look at the following C++ example:

 
//C++ program to demonstrate function overriding
#include <iostream>
using namespace std;
class baseClass {
public:
void print() {
cout << "The Base Function." << endl;
}
};
class derivedClass : public baseClass {
public:
void print() {
cout << "The Derived Function." << endl;
}
};
//Driver code
int main() {
derivedClass d1;
d1.print();
return 0;
}
Copy code

Output:

The Derived Function

In the above-given example, we defined print() function in both – the base class as well as the derived class. Now, when we access the print() function using the derived class object d1, the derived class print() function overrides the base class print() function.

All About Switch Statement in C++
All About Switch Statement in C++
A switch statement in C++ allows variables to be tested for equality against set of values called case. It evaluates a given expression and executes statements associated with it based...read more
All About C++ Friend Function
All About C++ Friend Function
Friend function in C++ is a non-member function of a class. It is defined outside the class scope but it does have the access to a private data member of...read more
Understanding Break Statement in C++
Understanding Break Statement in C++
The break statement is loop control statement that is used for the terminating the loop. These are used in the situations where we do not have the idea about actual...read more

Accessing Overridden Function in C++

You can access the overridden function of the base class in the following ways:

Example 1: Use the scope resolution operator

 
#include <iostream>
using namespace std;
class baseClass {
public:
void print() {
cout << "The Base Function." << endl;
}
};
class derivedClass : public baseClass {
public:
void print() {
cout << "The Derived Function." << endl;
}
};
// Driver code
int main() {
derivedClass d1, d2;
d1.print();
// Using the scope resolution operator ::
d2.baseClass::print();
return 0;
}
Copy code

Output 1:

The Derived Function. 
The Base Function.

Explore free C++ courses

Example 2: Using the Derived Class

 
#include <iostream>
using namespace std;
class baseClass {
public:
void print() {
cout << "The Base Function." << endl;
}
};
class derivedClass : public baseClass {
public:
void print() {
cout << "The Derived Function." << endl;
// Call overridden function
baseClass::print();
}
};
// Driver code
int main() {
derivedClass d1;
d1.print();
return 0;
}
Copy code

Output 2:

The Derived Function. 
The Base Function.

In the above example, we have used baseClass::print(); to call the overridden function inside the derived class itself.

Example 3: Use the Pointer

You can use the base class pointer to point to an object of the derived class, then call the overridden function from that pointer

 
#include <iostream>
using namespace std;
class baseClass {
public:
void print() {
cout << "The Base Function." << endl;
}
};
class derivedClass : public baseClass {
public:
void print() {
cout << "The Derived Function." << endl;
}
};
// Driver code
int main() {
derivedClass d1;
// Base Pointer points to d1
baseClass *ptr = &d1;
// Call function of Base class using ptr
ptr->print();
return 0;
}
Copy code

Output:

The Base Function.

In the above example, 

  • We have created a pointer of baseClass type ptr, which points to the derived class object d1
  • Then, we call the print() function using ptr which calls the overridden print() function from the base class. 

Function Overriding vs. Function Overloading

Although both these concepts are an implementation of polymorphism, they do differ in many aspects such as their definition and usage.

Function overloading occurs whenever two or more functions have the same name and the same return type but different numbers or types of arguments.

The following comparison table summarizes the differences between function overriding and function overloading in C++:

Function Overloading Function Overriding
Can occur with or without inheritance. Can only occur during the presence of inheritance.
Overloaded functions must have different function signatures (number or data types of arguments should be different). Overridden functions must have the same function signatures (number or data types of arguments should be identical).
Function Overloading allows the same function to exhibit different behavior depending on the arguments passed to the functions. Function Overriding occurs when the derived class and the base class functions are expected to perform differently.
Illustrates compile-time polymorphism. Illustrates run-time polymorphism.
Overloading takes place within the same class. Overriding occurs in a base class and its derived class.
Class can have any number of the overloaded functions.   Can have only one overridden function per derived class.
No special keyword is used to overload a function. The virtual keyword in the base class, and the override keyword in the derived class can be used to override a function.
Execution of functions is faster in this case. The function execution is comparatively slower.

Endnotes

Hope this article was helpful for you to understand how function overriding occurs in C++. We learned the different ways to perform function overriding and differentiated between function overriding and function overloading. If you are keen on learning about C++ and solidify your basics, you can explore our articles on the C++ programming language.

______________________

Recently completed any professional course/certification from the market? Tell us what you liked or disliked in the course for more curated content.

Click here to submit its review with Shiksha Online

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio