function overloading and function overriding provides a way to achieve polymorphism concept.
if you want to more about polymorphism click here. But before moving forward if you don’t know what is function then click this link and read what is a function?
The main difference between function overloading and function overriding is that function overloading enables us to define multiple functions with a similar name within the same class, whereas function overriding allows us to redefine a method having the same name and signature in inheriting class.
Function overloading vs function overriding
Basis | FUNCTION OVERLOADING | FUNCTION OVERRIDING |
---|---|---|
Definition | It allows us to declare two or more function having same name with different number of parameters or different datatypes of argument | It allows us to declare a function in parent class and child class with same name and signature |
Inheritance Concept | It can occur without inheritance concept | Overriding can only be done when one class is inherited by other class |
Signature | The overloaded functions must have different signature, for example, either the number of parameters or sort of parameters should be different | Function signatures must have same signature |
Polymorphism Type | It is an example of compile-time Polymorphism | It is an example of run-time Polymorphism |
Scope | The overloaded functions are always in the same scope | Functions are always in a different scope |
Purpose | Overloading is utilized to have the same names of various functions which act distinctively relying on parameters handed over to them | Overriding is required when a determined class function needs to perform some additional or unexpected job in comparison to the base class function |
Accomplish | Overloading is accomplished at compile time | Overriding is accomplished at runtime |
Constructor | A constructor can be overloaded | A constructor cannot be overridden |
Usage | A function can be overloaded any number of times | A function can be overridden only single time in the respective inheriting class |
Advantages of function overloading in c++
- The function can perform different operations and hence eliminates the use of different function names for the same kind of operations.
- The program becomes easy to understand.
- Easy maintainability of the code.
- Function overloading brings flexibility in C++ programs.
Advantages of function overriding in c++
- The child class having same function of parent class,can even have its independent implementation of that function.
- Helps in saving the memory space.
- Maintain consistency, and readability of our code.
- Helps in making code re-usability easy.
- The child class can access function of parent class as well.
Example of function Overloading in c++
#include <iostream> using namespace std; class shape{ public: void area(int side) { cout<<"Area of square :"<< side*side; } void area(int len, int width) { cout<<"Area of Rectangle :"<< len * width<<"\n"; } }; int main() { shape obj; cout<<"Example of function overloading \n"; obj.area(10); cout<<"\n"; obj.area(5,10); return 0; }
Here in the above example, area function was overloaded, one for calculating square’s area and the second one for calculating the rectangle’s area.
Example of function Overriding in c++
#include <iostream> using namespace std; class shape{ public: void display(){ cout<<" I am base class"; } }; class override_me: public shape { public: void display(){ cout<<"I am overriding display function of parent class \n"; } }; int main() { override_me obj; cout<<"Example of function overriding \n"; obj.display(); return 0; }
Here, we overrode the display method, enabling us to define or redefine the body of that function in the child class.