Chapter 8 Function Overloading PDF
Chapter 8 Function Overloading PDF
Chapter-8
Introduction:
User defined function is a function defined by the user to solve his/her problem. Such a function
can be called from anywhere and any number of times in the program.
C++ implements polymorphism through function overloading and operator overloading.
Function overloading allows the user to create new abstract data type.
Function Overloading:
Function Overloading means two or more functions have same name, but differ in the number of
arguments or data types of arguments.
Function overloading is the process of defining same function name to carry out similar types of
activities with various data items.
2|Page
Chapter 8- Function Overloading and Member Function II PUC, MDRPUC, Hassan
Inline Function:
An Inline function is a special type of function whose body is inserted at the place where it is
called, instead of transferring the control to the function.
The keyword inline is used to define inline function.
Important
Rules:
5 Marks
o Inline function definition starts with keyword inline.
o The inline function should be defined before all function that call it.
o The compiler replaces the function call statement with the function code itself (expansion)
and then compiles the entire code.
The general format for the inline function declaration is given below:
inline Returntype Fun_Name ( [Argument] )
{
……….. ;
return expression ;
}
Program to find the cube of a number using inline function:
#include<iostream.h>
#include<conio.h>
inline int cube ( int a )
{
return a * a * a;
}
void main( )
{
int n ;
clrscr( ); OUTPUT:
cout<<”Enter the input number”<<endl;
cin>>n; Enter the input number
cout<<”Cube of “ <<n<<” = “<<cunbe(n); 4
getch(); Cube of 4 = 64
}
3|Page
Chapter 8- Function Overloading and Member Function II PUC, MDRPUC, Hassan
Note: The inline function may not work some times for one of the following reasons:
The inline function definition is too long or too complicated.
The inline function is recursive.
The inline function has looping constructs.
The inline function has a switch or goto.
Friend Function:
A friend function is a non-member function of a class has the access permission to the private
member of the class.
The friend function is declared within a class with the prefix friend.
But it should be defined outside the class like a normal function without the prefix friend.
The general format for the friend function is given below:
class class_name
Important
{
public:
5 Marks
friend return_type function_name ( [arguments] );
}
The friend functions have the following properties:
Friend function is not a member function of the class, has full access permission to private and
protected members of the class.
It can be declared either in public or private part of a class.
A friend function cannot be called using the object of that class. It can be invoked like any normal
function.
The function is declared with keyword friend. But while defining friend function it does not use
either keyword friend or : : operator.
They are normal external functions that are given special access privileges.
It cannot access the data member variables directly and has to use an object name.membername.
Use of friend function is rare, since it violates the rule of encapsulation and data hiding.
4|Page
Chapter 8- Function Overloading and Member Function II PUC, MDRPUC, Hassan
5|Page
Chapter 8- Function Overloading and Member Function II PUC, MDRPUC, Hassan
Important Questions
5 Marks Question:
6|Page