Understanding Function Overriding in C++
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++.
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++.
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
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 codeint main() { derivedClass d1; d1.print(); return 0;}
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.
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 codeint main() { derivedClass d1, d2; d1.print();
// Using the scope resolution operator :: d2.baseClass::print();
return 0;}
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 codeint main() { derivedClass d1; d1.print(); return 0;}
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 codeint main() { derivedClass d1;
// Base Pointer points to d1 baseClass *ptr = &d1;
// Call function of Base class using ptr ptr->print();
return 0;}
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
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