2. Function Overloading
• C++ enables several functions of the
same name to be defined, as long as
these functions have different sets of
parameters (at least as far as their
types are concerned). This capability
is called function overloading.
3. Example To Use The concept of function
overloading
# include<iostream.h>
# include<conio.h>
void area (float r);
void area (float l, float b);
void main()
{
float r1, l1, b1;
clrscr();
cout<<“ Enter value of r1: ”<<endl;
cin>>r1;
cout<<“ Enter value of l1: ”<<endl;
cin>>l1;
cout<<“ Enter value of b1: ”<<endl;
cin>>b1;
cout<<“ Area of the circle is “<<endl;
area (r1);
cout<<“ Area of the rectangle is “<<endl;
area (l1, b1);
}
void area (float r)
{
float a = 3.14*r*r;
cout<<“ Area = “<<a<<endl;
}
void area ()
{
float a1 = l*b;
4. Output
Enter value of r1:
2
Enter value of l1:
4
Enter value of b1:
6
Area of the circle is
Area = 12.56
Area of the rectangle is
Area = 24
5. Inline Functions in C++
• Whenever we call a function, control jump to
function and come back to caller program when
execution of function is completed. This process
takes a lot of time. C++ provides a solution of this
problem, that is inline function. With inline function,
the compiler replaces the function call statement
with the function code itself (process called
expansion) and then compiles the entire code.
Thus, with inline functions, the compiler does not
have to jump to another location to execute the
function, and then jump back as the code of the
called function is already available to the calling
program.
6. Example to demonstrate inline functions
#include<iostream.h>
#include<conio.h>
inline float mul (float x, float y)
{
return (x*y);
}
inline double div (double p, double q)
{
return (p/q);
}
int main()
{
float a = 12.345;
float b = 9.82;
clrscr();
cout<<mul (a,b)<<endl;
cout<<div (a,b)<<endl;
return 0;
}