Function overloading in C++ | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Monday, July 20, 2020

Function overloading in C++ | My CS Tutorial


Overloaded functions in C++


In C++ two different functions can have the same name if their parameter types or number are different. That means that you can give the same name to more than one function if they have either a different number of parameters or different types in their parameters.

Function overloading in c++ | My CS Tutorial


Function overloading is a feature and in c++ where two or more function can have the same name but different parameter.
Function overloading is a compile time polymorphism.


Rules of overloading:-


sum(int num1,int num2)
sum(int num1,int num2,num 3)
sum(int num1,double num2)

The parameters should qualify any one or more of the following conditions,they should have different type,number or sequence of parameters.

Example:-


sum(int num1,int num2)
sum(double num1,double num2)

these two have different number of parameters:-

sum(int num1,int num2)
sum(int num1,int num2,num 3)

these two have different sequence of parameters:-

sum(int num1,double num2)
sum(double num1,int num2)

all of the above three cases are valid case of overloading.

Example:-


#include<iostream.h>
using namespace std;
class addition
{
public:
int sum(int num1,int num2)
{
return num1 + num2;
}
int sum(int num1,int num2,int num3)
{
return num1 + num2 + num3;
}
};
int main()
{
addition obj;
cout<<obj.sum(20,15)<<endl;
cout<<obj.sum(81,100,20)<<endl;
return 0;
}

output:-

35
101



_______________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment