SlideShare a Scribd company logo
Friend Function in C++
Mrs. J P Jankar-Tamkhade
friend Function
2
 A function outside the class can be made friend of class
 It can access private and protected members of the class in
which it is declared as friend
 It is declared inside the class as
friend ret_type function_name(parameters);
 Examples
1.Time
2. Complex numbers
3. Matrices
3
friend Function
#include <iostream>
using namespace std;
class Shape
{
double width;
public:
friend void print( Shape S1 );
void set( double wid );
};
// Member function definition
void Shape::set( double wid )
{
width = wid;
}
void print( Shape S )
{
cout << "Width of box : " << S.width ;
}
// Main function for the program
int main( )
{
Shape S1;
S1.set(10.0);
// Use friend function to print the wdith.
print( S1);
return 0;
}
Example of friend Function
4
#include<iostream>
using namespace std;
class B
{
int val1,val2;
public:
void get()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend float mean(B ob);
};
5
float mean(B ob)
{
return float(ob.val1+ob.val2)/2;
}
int main()
{
B obj;
obj.get();
cout<<"n Mean value is : "<<mean(obj);
return 0;
}
6
#include <iostream>
using namespace std;
class ONE;
class TWO
{
public:
void print(ONE& x);
};
class ONE
{
int a, b;
friend voidTWO::print(ONE& x);
public:
void getdata()
{
cin>>a>>b;
}
};
7
voidTWO::print(ONE& x)
{
cout << "a is " << x.a << endl;
cout << "b is " << x.b << endl;
}
int main()
{
ONE xobj;
TWO yobj;
xobj.getdata();
yobj.print(xobj);
return 0;
}
Friend Class
8
 Friend Class can access the private and protected
members of the class for which it is friend ,it is done by a
declaring prototype of this external function within the
class, and with the keyword friend.
 Friend class is used in C++ to gain access to protected
members exclusively by the friend class
9
Example of Friend Class
#include<iostream>
using namespace std;
class getdata
{
float num1,num2;
public:
void read()
{
cout<<"nnEnter the First Number : ";
cin>>num1;
cout<<"nnEnter the Second Number : ";
cin>>num2;
}
friend class add;
};
10
class add
{
public:
float c;
void add1(getdata G)
{
c=G.num1+G.num2;
cout<<"nnSum="<<c;
}
};
int main()
{
int cont;
getdata G1;
add A;
do {
G1.read();
A.add1(G1);
cout<<"nnDo you want to continue?
(1-YES,0-NO)";
cin>>cont;
}while(cont==1);
return 0;
}
Thank you

More Related Content

PPTX
Friend function
PPTX
Friend_Function_in_CPP its use and examples.
PPTX
friend_derivedclasspresentation1234.pptx
PPTX
424570636-friend-function-1-pptx.hiipptx
PPTX
Friend function OOPC
PPTX
cse l 5.pptx
PPTX
Friend Function
PPT
friend function(c++)
Friend function
Friend_Function_in_CPP its use and examples.
friend_derivedclasspresentation1234.pptx
424570636-friend-function-1-pptx.hiipptx
Friend function OOPC
cse l 5.pptx
Friend Function
friend function(c++)

Similar to Study of Friend Function in C++ with examples (20)

PPT
12-170211073924.ppthihello heoobchdhdhdhhd
PDF
Friend function in c++
PPTX
Friend function
PDF
Friend Function Computer Programming.pdf
PPT
Friends function and_classes
PPTX
friend class & friend function by Komal V Rokade.pptx
PPTX
L9_Friend Function.pptxyeyeyeyeyyeyeyeye
PPTX
2.6 Friend Function which helps to learn how the friend function works
PPTX
Friend functions
PPTX
OOP Week 3 Lecture 2.pptx
PDF
Lec_15_OOP_ObjectsAsParametersFriendClasses.pdf
PDF
Chapter23 friend-function-friend-class
PPT
Mca 2nd sem u-2 classes & objects
PPT
Bca 2nd sem u-2 classes & objects
PPT
static member and static member fumctions.ppt
PDF
22 scheme OOPs with C++ BCS306B_module1.pdf
PPTX
Lecture-11 Friend Functions and inline functions.pptx
PPTX
Class and object
PPTX
+2 CS class and objects
PPTX
Friend function in c++
12-170211073924.ppthihello heoobchdhdhdhhd
Friend function in c++
Friend function
Friend Function Computer Programming.pdf
Friends function and_classes
friend class & friend function by Komal V Rokade.pptx
L9_Friend Function.pptxyeyeyeyeyyeyeyeye
2.6 Friend Function which helps to learn how the friend function works
Friend functions
OOP Week 3 Lecture 2.pptx
Lec_15_OOP_ObjectsAsParametersFriendClasses.pdf
Chapter23 friend-function-friend-class
Mca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
static member and static member fumctions.ppt
22 scheme OOPs with C++ BCS306B_module1.pdf
Lecture-11 Friend Functions and inline functions.pptx
Class and object
+2 CS class and objects
Friend function in c++
Ad

Recently uploaded (20)

PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Artificial Intelligence
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPT
Mechanical Engineering MATERIALS Selection
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Current and future trends in Computer Vision.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Artificial Intelligence
CH1 Production IntroductoryConcepts.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Lecture Notes Electrical Wiring System Components
Automation-in-Manufacturing-Chapter-Introduction.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Mechanical Engineering MATERIALS Selection
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Current and future trends in Computer Vision.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Foundation to blockchain - A guide to Blockchain Tech
Operating System & Kernel Study Guide-1 - converted.pdf
Ad

Study of Friend Function in C++ with examples

  • 1. Friend Function in C++ Mrs. J P Jankar-Tamkhade
  • 2. friend Function 2  A function outside the class can be made friend of class  It can access private and protected members of the class in which it is declared as friend  It is declared inside the class as friend ret_type function_name(parameters);  Examples 1.Time 2. Complex numbers 3. Matrices
  • 3. 3 friend Function #include <iostream> using namespace std; class Shape { double width; public: friend void print( Shape S1 ); void set( double wid ); }; // Member function definition void Shape::set( double wid ) { width = wid; } void print( Shape S ) { cout << "Width of box : " << S.width ; } // Main function for the program int main( ) { Shape S1; S1.set(10.0); // Use friend function to print the wdith. print( S1); return 0; }
  • 4. Example of friend Function 4 #include<iostream> using namespace std; class B { int val1,val2; public: void get() { cout<<"Enter two values:"; cin>>val1>>val2; } friend float mean(B ob); };
  • 5. 5 float mean(B ob) { return float(ob.val1+ob.val2)/2; } int main() { B obj; obj.get(); cout<<"n Mean value is : "<<mean(obj); return 0; }
  • 6. 6 #include <iostream> using namespace std; class ONE; class TWO { public: void print(ONE& x); }; class ONE { int a, b; friend voidTWO::print(ONE& x); public: void getdata() { cin>>a>>b; } };
  • 7. 7 voidTWO::print(ONE& x) { cout << "a is " << x.a << endl; cout << "b is " << x.b << endl; } int main() { ONE xobj; TWO yobj; xobj.getdata(); yobj.print(xobj); return 0; }
  • 8. Friend Class 8  Friend Class can access the private and protected members of the class for which it is friend ,it is done by a declaring prototype of this external function within the class, and with the keyword friend.  Friend class is used in C++ to gain access to protected members exclusively by the friend class
  • 9. 9 Example of Friend Class #include<iostream> using namespace std; class getdata { float num1,num2; public: void read() { cout<<"nnEnter the First Number : "; cin>>num1; cout<<"nnEnter the Second Number : "; cin>>num2; } friend class add; };
  • 10. 10 class add { public: float c; void add1(getdata G) { c=G.num1+G.num2; cout<<"nnSum="<<c; } }; int main() { int cont; getdata G1; add A; do { G1.read(); A.add1(G1); cout<<"nnDo you want to continue? (1-YES,0-NO)"; cin>>cont; }while(cont==1); return 0; }