C++Tutorials

function overloading and function overriding in c++

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

BasisFUNCTION OVERLOADINGFUNCTION OVERRIDING
DefinitionIt allows us to declare two or more function having same name with different number of parameters or different datatypes of argumentIt allows us to declare a function in parent class and child class with same name and signature
Inheritance ConceptIt can occur without inheritance conceptOverriding can only be done when one class is inherited by other class
SignatureThe overloaded functions must have different signature, for example, either the number of parameters or sort of parameters should be differentFunction signatures must have same signature
Polymorphism TypeIt is an example of compile-time PolymorphismIt is an example of run-time Polymorphism
ScopeThe overloaded functions are always in the same scopeFunctions are always in a different scope
PurposeOverloading is utilized to have the same names of various functions which act distinctively relying on parameters handed over to themOverriding is required when a determined class function needs to perform some additional or unexpected job in comparison to the base class function
AccomplishOverloading is accomplished at compile timeOverriding is accomplished at runtime
ConstructorA constructor can be overloadedA constructor cannot be overridden
UsageA function can be overloaded any number of timesA function can be overridden only single time in the respective inheriting class

Advantages of function overloading in c++

  1. The function can perform different operations and hence eliminates the use of different function names for the same kind of operations.
  2. The program becomes easy to understand.
  3. Easy maintainability of the code.
  4. Function overloading brings flexibility in C++ programs.

Advantages of function overriding in c++

  1. The child class having same function of parent class,can even have its independent implementation of that function.
  2. Helps in saving the memory space.
  3. Maintain consistency, and readability of our code.
  4. Helps in making code re-usability easy.
  5. 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.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button