SlideShare a Scribd company logo
4
Most read
5
Most read
6
Most read
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

PPTX
Pointer in c
PDF
chapter-11-pointers.pdf
PPTX
Networking concepts by Sachidananda M H
PPT
Java access modifiers
PDF
chapter-13-database-concepts.pdf
PDF
C Pointers
PPTX
Polymorphism in c++(ppt)
PPTX
Polymorphism Using C++
Pointer in c
chapter-11-pointers.pdf
Networking concepts by Sachidananda M H
Java access modifiers
chapter-13-database-concepts.pdf
C Pointers
Polymorphism in c++(ppt)
Polymorphism Using C++

What's hot (20)

PPT
Stacks
PPSX
Function in c
PPTX
Functions in c
PPTX
Data members and member functions
PPT
Strings
PDF
Function overloading ppt
PPT
Lists
PDF
chapter-7-classes-and-objects.pdf
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
String in programming language in c or c++
PPTX
Nested loop in C language
PDF
USER DEFINED FUNCTIONS IN C.pdf
PPT
C++ Memory Management
PDF
SQL BUILT-IN FUNCTION
PDF
2nd puc computer science chapter 3 data structures 1
PPTX
data structures with algorithms vtu 2023 notes.pptx
PPTX
Built in function
PPT
16717 functions in C++
 
PPTX
Dynamic Polymorphism in C++
PPSX
C Programming : Arrays
Stacks
Function in c
Functions in c
Data members and member functions
Strings
Function overloading ppt
Lists
chapter-7-classes-and-objects.pdf
Array Introduction One-dimensional array Multidimensional array
String in programming language in c or c++
Nested loop in C language
USER DEFINED FUNCTIONS IN C.pdf
C++ Memory Management
SQL BUILT-IN FUNCTION
2nd puc computer science chapter 3 data structures 1
data structures with algorithms vtu 2023 notes.pptx
Built in function
16717 functions in C++
 
Dynamic Polymorphism in C++
C Programming : Arrays
Ad

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

PDF
Function overloading
PDF
Functions in C++
PDF
chapter-8-function-overloading.pdf
PDF
A COMPLETE FILE FOR C++
PPTX
Presentation on polymorphism in c++.pptx
PPT
DS Unit 6.ppt
PPT
443600107-1-Introduction-to-C-ppt (1).ppt
PPT
443600107-1-Introduction-to education -C-ppt
PPTX
Functions in C++ (OOP)
PPTX
full defination of final opp.pptx
PPTX
Object Oriented Programming using C++ Unit 1
PPTX
3. Polymorphism.pptx
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
DOCX
Bc0037
PPTX
11.C++Polymorphism [Autosaved].pptx
PPT
static member and static member fumctions.ppt
PPTX
C++ Functions.pptx
PDF
Unit3_OOP-converted.pdf
PPTX
+2 CS class and objects
PPTX
C++ first s lide
Function overloading
Functions in C++
chapter-8-function-overloading.pdf
A COMPLETE FILE FOR C++
Presentation on polymorphism in c++.pptx
DS Unit 6.ppt
443600107-1-Introduction-to-C-ppt (1).ppt
443600107-1-Introduction-to education -C-ppt
Functions in C++ (OOP)
full defination of final opp.pptx
Object Oriented Programming using C++ Unit 1
3. Polymorphism.pptx
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
Bc0037
11.C++Polymorphism [Autosaved].pptx
static member and static member fumctions.ppt
C++ Functions.pptx
Unit3_OOP-converted.pdf
+2 CS class and objects
C++ first s lide
Ad

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Pre independence Education in Inndia.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
01-Introduction-to-Information-Management.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Lesson notes of climatology university.
PPTX
GDM (1) (1).pptx small presentation for students
RMMM.pdf make it easy to upload and study
102 student loan defaulters named and shamed – Is someone you know on the list?
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pre independence Education in Inndia.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
human mycosis Human fungal infections are called human mycosis..pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
01-Introduction-to-Information-Management.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Anesthesia in Laparoscopic Surgery in India
Microbial disease of the cardiovascular and lymphatic systems
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
TR - Agricultural Crops Production NC III.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
O7-L3 Supply Chain Operations - ICLT Program
Lesson notes of climatology university.
GDM (1) (1).pptx small presentation for students

2nd puc computer science chapter 8 function overloading

  • 1. 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. 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. 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. 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. 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. 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. 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]