0% found this document useful (0 votes)
5 views

Function Overloading

Uploaded by

sarurasal171
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Function Overloading

Uploaded by

sarurasal171
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Function Overloading

Definition: Function overloading means two or more function have same name but
differ in the number of arguments or data types of arguments ie function name is
overloaded.
Function overloading is also called as Compile time polymorphism.

Ex.
int area(int r);
int area(int b, int h);
void cal(float a, float b);
void cal(float a, float b, float c);

//Write a program to find area of circle and triangle using function overloading.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class funoverload
{
private: int r,b,h;
float carea, tarea;
public: void area(int r);
void area(int b, int h);
};
void funoverload :: area(int r)
{
carea = 3.142*r*r;
cout<<"The area of a circle is = "<<carea<<endl;
}
void funoverload :: area(int b, int h)
{
tarea = 0.5*b*h;
cout<<"The triangle area is ="<<tarea;
}
void main()
{
funoverload p;
clrscr();
p.area(5);
p.area(4,7);
getch();
}
//Write a program to perform addition using function overloading.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class arith
{
private: int a,b,isum;
float fsum;
public: void add(int a, int b);
void add(float a, float b, float c);
};
void arith :: add(int a, int b)
{
isum = a + b;
cout<<"The addition of two numbers="<<isum<<endl;
}
void arith :: add(float a, float b,float c)
{
fsum = a + b + c;
cout<<"The addition of three numbers ="<<fsum;
}
void main()
{
arith h;
clrscr();
h.add(5,8);
h.add(2.5, 7.8,4.5);
getch();
}

Advantages of function overloading


1. It is easier to understand
2. Code maintenance is easy
3. The code is executed faster
4. Provides easier interface between programs and real world object
5. It is easy to understand flow of program information.

Restrictions on function overloading


1. Overloaded functions must have different argument list.
2. If typedef is used for naming functions, then the function is not consider
as different type.

Inline Function
The inline function is a short function where compiler replaces a function call
with the body of the function.
➢ The keyword inline is used to define inline function
➢ The compiler replaces the function call statement with the function code itself and
then complies the entire code
➢ It should be defined before all functions that call it
➢ They run little faster than normal function.

//WAP TO FIND SQUARE OF A NUMBER USING INLINE FUNCTION.


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
inline int square(int a)
{
return(a*a);
}
void main()
{
int x;
clrscr();
x = square(5);
cout<<"Square of 5 is = "<<x<<endl;
getch();
}
Output:
Square of 5 is = 25

// JP -WAP to find the cube of a number using inline function.


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
inline int cube(int a)
{
return(a*a*a);
}
void main()
{
int x , y;
clrscr();
cout<<"enter the number : ";
cin>>x;
y = cube(x);
cout<<"cube of "<<x<<"="<<y<<endl;
getch();
}
Output:
enter the number : 4
cube of 4 = 64

//Program to calculate factorial of a given number using inline function.


#include<iostream.h>
#include<conio.h>
inline int fact(int n)
{
if(n<1)
return(1);
else
return(n*fact(n-1));
}
void main()
{
int n;
clrscr();
cout<<"enter the value of n =";
cin>>n;
cout<<"The factorial of a given number="<<fact(n);
getch();
}
Output:
enter the value of n = 5
The factorial of a given number = 120

Advantages of Inline Function


1. These are compact function call
2. The size of the object code is reduced
3. Speed of execution of program increases
4. Very efficient code can be generated
5. The readability of the program increases.
Disadvantages
Inline function is substituted in place of a function call, therefore the size of the
executable file increases and more memory is needed

Inline function may not work sometime


1. The inline function definition is too long or too complicated.
2. The inline function is recursive.
3. The inline function has looping constructs.
4. The inline function has a switch or goto statement.

Friend Function
A friend function is a non-member function that is a friend of a class.
OR
It is a function which has authorization to access the private and protected members
of a class though it is not a member of the class.
➢ A friend function is declared within a class
➢ It is declared with prefix friend
➢ It can access public data members
Syntax:
class class-name
{
private:
public: friend return-type-specifier function-name(arguments);
}
❖ While defining a friend function outside the class it does not required either
keyword friend or scope resolution operator (:: )
❖ It has full access right to private and protected members of the class
❖ Unlike member function, it cannot access the member name directly and has to
use an object name and dot membership operator with each member name(
data member)
Ex. S.a
where
s→ object name of a class
a→ private data member
❖ IMP
Usually, friend function has the object as arguments
Ex.
add(p)→Inside main() declaration
add→ function-name
p→ Object of a class inside main() function

//WAP to add two numbers using friend function.


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class stud
{
private: int a,b,sum;
public: void getdata();
friend void add(stud s);
};
void stud :: getdata()
{
cout<<"enter the value of a and b =";
cin>>a>>b;
}
void add(stud s)
{
s.sum = s. a + s.b;
cout<<"The sum of two numbers = "<<s.sum<<endl;
}
void main()
{
stud p;
clrscr();
p.getdata();
add(p);
getch();
}
Output:
enter the value of a and b = 5 7
The sum of two numbers = 12

You might also like