Function Overloading
Function Overloading
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.
void main( )
{
funtionoverload f;
clrscr( );
cout<<”Area of square = “<<f.area (10);
cout<<”Area of square = “<<f.area (10,20);
getch ( );
}
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.
SY 2
VVPUC, Tumkur
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
SY 4