A) Write a program to illustrate function overloading. Write 2 overloading functions for adding two numbers.
B) Write a program illustrate function template for power of a number.
C) Write a program to illustrate function template for swapping of two numbers.
A) AIM:Write a program to illustrate function overloading. Write 2 overloading functions for adding two numbers.
SOURCE CODE:
#include<iostream>
using namespace std;
int add(int,int);
float add(float,float);
int main()
{
int i1,i2,i3;
float f1,f2,f3;
cout<<"Enter two integers:"<<endl;
cin>>i1>>i2;
cout<<"Enter two floating point values:"<<endl;
cin>>f1>>f2;
i3=add(i1,i2);
f3=add(f1,f2);
cout<<"Sum of two integers:"<<i3<<endl;
cout<<"Sum of two floating points:"<<f3<<endl;
}
int add(int a,int b)
{
return a+b;
}
float add(float a,float b)
{
return a+b;
}
OUTPUT:
Enter two integers:
9
6
Enter two floating point values:
2.6
8.5
Sum of two integers:15
Sum of two floating points:11.1
B) AIM:Write a program illustrate function template for power of a number.
SOURCE CODE:
#include<iostream>
using namespace std;
template<typename T>
T power(T a,T b)
{
T result=1;
while(b>0)
{
result=result*a;
b--;
}
return result;
}
int main()
{
int n1,n2;
cout<<"Enter two numbers:"<<endl;
cin>>n1>>n2;
cout<<n1<<" power "<< n2 <<" is "<<power(n1,n2)<<endl;
}
OUTPUT:
Enter two numbers:
3
5
3 power 5 is 243
C) AIM:Write a program to illustrate function template for swapping of two numbers.
SOURCE CODE:
#include<iostream>
using namespace std;
template<typename T>
T swap(T *a,T *b)
{
T temp;
temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int n1,n2;
cout<<"Enter two numbers:"<<endl;
cin>>n1>>n2;
cout<<"Before swapping n1="<<n1<<"\tn2="<<n2<<endl;
swap(&n1,&n2);
cout<<"After swapping n1="<<n1<<"\tn2="<<n2<<endl;
}
OUTPUT:
Enter two numbers:
9
6
Before swapping n1=9 n2=6
After swapping n1=6 n2=9
B) Write a program illustrate function template for power of a number.
C) Write a program to illustrate function template for swapping of two numbers.
A) AIM:Write a program to illustrate function overloading. Write 2 overloading functions for adding two numbers.
SOURCE CODE:
#include<iostream>
using namespace std;
int add(int,int);
float add(float,float);
int main()
{
int i1,i2,i3;
float f1,f2,f3;
cout<<"Enter two integers:"<<endl;
cin>>i1>>i2;
cout<<"Enter two floating point values:"<<endl;
cin>>f1>>f2;
i3=add(i1,i2);
f3=add(f1,f2);
cout<<"Sum of two integers:"<<i3<<endl;
cout<<"Sum of two floating points:"<<f3<<endl;
}
int add(int a,int b)
{
return a+b;
}
float add(float a,float b)
{
return a+b;
}
OUTPUT:
Enter two integers:
9
6
Enter two floating point values:
2.6
8.5
Sum of two integers:15
Sum of two floating points:11.1
B) AIM:Write a program illustrate function template for power of a number.
SOURCE CODE:
#include<iostream>
using namespace std;
template<typename T>
T power(T a,T b)
{
T result=1;
while(b>0)
{
result=result*a;
b--;
}
return result;
}
int main()
{
int n1,n2;
cout<<"Enter two numbers:"<<endl;
cin>>n1>>n2;
cout<<n1<<" power "<< n2 <<" is "<<power(n1,n2)<<endl;
}
OUTPUT:
Enter two numbers:
3
5
3 power 5 is 243
C) AIM:Write a program to illustrate function template for swapping of two numbers.
SOURCE CODE:
#include<iostream>
using namespace std;
template<typename T>
T swap(T *a,T *b)
{
T temp;
temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int n1,n2;
cout<<"Enter two numbers:"<<endl;
cin>>n1>>n2;
cout<<"Before swapping n1="<<n1<<"\tn2="<<n2<<endl;
swap(&n1,&n2);
cout<<"After swapping n1="<<n1<<"\tn2="<<n2<<endl;
}
OUTPUT:
Enter two numbers:
9
6
Before swapping n1=9 n2=6
After swapping n1=6 n2=9