SlideShare a Scribd company logo
1 of 17
Download to read offline
FUNCTION OVERLOADING
Prof. K. Adisesha
Learning Outcomes
ī¯ Introduction
ī¯ Defining Function
ī¯ Function overloading
ī¯ Calling Overloaded Functions
ī¯ Inline Function
ī¯ Friend Function
2
Introduction
Function
ī¯ 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.
3
Function Overloading
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.
4
Function Overloading
Definition and Declaration of overloaded functions:
ī¯ The main factor in function overloading is a functions
argument list.
ī¯ If functions having same name with different types of
arguments or different number of arguments, then it is
invoked automatically by the compiler.
ī¯ Function Overloading is also known as Compile time
polymorphism.
ī¯ Example:
īŽ int sum (int a, int b)
īŽ float sum ( float p, float q);
. 5
Function Overloading
ī¯ To overload a function, each overloaded function must be
declared and defined separately.
ī¯ Example:
int product ( int p, int q, int r); //Definition of overloaded functions
float product ( float x, float y);
int product ( int p, int q, int r) //Declaration of overloaded functions
{
cout<<”Product = “<<p * q * r << endl;
}
float product ( float x, float y, float z);
{
cout<< “Product = “ << x * y <<endl;
}
6
Function Overloading
Need for function overloading:
ī¯ The advantage of function overloading are:
īŽ Code is executed faster.
īŽ It is easier to understand the flow of information
and debug.
īŽ Code Maintenance is easy.
īŽ Easier interface between programs and real world
objects.
7
Inline Function
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.
ī¯ Rules:
īŽ Inline function definition starts with keyword inline.
īŽ The inline function should be defined before all function
that call it.
īŽ The compiler replaces the function call statement with the
function code itself and then compiles the entire code.
8
Inline Function
Inline function:
ī¯ The general format for the inline function declaration is given
below.
ī¯ Syntax:
inline Returntype Fun_Name ( [Argument] )
{ â€Ļâ€Ļâ€Ļ.. ;
return expression;
}
ī¯ Example:
9
#include<iostream.h>
inline int cube( int a )
{
return a * a * a;
}
void main( )
{
cout<<“Cube of 3 is”<<cube(3);
return 0;
}
Inline Function
ī¯ Program to check whether a number is prime or not using
inline function:
10
#include<iostream.h>
#include<conio.h>
inline int prime ( int n )
{
for(int i=2; i<n/2; i++)
if ( n % i = = 0)
return 0;
return 1;
}
void main( )
{
int num ;
clrscr( );
cout<<”Enter the input
number”<<endl;
cin>>num;
//inline function call
if ( prime(num))
cout<<”Number is Prime “ ;
else
cout<<”No. is not a Prime “ ;
getch();
}
Inline Function
Advantage of inline function:
ī¯ The size of the object code is considerably reduced.
ī¯ The speed of execution of a program increases.
ī¯ Very efficient code can be generated.
ī¯ There is no burden on the system for function calling.
ī¯ It also saves the overhead of return call from a function.
ī¯ The readability of the program increases.
11
Inline Function
Disadvantage of inline function:
ī¯ May increase the size of the executable file
ī¯ More memory is needed.
ī¯ If used in header file, it will make your header file size
large and may also make it unreadable
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 statement.
12
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
{
public:
friend return_type function_name ( [arguments] );
}
13
Friend Function
Properties of friend functions:
ī¯ Friend function is a non-member function of the class, has full access
permission to 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 (: :) 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:
objectname.membername.
ī¯ Use of friend function is rare, since it violates the rule of encapsulation and
data hiding.
14
Friend Function
#include<iostream.h>
#include<conio.h>
class number
{
private:
int a;
public:
void readdata( )
{
cout<<”Enter the Number”<<endl;
cin>>a;
}
friend int even(number);
};
int even(number n)
{
if(n.a % 2 = = 0)
return 1;
else
return 0;
}
void main( )
{
number num1;
num1.readadata( );
if( even(num1) ) //friend function call
cout<<”Number is Even”;
else
cout<<’Number is Odd”;
}
15
Program to check a number is even or odd using a friend function
Discussion
BLUE PRINT:
ī¯ Function Overloading and Member Function.
16
Discussion
Important Questions:
ī¯ What is function overloading? Explain the need for function
overloading.
ī¯ Discuss overloaded functions with syntax and example.
ī¯ What is inline function? Write a simple program for it.
ī¯ Mention the advantage and disadvantage of inline function.
ī¯ Explain friend function and their characteristics.
ī¯ Program to check whether a number is prime or not using
inline function.
17

More Related Content

What's hot

virtual function
virtual functionvirtual function
virtual functionVENNILAV6
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
Friend function
Friend functionFriend function
Friend functionzindadili
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access SpecifiersMuhammad Hammad Waseem
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++Rabin BK
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloadingMd. Ashraful Islam
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 

What's hot (20)

Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Functions in c
Functions in cFunctions in c
Functions in c
 
virtual function
virtual functionvirtual function
virtual function
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Friend function
Friend functionFriend function
Friend function
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Inline function
Inline functionInline function
Inline function
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
class and objects
class and objectsclass and objects
class and objects
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 

Similar to Function overloading ppt

chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfstudy material
 
2nd puc computer science chapter 8 function overloading
 2nd puc computer science chapter 8   function overloading 2nd puc computer science chapter 8   function overloading
2nd puc computer science chapter 8 function overloadingAahwini Esware gowda
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2yndaravind
 
Functions part1
Functions part1Functions part1
Functions part1yndaravind
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1ReKruiTIn.com
 
c.p function
c.p functionc.p function
c.p functiongiri5624
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1YOGESH SINGH
 
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFaisal Shehzad
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfSudhanshiBakre1
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
C++
C++C++
C++dean129
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functionsAlisha Korpal
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpuDhaval Jalalpara
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++Amresh Raj
 
Functionincprogram
FunctionincprogramFunctionincprogram
FunctionincprogramSampath Kumar
 

Similar to Function overloading ppt (20)

Function overloading
Function overloadingFunction overloading
Function overloading
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
 
2nd puc computer science chapter 8 function overloading
 2nd puc computer science chapter 8   function overloading 2nd puc computer science chapter 8   function overloading
2nd puc computer science chapter 8 function overloading
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 
Functions part1
Functions part1Functions part1
Functions part1
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
Funtion
FuntionFuntion
Funtion
 
c.p function
c.p functionc.p function
c.p function
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdf
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
C++
C++C++
C++
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
DS Unit 6.ppt
DS Unit 6.pptDS Unit 6.ppt
DS Unit 6.ppt
 

More from Prof. Dr. K. Adisesha

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfProf. Dr. K. Adisesha
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaProf. Dr. K. Adisesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfProf. Dr. K. Adisesha
 
Data_structure using C-Adi.pdf
Data_structure using C-Adi.pdfData_structure using C-Adi.pdf
Data_structure using C-Adi.pdfProf. Dr. K. Adisesha
 

More from Prof. Dr. K. Adisesha (20)

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdf
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdf
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdf
 
Introduction to Computers.pdf
Introduction to Computers.pdfIntroduction to Computers.pdf
Introduction to Computers.pdf
 
R_Programming.pdf
R_Programming.pdfR_Programming.pdf
R_Programming.pdf
 
Scholarship.pdf
Scholarship.pdfScholarship.pdf
Scholarship.pdf
 
Operating System-2 by Adi.pdf
Operating System-2 by Adi.pdfOperating System-2 by Adi.pdf
Operating System-2 by Adi.pdf
 
Operating System-1 by Adi.pdf
Operating System-1 by Adi.pdfOperating System-1 by Adi.pdf
Operating System-1 by Adi.pdf
 
Operating System-adi.pdf
Operating System-adi.pdfOperating System-adi.pdf
Operating System-adi.pdf
 
Data_structure using C-Adi.pdf
Data_structure using C-Adi.pdfData_structure using C-Adi.pdf
Data_structure using C-Adi.pdf
 
JAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdfJAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdf
 
JAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdfJAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdf
 

Recently uploaded

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
 

Function overloading ppt

  • 2. Learning Outcomes ī¯ Introduction ī¯ Defining Function ī¯ Function overloading ī¯ Calling Overloaded Functions ī¯ Inline Function ī¯ Friend Function 2
  • 3. Introduction Function ī¯ 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. 3
  • 4. Function Overloading 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. 4
  • 5. Function Overloading Definition and Declaration of overloaded functions: ī¯ The main factor in function overloading is a functions argument list. ī¯ If functions having same name with different types of arguments or different number of arguments, then it is invoked automatically by the compiler. ī¯ Function Overloading is also known as Compile time polymorphism. ī¯ Example: īŽ int sum (int a, int b) īŽ float sum ( float p, float q); . 5
  • 6. Function Overloading ī¯ To overload a function, each overloaded function must be declared and defined separately. ī¯ Example: int product ( int p, int q, int r); //Definition of overloaded functions float product ( float x, float y); int product ( int p, int q, int r) //Declaration of overloaded functions { cout<<”Product = “<<p * q * r << endl; } float product ( float x, float y, float z); { cout<< “Product = “ << x * y <<endl; } 6
  • 7. Function Overloading Need for function overloading: ī¯ The advantage of function overloading are: īŽ Code is executed faster. īŽ It is easier to understand the flow of information and debug. īŽ Code Maintenance is easy. īŽ Easier interface between programs and real world objects. 7
  • 8. Inline Function 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. ī¯ Rules: īŽ Inline function definition starts with keyword inline. īŽ The inline function should be defined before all function that call it. īŽ The compiler replaces the function call statement with the function code itself and then compiles the entire code. 8
  • 9. Inline Function Inline function: ī¯ The general format for the inline function declaration is given below. ī¯ Syntax: inline Returntype Fun_Name ( [Argument] ) { â€Ļâ€Ļâ€Ļ.. ; return expression; } ī¯ Example: 9 #include<iostream.h> inline int cube( int a ) { return a * a * a; } void main( ) { cout<<“Cube of 3 is”<<cube(3); return 0; }
  • 10. Inline Function ī¯ Program to check whether a number is prime or not using inline function: 10 #include<iostream.h> #include<conio.h> inline int prime ( int n ) { for(int i=2; i<n/2; i++) if ( n % i = = 0) return 0; return 1; } void main( ) { int num ; clrscr( ); cout<<”Enter the input number”<<endl; cin>>num; //inline function call if ( prime(num)) cout<<”Number is Prime “ ; else cout<<”No. is not a Prime “ ; getch(); }
  • 11. Inline Function Advantage of inline function: ī¯ The size of the object code is considerably reduced. ī¯ The speed of execution of a program increases. ī¯ Very efficient code can be generated. ī¯ There is no burden on the system for function calling. ī¯ It also saves the overhead of return call from a function. ī¯ The readability of the program increases. 11
  • 12. Inline Function Disadvantage of inline function: ī¯ May increase the size of the executable file ī¯ More memory is needed. ī¯ If used in header file, it will make your header file size large and may also make it unreadable 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 statement. 12
  • 13. 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 { public: friend return_type function_name ( [arguments] ); } 13
  • 14. Friend Function Properties of friend functions: ī¯ Friend function is a non-member function of the class, has full access permission to 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 (: :) 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: objectname.membername. ī¯ Use of friend function is rare, since it violates the rule of encapsulation and data hiding. 14
  • 15. Friend Function #include<iostream.h> #include<conio.h> class number { private: int a; public: void readdata( ) { cout<<”Enter the Number”<<endl; cin>>a; } friend int even(number); }; int even(number n) { if(n.a % 2 = = 0) return 1; else return 0; } void main( ) { number num1; num1.readadata( ); if( even(num1) ) //friend function call cout<<”Number is Even”; else cout<<’Number is Odd”; } 15 Program to check a number is even or odd using a friend function
  • 16. Discussion BLUE PRINT: ī¯ Function Overloading and Member Function. 16
  • 17. Discussion Important Questions: ī¯ What is function overloading? Explain the need for function overloading. ī¯ Discuss overloaded functions with syntax and example. ī¯ What is inline function? Write a simple program for it. ī¯ Mention the advantage and disadvantage of inline function. ī¯ Explain friend function and their characteristics. ī¯ Program to check whether a number is prime or not using inline function. 17