SlideShare a Scribd company logo
1 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
CHAPTER 8
FUNCTION OVERLOADING
Polymorphism is one of the main concepts of object oriented programming. Poly means many, and morph
means form. So Polymorphism is many formed.
There are two types of Polymorphism- Compile time polymorphism and Run time polymorphism.
Compile time polymorphism- in this type, code associated with a function call is known at the compile
time itself. Ex: Function overloading and operator overloading.
Runtime polymorphism- in this type, the code associated with the function call is known only during run
time(execution time). It is also known as Dynamic binding.
Function overloading:
The process of using same function name but different number and type of arguments to perform
different tasks is known as function overloading.
Function signature:
Argument list of a function that gives the number of arguments and types of arguments is called Function
signature.
Two functions are said to have same function signature if they use same number of arguments and type of
arguments. Function name can be different.It doesnot include return type of function.
Ex:
void function2(int x,float y,char ch);
void function5(int a,float b,char c); //function2() and function5() have same signature
Declaration and definition of function overloading:
To overload a function, we must have
1. Function names should be similar
2. All the functions should have different signatures.
Example: a sum() function to find sum of integers, real numbers and double.
In the above question, three member functions are needed.
int sum(int,int);
float sum(float,float);
double sum(double,double);
When functions are overloaded, compiler determines which function has to be executed based on
the number and type of arguments.
void SUM(int x, double y);
int SUM(int x,double y);
//generates syntax error because number.of arguments and their datatypes are same.
2 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
//program using function overloading without class
#include<iostream.h>
void display(int);
void display(float);
void display(char);
void main()
{
int a;
float b;
char c;
clrscr();
a=10;
b=45.78;
c=’&’;
display(a);
display(b);
display(c);
getch();
}
void display(int x)
{
cout<<”integer value=”<<x<<endl;
}
void display(float y)
{
cout<<”Real number=”<<y<<endl;
}
void display(char z)
{
cout<<”Character=”<<x<<endl;
}
//program using function overloading with class
#include<iostream.h>
int AREA(int);
int AREA(int,int);
float AREA (float,float);
class funcoverload
{
public:
int area(int s)
{
return(s*s);
}
int area(int l,int w)
{
3 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
return(l*w);
}
float area(float b,float h)
{
return(0.5*b*h);
}
};
void main()
{
funcoverload f; //creating object f
clrscr();
f.area(6); //accessing member function and passing integer value as argument
f.area(10,12);
f.area(3.5,6.8);
getch();
}
Advantages of function overloading:
1. Eliminates the use of different function names for same operation.
2. Helps to understand and debug easily
3. Easy to maintain code. Any changes can be made easy.
4. Better understanding of the relation between program and outside world.
5. Reduces complexity of program
Inline functions
Functions are used to save memory space but it creates overhead( passing arguments, returning values,
storing address of next instruction in calling function etc). To reduce the overhead and to increase the
speed of program execution, a new type of function is introduced--- INLINE function.
Inline function is a function whose function body is inserted at the place of function
call.
The compiler replaces the function call with the corresponding function body
Inline function increases the efficiency and speed of execution of program. But more memory is
needed if we call inline function many times.
Inline function definition is similarto a function definition but keyword inline precedes the
function name. It should be given before all functions that call it.
Syntax:
inline returntype functionname(argumentlist)
{
…………………………………………
return(expression);
}
Inline function is useful when calling function is small and straight line code with few
statements. No loops or branching statements are allowed.
Advantages:
4 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
1. Very efficient code can be generated.
2. Readability of program increase
3. Speed of execution of program increases
4. Size of object code is reduced
Disadvantage:
1. As function body is replaced in place of function call, the size of executable file increase and
more memory is needed.
Ex: Find cube of a number using inline function
#include<iostream.h>
inline double CUBE(double a)
{
return(a*a*a);
}
void main()
{
double n;
cout<<”enter a number”<<endl;
cin>>n;
cout<<”Cube of “<<n<<”=”<<CUBE(n);
getch();
}
Situations where inline functions may not work:
1. When inline function definition is to long or complicated
2. When inline functionis recursive.
3. When inline function has looping constructs
4. When inline function has switch or any branching statements or goto
Friend function:
Friend function is a non member function that can access the private and protected data
members of a class.
FUNCTION()
Friend function definition
Friend function declaration
Features of Friend function:
private
protected
Data or function
Data or function
friend FUNCTION();
………………………………………..
………………………………………………
……………………………………………..
Class A
5 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
• Keyword friend must precede the friend function declaration.
• Friend function is a non member function of a class
• It can access private and protected members of a class
• It is declared inside a class and can be defined inside or outside the class. Scope resolution
operator is not needed to access friend function outside the class.
• It receives objects of the class as arguments
• It can be invoked like a normal function without using any object
• It cannot access data member directly. It has to use object name and dot membership operator
with each member name.
• It can be declared either in public or private part of a class.
• Use of friend function is rare since it violates the rule of encapsulation and data hiding.
Ex: Program to find average of two numbers using friend function
#include<iostream.h>
class Sample
{
private:
int a,b;
public:
void readdata()
{
cout<<”enter value of a and b”<<endl;
cin>>a>>b;
}
friend float average(Sample s); //declaring friend function with object as argument
};
float average(Sample s) //defining friend function
{
return((s.a+s.b)/2.0) //accessing member a and b using object & dot operator
}
void main()
{
Sample x; //creating object
x.readdata();
cout<<”average value=”<<average(x) <<”n”; //calling friend function
}
A friend function can be used as a bridge between two classes.
#include<iostream.h>
class wife; //forward declaration that tells the compiler about classname before declaring it
class husband
{
private:
char name[20];
int salary;
public:
void readdata()
6 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
{
cout<<”input husband name”;
cin>> name;
cout<<input husband salary”;
cin>>salary;
}
friend int totalsalary(husband,wife);
}; //end of class husband
class wife
{
private:
char name[20];
int salary;
public:
void readdata()
{
cout<<enter wife name”
cin>>name;
cout<<enter wife salary”;
cin>>salary;
}
friend int totalsalary(husband,wife);
}; //end of class wife
int totalsalary(husband h,wife w)
{
return(h.salary+w.salary);
}
void main()
{
husband h;
wife w;
h.readdata();
w.readdata();
cout<<”total salary of the family is “<<totalsalary(h,w);
}
********************************
function over loading (32 question)
1What is a friend function? Write the characteristics of a friend
function.[2019]
2Define function overloading. Mention its advantages.[2019s]
7 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
3 Discuss overloaded functions with an example.[2018s]
4 Explain friend function with syntax and programming example.
[2018]
5 When function overloading is needed? Write any two advantages and
restrictions on overloading functions[2017s]
6 Explain inline function with programming example.[2017] [2015]
7 What are the advantages and disadvantages of inline
function?[2016s]
8 Describe briefly the use of friend function in C++ with syntax and
example.[2016]
9 What is function overloading? Explain the need for
overloading.[2015s][2020]

More Related Content

What's hot (20)

C# Types of classes
C# Types of classesC# Types of classes
C# Types of classes
Prem Kumar Badri
 
Friend function in c++
Friend function in c++ Friend function in c++
Friend function in c++
University of Madras
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
PhD Research Scholar
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
2nd PUC computer science chapter 6 oop concept
2nd PUC computer science chapter 6   oop concept2nd PUC computer science chapter 6   oop concept
2nd PUC computer science chapter 6 oop concept
Aahwini Esware gowda
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
study material
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
Fahim Adil
 
Networking concepts by Sachidananda M H
Networking concepts by Sachidananda M HNetworking concepts by Sachidananda M H
Networking concepts by Sachidananda M H
Sachidananda M H
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
Praveen M Jigajinni
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
Sujan Mia
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Socat
SocatSocat
Socat
Nitin Kumar Yadav
 
Member Function in C++
Member Function in C++ Member Function in C++
Member Function in C++
NikitaKaur10
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
Grishma Rajput
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
Lovely Professional University
 
Friend Function
Friend FunctionFriend Function
Friend Function
Mehak Tawakley
 

Similar to 2nd puc computer science chapter 8 function overloading (20)

chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
study material
 
Function overloading
Function overloadingFunction overloading
Function overloading
Prof. Dr. K. Adisesha
 
polymorphism for b.tech iii year students
polymorphism for b.tech iii year studentspolymorphism for b.tech iii year students
polymorphism for b.tech iii year students
Somesh Kumar
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Pranali Chaudhari
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
Muhammed Thanveer M
 
polymorphism.pdf
polymorphism.pdfpolymorphism.pdf
polymorphism.pdf
riyawagh2
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
ishan743441
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
Polymorphism & Templates
Polymorphism & TemplatesPolymorphism & Templates
Polymorphism & Templates
Meghaj Mallick
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
PRINCE KUMAR
 
Object Oriented Programming notes provided
Object Oriented Programming notes providedObject Oriented Programming notes provided
Object Oriented Programming notes provided
dummydoona
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
Amresh Raj
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
aptechsravan
 
Virtual function
Virtual functionVirtual function
Virtual function
sdrhr
 
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Lecture 4, c++(complete reference,herbet sheidt)chapter-14Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Abu Saleh
 
EEE 3rd year oops cat 3 ans
EEE 3rd year  oops cat 3  ansEEE 3rd year  oops cat 3  ans
EEE 3rd year oops cat 3 ans
Karthik Venkatachalam
 
Comparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphismComparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphism
CHAITALIUKE1
 
Bc0037
Bc0037Bc0037
Bc0037
hayerpa
 
C questions
C questionsC questions
C questions
parm112
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
study material
 
polymorphism for b.tech iii year students
polymorphism for b.tech iii year studentspolymorphism for b.tech iii year students
polymorphism for b.tech iii year students
Somesh Kumar
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
Muhammed Thanveer M
 
polymorphism.pdf
polymorphism.pdfpolymorphism.pdf
polymorphism.pdf
riyawagh2
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
ishan743441
 
Polymorphism & Templates
Polymorphism & TemplatesPolymorphism & Templates
Polymorphism & Templates
Meghaj Mallick
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
PRINCE KUMAR
 
Object Oriented Programming notes provided
Object Oriented Programming notes providedObject Oriented Programming notes provided
Object Oriented Programming notes provided
dummydoona
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
Amresh Raj
 
Virtual function
Virtual functionVirtual function
Virtual function
sdrhr
 
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Lecture 4, c++(complete reference,herbet sheidt)chapter-14Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Abu Saleh
 
Comparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphismComparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphism
CHAITALIUKE1
 
C questions
C questionsC questions
C questions
parm112
 

Recently uploaded (20)

KNN,Weighted KNN,Nearest Centroid Classifier,Locally Weighted Regression
KNN,Weighted KNN,Nearest Centroid Classifier,Locally Weighted RegressionKNN,Weighted KNN,Nearest Centroid Classifier,Locally Weighted Regression
KNN,Weighted KNN,Nearest Centroid Classifier,Locally Weighted Regression
Global Academy of Technology
 
THE FEMALE POPE IN SAINT PETER'S BASILICA
THE FEMALE POPE IN SAINT PETER'S BASILICATHE FEMALE POPE IN SAINT PETER'S BASILICA
THE FEMALE POPE IN SAINT PETER'S BASILICA
Claude LaCombe
 
Order Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptxOrder Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptx
Arshad Shaikh
 
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANASTUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
Kweku Zurek
 
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptxQUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
Sourav Kr Podder
 
Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...
Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...
Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...
ChatanBawankar
 
The Ellipsis Manual Analysis And Engineering Of Human Behavior Chase Hughes
The Ellipsis Manual Analysis And Engineering Of Human Behavior Chase HughesThe Ellipsis Manual Analysis And Engineering Of Human Behavior Chase Hughes
The Ellipsis Manual Analysis And Engineering Of Human Behavior Chase Hughes
pekokmupei
 
QUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptx
QUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptxQUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptx
QUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptx
Sourav Kr Podder
 
Research Handbook On Environment And Investment Law Kate Miles
Research Handbook On Environment And Investment Law Kate MilesResearch Handbook On Environment And Investment Law Kate Miles
Research Handbook On Environment And Investment Law Kate Miles
mucomousamir
 
Drug Metabolism advanced medicinal chemistry.pptx
Drug Metabolism advanced medicinal chemistry.pptxDrug Metabolism advanced medicinal chemistry.pptx
Drug Metabolism advanced medicinal chemistry.pptx
pharmaworld
 
How to Use Owl Slots in Odoo 17 - Odoo Slides
How to Use Owl Slots in Odoo 17 - Odoo SlidesHow to Use Owl Slots in Odoo 17 - Odoo Slides
How to Use Owl Slots in Odoo 17 - Odoo Slides
Celine George
 
Flower Identification Class-10 by Kushal Lamichhane.pdf
Flower Identification Class-10 by Kushal Lamichhane.pdfFlower Identification Class-10 by Kushal Lamichhane.pdf
Flower Identification Class-10 by Kushal Lamichhane.pdf
kushallamichhame
 
EVALUATION AND MANAGEMENT OF OPEN FRACTURE
EVALUATION AND MANAGEMENT OF OPEN FRACTUREEVALUATION AND MANAGEMENT OF OPEN FRACTURE
EVALUATION AND MANAGEMENT OF OPEN FRACTURE
BipulBorthakur
 
How to Configure Subcontracting in Odoo 18 Manufacturing
How to Configure Subcontracting in Odoo 18 ManufacturingHow to Configure Subcontracting in Odoo 18 Manufacturing
How to Configure Subcontracting in Odoo 18 Manufacturing
Celine George
 
Protest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE EnglishProtest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE English
jpinnuck
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdfTechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup
 
How to create and manage blogs in odoo 18
How to create and manage blogs in odoo 18How to create and manage blogs in odoo 18
How to create and manage blogs in odoo 18
Celine George
 
Sri Guru Arjun Dev Ji .
Sri Guru Arjun Dev Ji                   .Sri Guru Arjun Dev Ji                   .
Sri Guru Arjun Dev Ji .
Balvir Singh
 
Policies, procedures, subject selection and QTAC.pptx
Policies, procedures, subject selection and QTAC.pptxPolicies, procedures, subject selection and QTAC.pptx
Policies, procedures, subject selection and QTAC.pptx
mansk2
 
5503 Course Proposal Online Computer Middle School Course Wood M.pdf
5503 Course Proposal Online Computer Middle School Course Wood M.pdf5503 Course Proposal Online Computer Middle School Course Wood M.pdf
5503 Course Proposal Online Computer Middle School Course Wood M.pdf
Melanie Wood
 
KNN,Weighted KNN,Nearest Centroid Classifier,Locally Weighted Regression
KNN,Weighted KNN,Nearest Centroid Classifier,Locally Weighted RegressionKNN,Weighted KNN,Nearest Centroid Classifier,Locally Weighted Regression
KNN,Weighted KNN,Nearest Centroid Classifier,Locally Weighted Regression
Global Academy of Technology
 
THE FEMALE POPE IN SAINT PETER'S BASILICA
THE FEMALE POPE IN SAINT PETER'S BASILICATHE FEMALE POPE IN SAINT PETER'S BASILICA
THE FEMALE POPE IN SAINT PETER'S BASILICA
Claude LaCombe
 
Order Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptxOrder Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptx
Arshad Shaikh
 
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANASTUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
Kweku Zurek
 
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptxQUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
Sourav Kr Podder
 
Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...
Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...
Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...
ChatanBawankar
 
The Ellipsis Manual Analysis And Engineering Of Human Behavior Chase Hughes
The Ellipsis Manual Analysis And Engineering Of Human Behavior Chase HughesThe Ellipsis Manual Analysis And Engineering Of Human Behavior Chase Hughes
The Ellipsis Manual Analysis And Engineering Of Human Behavior Chase Hughes
pekokmupei
 
QUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptx
QUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptxQUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptx
QUIZ-O-FORCE 3.0 FINAL SET BY SOURAV .pptx
Sourav Kr Podder
 
Research Handbook On Environment And Investment Law Kate Miles
Research Handbook On Environment And Investment Law Kate MilesResearch Handbook On Environment And Investment Law Kate Miles
Research Handbook On Environment And Investment Law Kate Miles
mucomousamir
 
Drug Metabolism advanced medicinal chemistry.pptx
Drug Metabolism advanced medicinal chemistry.pptxDrug Metabolism advanced medicinal chemistry.pptx
Drug Metabolism advanced medicinal chemistry.pptx
pharmaworld
 
How to Use Owl Slots in Odoo 17 - Odoo Slides
How to Use Owl Slots in Odoo 17 - Odoo SlidesHow to Use Owl Slots in Odoo 17 - Odoo Slides
How to Use Owl Slots in Odoo 17 - Odoo Slides
Celine George
 
Flower Identification Class-10 by Kushal Lamichhane.pdf
Flower Identification Class-10 by Kushal Lamichhane.pdfFlower Identification Class-10 by Kushal Lamichhane.pdf
Flower Identification Class-10 by Kushal Lamichhane.pdf
kushallamichhame
 
EVALUATION AND MANAGEMENT OF OPEN FRACTURE
EVALUATION AND MANAGEMENT OF OPEN FRACTUREEVALUATION AND MANAGEMENT OF OPEN FRACTURE
EVALUATION AND MANAGEMENT OF OPEN FRACTURE
BipulBorthakur
 
How to Configure Subcontracting in Odoo 18 Manufacturing
How to Configure Subcontracting in Odoo 18 ManufacturingHow to Configure Subcontracting in Odoo 18 Manufacturing
How to Configure Subcontracting in Odoo 18 Manufacturing
Celine George
 
Protest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE EnglishProtest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE English
jpinnuck
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdfTechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.05.28.pdf
TechSoup
 
How to create and manage blogs in odoo 18
How to create and manage blogs in odoo 18How to create and manage blogs in odoo 18
How to create and manage blogs in odoo 18
Celine George
 
Sri Guru Arjun Dev Ji .
Sri Guru Arjun Dev Ji                   .Sri Guru Arjun Dev Ji                   .
Sri Guru Arjun Dev Ji .
Balvir Singh
 
Policies, procedures, subject selection and QTAC.pptx
Policies, procedures, subject selection and QTAC.pptxPolicies, procedures, subject selection and QTAC.pptx
Policies, procedures, subject selection and QTAC.pptx
mansk2
 
5503 Course Proposal Online Computer Middle School Course Wood M.pdf
5503 Course Proposal Online Computer Middle School Course Wood M.pdf5503 Course Proposal Online Computer Middle School Course Wood M.pdf
5503 Course Proposal Online Computer Middle School Course Wood M.pdf
Melanie Wood
 

2nd puc computer science chapter 8 function overloading

  • 1. 1 YouTube channel: study with me Ashwini e Ashwini E [email protected] CHAPTER 8 FUNCTION OVERLOADING Polymorphism is one of the main concepts of object oriented programming. Poly means many, and morph means form. So Polymorphism is many formed. There are two types of Polymorphism- Compile time polymorphism and Run time polymorphism. Compile time polymorphism- in this type, code associated with a function call is known at the compile time itself. Ex: Function overloading and operator overloading. Runtime polymorphism- in this type, the code associated with the function call is known only during run time(execution time). It is also known as Dynamic binding. Function overloading: The process of using same function name but different number and type of arguments to perform different tasks is known as function overloading. Function signature: Argument list of a function that gives the number of arguments and types of arguments is called Function signature. Two functions are said to have same function signature if they use same number of arguments and type of arguments. Function name can be different.It doesnot include return type of function. Ex: void function2(int x,float y,char ch); void function5(int a,float b,char c); //function2() and function5() have same signature Declaration and definition of function overloading: To overload a function, we must have 1. Function names should be similar 2. All the functions should have different signatures. Example: a sum() function to find sum of integers, real numbers and double. In the above question, three member functions are needed. int sum(int,int); float sum(float,float); double sum(double,double); When functions are overloaded, compiler determines which function has to be executed based on the number and type of arguments. void SUM(int x, double y); int SUM(int x,double y); //generates syntax error because number.of arguments and their datatypes are same.
  • 2. 2 YouTube channel: study with me Ashwini e Ashwini E [email protected] //program using function overloading without class #include<iostream.h> void display(int); void display(float); void display(char); void main() { int a; float b; char c; clrscr(); a=10; b=45.78; c=’&’; display(a); display(b); display(c); getch(); } void display(int x) { cout<<”integer value=”<<x<<endl; } void display(float y) { cout<<”Real number=”<<y<<endl; } void display(char z) { cout<<”Character=”<<x<<endl; } //program using function overloading with class #include<iostream.h> int AREA(int); int AREA(int,int); float AREA (float,float); class funcoverload { public: int area(int s) { return(s*s); } int area(int l,int w) {
  • 3. 3 YouTube channel: study with me Ashwini e Ashwini E [email protected] return(l*w); } float area(float b,float h) { return(0.5*b*h); } }; void main() { funcoverload f; //creating object f clrscr(); f.area(6); //accessing member function and passing integer value as argument f.area(10,12); f.area(3.5,6.8); getch(); } Advantages of function overloading: 1. Eliminates the use of different function names for same operation. 2. Helps to understand and debug easily 3. Easy to maintain code. Any changes can be made easy. 4. Better understanding of the relation between program and outside world. 5. Reduces complexity of program Inline functions Functions are used to save memory space but it creates overhead( passing arguments, returning values, storing address of next instruction in calling function etc). To reduce the overhead and to increase the speed of program execution, a new type of function is introduced--- INLINE function. Inline function is a function whose function body is inserted at the place of function call. The compiler replaces the function call with the corresponding function body Inline function increases the efficiency and speed of execution of program. But more memory is needed if we call inline function many times. Inline function definition is similarto a function definition but keyword inline precedes the function name. It should be given before all functions that call it. Syntax: inline returntype functionname(argumentlist) { ………………………………………… return(expression); } Inline function is useful when calling function is small and straight line code with few statements. No loops or branching statements are allowed. Advantages:
  • 4. 4 YouTube channel: study with me Ashwini e Ashwini E [email protected] 1. Very efficient code can be generated. 2. Readability of program increase 3. Speed of execution of program increases 4. Size of object code is reduced Disadvantage: 1. As function body is replaced in place of function call, the size of executable file increase and more memory is needed. Ex: Find cube of a number using inline function #include<iostream.h> inline double CUBE(double a) { return(a*a*a); } void main() { double n; cout<<”enter a number”<<endl; cin>>n; cout<<”Cube of “<<n<<”=”<<CUBE(n); getch(); } Situations where inline functions may not work: 1. When inline function definition is to long or complicated 2. When inline functionis recursive. 3. When inline function has looping constructs 4. When inline function has switch or any branching statements or goto Friend function: Friend function is a non member function that can access the private and protected data members of a class. FUNCTION() Friend function definition Friend function declaration Features of Friend function: private protected Data or function Data or function friend FUNCTION(); ……………………………………….. ……………………………………………… …………………………………………….. Class A
  • 5. 5 YouTube channel: study with me Ashwini e Ashwini E [email protected] • Keyword friend must precede the friend function declaration. • Friend function is a non member function of a class • It can access private and protected members of a class • It is declared inside a class and can be defined inside or outside the class. Scope resolution operator is not needed to access friend function outside the class. • It receives objects of the class as arguments • It can be invoked like a normal function without using any object • It cannot access data member directly. It has to use object name and dot membership operator with each member name. • It can be declared either in public or private part of a class. • Use of friend function is rare since it violates the rule of encapsulation and data hiding. Ex: Program to find average of two numbers using friend function #include<iostream.h> class Sample { private: int a,b; public: void readdata() { cout<<”enter value of a and b”<<endl; cin>>a>>b; } friend float average(Sample s); //declaring friend function with object as argument }; float average(Sample s) //defining friend function { return((s.a+s.b)/2.0) //accessing member a and b using object & dot operator } void main() { Sample x; //creating object x.readdata(); cout<<”average value=”<<average(x) <<”n”; //calling friend function } A friend function can be used as a bridge between two classes. #include<iostream.h> class wife; //forward declaration that tells the compiler about classname before declaring it class husband { private: char name[20]; int salary; public: void readdata()
  • 6. 6 YouTube channel: study with me Ashwini e Ashwini E [email protected] { cout<<”input husband name”; cin>> name; cout<<input husband salary”; cin>>salary; } friend int totalsalary(husband,wife); }; //end of class husband class wife { private: char name[20]; int salary; public: void readdata() { cout<<enter wife name” cin>>name; cout<<enter wife salary”; cin>>salary; } friend int totalsalary(husband,wife); }; //end of class wife int totalsalary(husband h,wife w) { return(h.salary+w.salary); } void main() { husband h; wife w; h.readdata(); w.readdata(); cout<<”total salary of the family is “<<totalsalary(h,w); } ******************************** function over loading (32 question) 1What is a friend function? Write the characteristics of a friend function.[2019] 2Define function overloading. Mention its advantages.[2019s]
  • 7. 7 YouTube channel: study with me Ashwini e Ashwini E [email protected] 3 Discuss overloaded functions with an example.[2018s] 4 Explain friend function with syntax and programming example. [2018] 5 When function overloading is needed? Write any two advantages and restrictions on overloading functions[2017s] 6 Explain inline function with programming example.[2017] [2015] 7 What are the advantages and disadvantages of inline function?[2016s] 8 Describe briefly the use of friend function in C++ with syntax and example.[2016] 9 What is function overloading? Explain the need for overloading.[2015s][2020]