SlideShare uma empresa Scribd logo
1 de 7
Baixar para ler offline
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]

Mais conteúdo relacionado

Mais procurados

Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
Tech_MX
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Dhaval Dalal
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
Deepak Singh
 

Mais procurados (20)

Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
 
Characteristics of oop
Characteristics of oopCharacteristics of oop
Characteristics of oop
 
Concepts of OOPs
Concepts of OOPsConcepts of OOPs
Concepts of OOPs
 
Java reflection
Java reflectionJava reflection
Java reflection
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
C and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharC and C++ Industrial Training Jalandhar
C and C++ Industrial Training Jalandhar
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Angular Unit Testing
Angular Unit TestingAngular Unit Testing
Angular Unit Testing
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
 

Semelhante a 2nd puc computer science chapter 8 function overloading

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
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Amir Ali
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
nirajmandaliya
 

Semelhante a 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
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
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
 
polymorphism.pdf
polymorphism.pdfpolymorphism.pdf
polymorphism.pdf
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Polymorphism & Templates
Polymorphism & TemplatesPolymorphism & Templates
Polymorphism & Templates
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
 
Virtual function
Virtual functionVirtual function
Virtual function
 
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
 
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
 
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
 
Bc0037
Bc0037Bc0037
Bc0037
 
C questions
C questionsC questions
C questions
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Oop concepts
Oop conceptsOop concepts
Oop concepts
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 

Mais de Aahwini Esware gowda (6)

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
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
 
2nd puc computer science chapter 3 data structures 1
2nd puc computer science chapter 3 data structures 12nd puc computer science chapter 3 data structures 1
2nd puc computer science chapter 3 data structures 1
 
2nd PUC computer science chapter 2 boolean algebra
2nd PUC computer science chapter 2  boolean algebra 2nd PUC computer science chapter 2  boolean algebra
2nd PUC computer science chapter 2 boolean algebra
 
2nd PUC computer science chapter 2 boolean algebra 1
2nd PUC computer science chapter 2  boolean algebra 12nd PUC computer science chapter 2  boolean algebra 1
2nd PUC computer science chapter 2 boolean algebra 1
 
2nd puc computer science chapter 1 backdrop of computers
2nd puc computer science chapter 1  backdrop of computers 2nd puc computer science chapter 1  backdrop of computers
2nd puc computer science chapter 1 backdrop of computers
 

Último

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 

Último (20)

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 

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]