FREE E LEARNING PLATFORM
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Difference between Function Overloading and Function overriding in C++


Function overloading and Function overriding both are examples of polymorphism but they are completely different. Before we discuss the difference between them, lets discuss a little bit about them first.

Function Overloading

Function overloading is a feature that allows us to have same function more than once in a program. Overloaded functions have same name but their signature must be different.

Example

Here we have the same function sum declared four times with different signatures. Based on the parameters we pass, while calling function sum, decides which method is to be called. This happens during compilation, which is why it is also known as compile time polymorphism. If you are wondering why I have suffixed each floating point value with "f" letter in the example below, during function call then refer this: function overloading float issue.

#include <iostream>  
using namespace std;  
// overloaded functions  
float sum(int, int);  
float sum(float, float);  
float sum(int, float);  
float sum(float, int);  
int main(){     
//This will call the second function     
cout<<sum(15.7f, 12.7f)<<endl;       
//This will call the first function     
cout<<sum(200, 100)<<endl;       
//This will call the third function     
cout<<sum(100, 20.7f)<<endl;       
//This will call the fourth function     
cout<<sum(90.8f, 30)<<endl;       
return 0;  }  
float sum(int a, int b){     
return a+b;  }  
float sum(float a, float b){     
return a+b;  }  
float sum(int a, float b){     
return a+b;  }  
float sum(float a, int b){     
return a+b;  }

Output:

28.4
300
120.7
120.8

Function Overriding

Function overriding is a feature of OOPs Programming that allows us to override a function of parent class in child class.

Example:

#include<iostream>  
using namespace std;  
//Parent class or super class or base class  
class A{  
public:     
void disp() {         
cout<<"Parent Class disp() function"<<endl;     
}     
void xyz() {           
cout<<"xyz() function of parent class";     
}  };  
//child class or sub class or derived class  
class B : public A{  public:      
/* Overriding disp() function of parent class       
* and giving a different definition to it.       */      
void disp() {           
cout<<"Child class disp() function"<<endl;      
}  };  
int main(){      
//Creating object of child class B      
B obj;      
obj.disp();      
/* If you want to call the overridden function       
* (the same function which is present in parent class)       
* from the child class then assign the reference of       
* parent class to the child class object.       */       
A obj2 = B();      
obj2.disp();  
}

Output

Child class disp() function
Parent Class disp() function

Difference between function overloading and function overriding

Now that we understand what is function overloading and overriding in C++ programming, lets see the difference between them:

1) Function Overloading happens in the same class when we declare same functions with different arguments in the same class. Function Overriding is happens in the child class when child class overrides parent class function.

2) In function overloading function signature should be different for all the overloaded functions. In function overriding the signature of both the functions (overriding function and overridden function) should be same.

3) Overloading happens at the compile time thats why it is also known as compile time polymorphism while overriding happens at run time which is why it is known as run time polymorphism.

4) In function overloading we can have any number of overloaded functions. In function overriding we can have only one overriding function in the child class.

noidatut course







Leave Comment