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

Function Overloading

Function overloading allows multiple functions to have the same name but different parameters. It is a way to achieve polymorphism at compile time. Functions are differentiated by the number and type of parameters. The compiler determines which function to call based on the parameters passed. Inline functions replace function calls with the function body to improve performance by eliminating the overhead of calls. Friend functions can access private members of a class even though they are non-member functions. They have special access privileges not given to normal non-member functions.

Uploaded by

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

Function Overloading

Function overloading allows multiple functions to have the same name but different parameters. It is a way to achieve polymorphism at compile time. Functions are differentiated by the number and type of parameters. The compiler determines which function to call based on the parameters passed. Inline functions replace function calls with the function body to improve performance by eliminating the overhead of calls. Friend functions can access private members of a class even though they are non-member functions. They have special access privileges not given to normal non-member functions.

Uploaded by

Uday T k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

VVPUC, Tumkur

8. FUNCTION OVERLOADING

 Introduction:
 Polymorphism refers to one name having many forms, different behavior of an instance of an
object depending on situation.
 Polymorphism is achieved by function overloading or operator overloading.
 Inline function or friend function are used.

 Need for function overloading:


 Function overloading means two/more functions having same name but differ in number of
arguments or datatype of arguments.

 Advantages of function overloading:


 When different functions are created for different operations then user has to call respective
functions depending on situation.
 Instead for different situation if same function is called with different arguments using function
overloading, then compiler automatically decides which function to call by comparing the
arguments types, thus code is executed faster.
 Easier to understand the flow of information and debug.
 Code maintenance is easy.
 Easier interface between programs and real world objects.

 Definition and declarations of overloaded functions:


The main factor of function overloading is the functions argument list:
 C++ identifies overloaded functions by number and type of arguments.
 When two functions have same name and different types of arguments / different number of
arguments, function overloading is called by compiler automatically.
 It is also known as compile time polymorphism.
 Eg: int area (int s);
int area (int l, int b);
int area (int a, int b, int c);
 Here the function area has 1 integer argument which differs from function that takes 2 integer
arguments and 3 integer arguments. This is called as function overloading.
 To overload the function each overloaded functions must be declared and defined separately.
 The compiler automatically choose right type of function overloading depending on number of
arguments.

 Restrictions on overloaded functions:


 Each function in a set of overloaded function must have different argument list.

 Eg.: Program to show overloaded function.


#include<iostream.h>
#include<conio.h>
class functionoverload
{
SY 1
VVPUC, Tumkur

public: int area (int a)


{
return (a*a); //Area of square
}

int area ( int l, int b)


{
return (l*b); //Area of rectangle
}
};

void main( )
{
funtionoverload f;
clrscr( );
cout<<”Area of square = “<<f.area (10);
cout<<”Area of square = “<<f.area (10,20);
getch ( );
}

 Other function in a class:


 The member function of a class may be defined inside or outside the class.
 The function defined inside a class is called inline function.

 Inline function:
 Function call involves the process of invoking a function by passing parameter to the function.
Allocating space for local variables which uses extra time and memory space this can be
avoided using inline function.
 Inline function replaces function call with the body of the function.
 Keyword inline is used to define the inline function.
 Inline function consists of function call along with function code.
 Inline function definition start with keyword inline.
 The compiler replaces function call along with function code.
 They are little bit faster.

 Advantages of inline function:


 Inline members are compact of function calls.
 Size of object code is considerably reduced.
 Speed of execution of a program increases.
 Very efficient code can be generated.
 Readability of the program increases.

 Disadvantages of Inline function:


 As body of inline function is substituted in place of function call. Size of executable file increases
and more memory is needed.

SY 2
VVPUC, Tumkur

 Eg: Program to find area of square using inline function


#include<iostream.h>
#include<conio.h>
inline int area ( int a )
{
return (a*a);
}
void main ( )
{
int x , y;
clrscr( );
cout<<”Enter a number\n”;
cin>>x;
y = area(x);
cout<<”Area of square =”<<y;
getch ( );
}

NOTE:
i. Inline function may not work for sometime for some reason.
ii. Inline function definition is too long or complicated.
iii. Inline function is recursive.
iv. Inline function has looping constructs.
v. Inline function should have switch or goto.

 Friend Function:
 Private and protected members of a class cannot be accessed by non-member function.
 Common functions can be shared between two classes by making common function called
“Friend” to both the classes, there by allowing the function to have access to private data of both
the classes.
 “Friend function is a non-member function that is friend of a class which are declared with in a
class with prefix friend”.
 Friend function should be defined outside the class as normal function without prefix friend.
 It can access public members like non member functions.
 Syntax:
class classname
{
public: friend void function (void);
friend return_type_specifier functionname(arguments);
};

 Even though friend function is not member of the function it has full access to private and
protected members of a class.
 They can be invoked like any other function.
 They are normal external function that has special access privilages.
 It can access member variables as objectname.membername
 The function is declared with a keyword friend but while defining the code, the friend function
does not use either keyword friend or scope resolution operator.

SY 3
VVPUC, Tumkur

 Eg.: Program to add two numbers using friend function


#include<iostream.h>
#include<conio.h>
class add
{
private: int a,b;
public: void read ( int i, int j);
friend int sum (add a1);
};
void add :: read (int i, int j)
{
a = I;
b = j;
}
int sum (add a1)
{
return (add.a1+add.a2);
}
void main( )
{
add obj;
clrscr ( );
obj.read (10, 20);
cout<<”Sum= “<<sum(obj);
getch( );
}

SY 4

You might also like