SlideShare a Scribd company logo
By: Jagrati Mehra
Course: B. Sc. (CS) II year
Submitted to: Prof. Someshwar Joshi
Suppose there is two different objects of two
different classes.
class ABC
ABC a
int data
function()
{
........
}
class XYZ
XYZ x
int data
function()
{
........
}
class ABC
ABC a
int data
function()
{
........
}
class XYZ
XYZ x
int data
function()
{
........
}
But what if I need to access both data and
information ?
Here we use friend function.
Access not
possible
..!!!
Simple only add friend in front of member function. We
don’t need to write (friend) while defining out of class.
void display
{
//body
}
friend void display
{
//body
}
How is friend function is different from
normal function?
Let understand by a program-
Normal function Friend function
#include<iostream.h>
#include<conio.h>
class test
{
int data1,data2;
public:
test()
{}
test (int a, int b)
{
data1=a;
data2=b;
}
void display();//normal function
friend void multiply(test);//friend function
};
void test::display()//normal function in scope of class.
{
cout<<“data 1= ”<<data1;//directly access private variable
cout<<“data 2=“<<data2<<endl;
}
void multiply(test t)//object as argument.
{
int data0;
data0=t.data1*t.data2;//need object name and dot(.)
cout<<“multiplication =“<<data0;
}
void main()
{
test obj(100,200);
obj.display();//need object name and dot(.) while calling.
multiply(obj);//direct calling.
getch();
}
 friend function need not required scope
resolution operator.
 friend function can be declared in private or
public doesn’t matter.
 friend function can not be called using object of
the class.
 friend function need object name and dot (.) to
access private members.
 Usually it passes objects as arguments.
 The function can define anywhere like a normal
function.
friend void total(boss b,employee e);
class boss
boss b
int salary
void display()
{
........
}
class employee
employee e
int salary
void display()
{
........
}
#include<iostream.h>
#include<conio.h>
class boss
{
int salary;
public:
boss()
{}
boss(int a)
{
salary=a;
}
void display()
{
cout<<“Boss Salary =“<<salary;
}
friend void total(boss,employee);
};
class employee
{
int salary;
public:
employee()
{}
employee(int b)
{
salary=b;
}
void display()
{
cout<<“Employee Salary =“<<salary;
}
friend void total(boss,employee);
};
void total_salary(boss b,employee e)
{
int total;
total=b.salary+e.salary;
cout<<“Total Salary =“<<total<<endl;
}
void main()
{
boss b(50000);
b.display();
employee e(25000);
e.display();
total(b,e);
getch();
}
Boss Salary =50000
Employee Salary=25000
Total Salary = 75000
Friend function in c++

More Related Content

What's hot

Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
Md. Ashraful Islam
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
Tamanna Akter
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctorSomnath Kulkarni
 
Function overloading
Function overloadingFunction overloading
Function overloading
Selvin Josy Bai Somu
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
ThamizhselviKrishnam
 
Access specifier
Access specifierAccess specifier
Access specifier
zindadili
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
RubaNagarajan
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
Hashim Hashim
 
Friend Function
Friend FunctionFriend Function
Friend Function
Mehak Tawakley
 
Late and Early binding in c++
Late and Early binding in c++Late and Early binding in c++
Late and Early binding in c++
FazalRehman79
 
Inheritance
InheritanceInheritance
Inheritance
PriyankaAkhil
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 

What's hot (20)

Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Access specifier
Access specifierAccess specifier
Access specifier
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
Friend Function
Friend FunctionFriend Function
Friend Function
 
Late and Early binding in c++
Late and Early binding in c++Late and Early binding in c++
Late and Early binding in c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 

Similar to Friend function in c++

Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
Swarup Boro
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
zahid khan
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
Rai University
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
Rai University
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
Class and object
Class and objectClass and object
Class and object
prabhat kumar
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++M Hussnain Ali
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
Dev Chauhan
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
study material
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
M Vishnuvardhan Reddy
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Kumar Boro
 
C++ classes
C++ classesC++ classes
C++ classes
imhammadali
 
Intro to Python (High School) Unit #2
Intro to Python (High School) Unit #2Intro to Python (High School) Unit #2
Intro to Python (High School) Unit #2
Jay Coskey
 
Preexisting code, please useMain.javapublic class Main { p.pdf
Preexisting code, please useMain.javapublic class Main {    p.pdfPreexisting code, please useMain.javapublic class Main {    p.pdf
Preexisting code, please useMain.javapublic class Main { p.pdf
rd1742
 
Class and object
Class and objectClass and object
Class and object
Prof. Dr. K. Adisesha
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
Epsiba1
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
Shahzad Younas
 

Similar to Friend function in c++ (20)

Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Class and object
Class and objectClass and object
Class and object
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 
ccc
cccccc
ccc
 
cse l 5.pptx
cse l 5.pptxcse l 5.pptx
cse l 5.pptx
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
Csharp_Chap03
Csharp_Chap03Csharp_Chap03
Csharp_Chap03
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
C++ classes
C++ classesC++ classes
C++ classes
 
Intro to Python (High School) Unit #2
Intro to Python (High School) Unit #2Intro to Python (High School) Unit #2
Intro to Python (High School) Unit #2
 
Preexisting code, please useMain.javapublic class Main { p.pdf
Preexisting code, please useMain.javapublic class Main {    p.pdfPreexisting code, please useMain.javapublic class Main {    p.pdf
Preexisting code, please useMain.javapublic class Main { p.pdf
 
Class and object
Class and objectClass and object
Class and object
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
 

More from Jagrati Mehra

Yuva Sangam Report.pdf
Yuva Sangam Report.pdfYuva Sangam Report.pdf
Yuva Sangam Report.pdf
Jagrati Mehra
 
Mathematical Model for Waste Water Treatment.pptx
Mathematical Model for Waste Water Treatment.pptxMathematical Model for Waste Water Treatment.pptx
Mathematical Model for Waste Water Treatment.pptx
Jagrati Mehra
 
Self Learning, Cooperative and Collaborative Learning
Self Learning, Cooperative and Collaborative LearningSelf Learning, Cooperative and Collaborative Learning
Self Learning, Cooperative and Collaborative Learning
Jagrati Mehra
 
Learner as a develpoing individual
Learner as a develpoing individualLearner as a develpoing individual
Learner as a develpoing individual
Jagrati Mehra
 
Plato's Philosophy of Mathematics
Plato's Philosophy of MathematicsPlato's Philosophy of Mathematics
Plato's Philosophy of Mathematics
Jagrati Mehra
 
Issue related to teacher motivation, working conditions in urban and rural areas
Issue related to teacher motivation, working conditions in urban and rural areasIssue related to teacher motivation, working conditions in urban and rural areas
Issue related to teacher motivation, working conditions in urban and rural areas
Jagrati Mehra
 
Problems of Teacher Training or Teacher Education
Problems of Teacher Training or Teacher EducationProblems of Teacher Training or Teacher Education
Problems of Teacher Training or Teacher Education
Jagrati Mehra
 
Buddhist Education for B. Ed. course
Buddhist Education for B. Ed. courseBuddhist Education for B. Ed. course
Buddhist Education for B. Ed. course
Jagrati Mehra
 
Cyclotron
CyclotronCyclotron
Cyclotron
Jagrati Mehra
 
Water pollution
Water pollutionWater pollution
Water pollution
Jagrati Mehra
 

More from Jagrati Mehra (10)

Yuva Sangam Report.pdf
Yuva Sangam Report.pdfYuva Sangam Report.pdf
Yuva Sangam Report.pdf
 
Mathematical Model for Waste Water Treatment.pptx
Mathematical Model for Waste Water Treatment.pptxMathematical Model for Waste Water Treatment.pptx
Mathematical Model for Waste Water Treatment.pptx
 
Self Learning, Cooperative and Collaborative Learning
Self Learning, Cooperative and Collaborative LearningSelf Learning, Cooperative and Collaborative Learning
Self Learning, Cooperative and Collaborative Learning
 
Learner as a develpoing individual
Learner as a develpoing individualLearner as a develpoing individual
Learner as a develpoing individual
 
Plato's Philosophy of Mathematics
Plato's Philosophy of MathematicsPlato's Philosophy of Mathematics
Plato's Philosophy of Mathematics
 
Issue related to teacher motivation, working conditions in urban and rural areas
Issue related to teacher motivation, working conditions in urban and rural areasIssue related to teacher motivation, working conditions in urban and rural areas
Issue related to teacher motivation, working conditions in urban and rural areas
 
Problems of Teacher Training or Teacher Education
Problems of Teacher Training or Teacher EducationProblems of Teacher Training or Teacher Education
Problems of Teacher Training or Teacher Education
 
Buddhist Education for B. Ed. course
Buddhist Education for B. Ed. courseBuddhist Education for B. Ed. course
Buddhist Education for B. Ed. course
 
Cyclotron
CyclotronCyclotron
Cyclotron
 
Water pollution
Water pollutionWater pollution
Water pollution
 

Recently uploaded

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 

Recently uploaded (20)

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 

Friend function in c++

  • 1. By: Jagrati Mehra Course: B. Sc. (CS) II year Submitted to: Prof. Someshwar Joshi
  • 2. Suppose there is two different objects of two different classes. class ABC ABC a int data function() { ........ } class XYZ XYZ x int data function() { ........ }
  • 3. class ABC ABC a int data function() { ........ } class XYZ XYZ x int data function() { ........ } But what if I need to access both data and information ? Here we use friend function. Access not possible ..!!!
  • 4. Simple only add friend in front of member function. We don’t need to write (friend) while defining out of class. void display { //body } friend void display { //body } How is friend function is different from normal function? Let understand by a program- Normal function Friend function
  • 5. #include<iostream.h> #include<conio.h> class test { int data1,data2; public: test() {} test (int a, int b) { data1=a; data2=b; } void display();//normal function friend void multiply(test);//friend function };
  • 6. void test::display()//normal function in scope of class. { cout<<“data 1= ”<<data1;//directly access private variable cout<<“data 2=“<<data2<<endl; } void multiply(test t)//object as argument. { int data0; data0=t.data1*t.data2;//need object name and dot(.) cout<<“multiplication =“<<data0; } void main() { test obj(100,200); obj.display();//need object name and dot(.) while calling. multiply(obj);//direct calling. getch(); }
  • 7.  friend function need not required scope resolution operator.  friend function can be declared in private or public doesn’t matter.  friend function can not be called using object of the class.  friend function need object name and dot (.) to access private members.  Usually it passes objects as arguments.  The function can define anywhere like a normal function.
  • 8. friend void total(boss b,employee e); class boss boss b int salary void display() { ........ } class employee employee e int salary void display() { ........ }
  • 9. #include<iostream.h> #include<conio.h> class boss { int salary; public: boss() {} boss(int a) { salary=a; } void display() { cout<<“Boss Salary =“<<salary; } friend void total(boss,employee); }; class employee { int salary; public: employee() {} employee(int b) { salary=b; } void display() { cout<<“Employee Salary =“<<salary; } friend void total(boss,employee); };
  • 10. void total_salary(boss b,employee e) { int total; total=b.salary+e.salary; cout<<“Total Salary =“<<total<<endl; } void main() { boss b(50000); b.display(); employee e(25000); e.display(); total(b,e); getch(); } Boss Salary =50000 Employee Salary=25000 Total Salary = 75000