Write a C++ program for implementing the function overloading.

Function overloading in C++ You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload
function declarations that differ only by return type. C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.
An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation). When you call an overloaded function or operator, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.

Function overloading in C++
You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type.


Algorithm:
1. Define a class test.
2. Define the function sum with different arguments with different data types.
3. Define the object for the class test in main function.
4. Call the function using the argument type to perform different operations.
5. Print the output.


Program Code:
#include<iostream.h>
#include<conio.h>
class test
{
public:
int sum(int,int);
float sum(float,float);
double sum(double,double);
};
int test::sum(int a, int b)
{
return(a+b);
}
float test::sum(float a ,float b)
{
return(a+b);
}
double test::sum(double a, double b)
{
return(a+b);
}
void main()
{
test obj;
int choice,ans;
int a,b;
float x,y;
double m,n;
double result=0;
clrscr();
cout<<"\n\t\t main menu";
cout<<"\n\t1. Addition of two integer numbers";
cout<<"\n\t2. Addition of two float numbers";
cout<<"\n\t3. Addition of two double numbers"<<endl;
do
{
cout<<"\n enter your choice:";
cin>>choice;
switch(choice)
{
case 1: cout<<"\n enter 2 numbers";
cin>>a>>b;
result=obj.sum(a,b);
break;
case 2:
cout<<"\n enter 2 number";
cin>>x>>y;
result=obj.sum(x,y);
break;
case 3:
cout<<"\n enter 2 number";
cin>>m>>n;
result=obj.sum(m,n);
break;
default:
cout<<"wrong choice";
break;
}
cout<<"\n\n result"<<result<<endl;
cout<<"\ndo you want to continue:";
ans=getch();
}
while(ans=='y'||ans=='Y');
getch();
}


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: