Lab 09 Functions Overloading and Inline Functions

advertisement
ISE-103 Fundamentals of Computer Programming
Function Overloading and Inline Functions
Program # 01:
#include<iostream.h>
#include<conio.h>
void main (void)
{
int num1, num2, num3;
float fNum1, fNum2, fNum3;
int option;
//Function Declaration
int Sum(int, int);
int Sum(int, int, int);
float Sum(float, float);
float Sum(float, float, float);
float Sum(int, int, float);
clrscr();
cout<<"Press 1 to add two integers"<<endl;
cout<<"Press 2 to add three integers"<<endl;
cout<<"Press 3 to add two decimal numbers"<<endl;
cout<<"Press 4 to add three decimal numbers"<<endl;
cout<<"Press 5 to add two integers and one decimal number"<<endl;
cin>>option;
switch(option)
{
case 1:
{
cout<<"Enter 1st Integer =";
cin>>num1;
cout<<"Enter 2nd Integer =";
cin>>num2;
cout<<"Sum of two integers = "<<Sum(num1, num2);
break;
}
case 2:
{
cout<<"Enter 1st Integer =";
cin>>num1;
cout<<"Enter 2nd Integer =";
cin>>num2;
cout<<"Enter 3rd Integer =";
cin>>num3;
cout<<"Sum of three integers = "<<Sum(num1, num2, num3);
break;
}
case 3:
{
cout<<"Enter 1st Float Number =";
cin>>fNum1;
cout<<"Enter 2nd Float Number =";
cin>>fNum2;
cout<<"Sum of two integers = "<<Sum(fNum1, fNum2);
break;
}
case 4:
{
cout<<"Enter 1st Float Number =";
cin>>fNum1;
cout<<"Enter 2nd Float Number =";
cin>>fNum2;
cout<<"Enter 3rd Float Number =";
cin>>fNum3;
cout<<"Sum of two integers = "<<Sum(fNum1, fNum2, fNum3);
break;
}
case 5:
{
cout<<"Enter 1st Integer =";
cin>>num1;
cout<<"Enter 2nd Integer =";
cin>>num2;
cout<<"Enter Float Number =";
cin>>fNum1;
cout<<"Sum of two integers = "<<Sum(num1, num2, fNum1);
break;
}
default:
{
cout<<"Invalid Option Selected";
break;
}
}
getch();
}
//Defination of the Function
int Sum(int x, int y)
{
return x + y;
}
int Sum(int x, int y, int z)
{
int sum = x + y + z;
return sum;
}
float Sum(float x, float y)
{
return x + y;
}
float Sum(float x, float y, float z)
{
return x + y + z;
}
float Sum(int x, int y, float z)
{
float sum = x + y + z;
return sum;
}
//three errors
lecture…
must
be
checked
before
Program # 02:
Write a program to find greatest number among the three numbers using
user-defined functions, use function overloading to handle integer and
float type.
Program # 03:
Write a program that ask user to input a number and calculate its
factorial using inline function.
Program # 04:
Function Template:
Syntex:
Template <class T>
T Function_Name (Arguments of T seperated by coma)
{
Statement;
Statement;
}
Example:
#include<iostream.h>
#include<conio.h>
template <class T>
T sum (T a, T b, T c)
{
return a + b + c;
}
void main (void)
{
clrscr();
cout<<"Sum of three integer values ="<<sum(85,25,54)<<endl;
cout<<"Sum of three float values ="<<sum(54.2, 54.3, 2.01)<<endl;
getch();
}
Download