SlideShare a Scribd company logo
OBJECT ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING
Friends Classes and Functions
Friend functionsFriend functions
 A friend function of a class is defined outside that class's
scope, yet has the right to access the non-public (and
public) members of the class
 Let’s say you want a function to operate on objects of
two different classes and access their private data, you
would need to use a friend function
CPS235:Inheritance 2
Example ofExample of friendfriend functionsfunctions
class beta; //req for frifunc declaration
class alpha
{
private:
int data;
public:
alpha():data(3) {}
friend int frifunc(alpha, beta); //friend
func
declaration
};
class beta
{
private:
int data;
public:
beta():data(7) {}
friend int frifunc(alpha,beta);
//friend func
declaration
};CPS235:Inheritance 3
A class cannot be referred to
until it has been declared
Example ofExample of friendfriend functionsfunctions
int frifunc(alpha a, beta b) //friend func
defined
{
return (a.data + b.data);
}
void main()
{
alpha aa;
beta bb;
cout<<frifunc(aa,bb)<<endl; //friend func
called
getch();
}
CPS235:Inheritance 4
friendfriend classesclasses
 The member functions of a class can all be made friends
at the same time when you make the entire class a
friend
 If class alpha declares class beta as a friend class, then all
member functions of class beta can access private data
of class alpha
 It is also possible to declare only one member function
of another class to be a friend
CPS235:Inheritance 5
friendfriend classesclasses
class beta;
class alpha
{
private:
int data;
public:
alpha():data(3) {}
friend class beta; //friend class
declaration
};
class beta
{
public:
void func(alpha a)
{
cout<<"alpha's data:
"<<a.data;
}
};
CPS235:Inheritance 6
friendfriend classesclasses
void main()
{
alpha aa;
beta bb;
bb.func(aa);
getch();
}
CPS235:Inheritance 7
Class member functions asClass member functions as
friendsfriends
class CSquare;
class CRectangle {
int width, height;
public:
int area ()
{return (width * height);}
void convert (CSquare a);
};
class CSquare {
private:
int side;
public:
void set_side (int a)
{side=a;}
friend void CRectangle::convert(CSquare);
};
CPS235:Inheritance 8
Class member functions asClass member functions as
friendsfriends
void CRectangle::convert (CSquare a) {
width = a.side;
height = a.side;
}
int main () {
CSquare sqr;
CRectangle rect;
sqr.set_side(4);
rect.convert(sqr);
cout << rect.area();
getch();
return 0;
}
CPS235:Inheritance 9
Summary (friends)Summary (friends)
 Even though the prototypes for friend functions appear
in the class definition, friends are not member functions
 Friend declarations can be placed anywhere in a class
definition either in public, private or protected sections
 Violates the data hiding principle of classes, so it should
be avoided as much as possible
 Friendship is granted, not taken i.e., for class B to be a
friend of class A, class A must explicitly declare that
class B is its friend
 The friendship relation is neither symmetric nor
transitive
CPS235:Inheritance 10

More Related Content

PPTX
Friend function & friend class
PPTX
Data members and member functions
PPTX
Destructors
PPTX
virtual function
PPTX
classes and objects in C++
PPT
Data members and member functions
PPTX
Abstract class in c++
PPTX
Function overloading
Friend function & friend class
Data members and member functions
Destructors
virtual function
classes and objects in C++
Data members and member functions
Abstract class in c++
Function overloading

What's hot (20)

PPTX
Templates in C++
PPTX
Virtual base class
PDF
Access specifiers (Public Private Protected) C++
PPTX
07. Virtual Functions
PPTX
Functions in c
PPTX
Friend functions
PPTX
Inheritance in c++
PPT
friend function(c++)
PPTX
This pointer
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
Polymorphism In c++
PDF
Operator overloading
PPTX
[OOP - Lec 08] Encapsulation (Information Hiding)
PPTX
Member Function in C++
PDF
PPTX
[OOP - Lec 07] Access Specifiers
PPTX
Inheritance in java
PPT
C++ classes tutorials
PPT
Function overloading(c++)
PPTX
Inheritance in c++
Templates in C++
Virtual base class
Access specifiers (Public Private Protected) C++
07. Virtual Functions
Functions in c
Friend functions
Inheritance in c++
friend function(c++)
This pointer
[OOP - Lec 19] Static Member Functions
Polymorphism In c++
Operator overloading
[OOP - Lec 08] Encapsulation (Information Hiding)
Member Function in C++
[OOP - Lec 07] Access Specifiers
Inheritance in java
C++ classes tutorials
Function overloading(c++)
Inheritance in c++
Ad

Viewers also liked (18)

PDF
Chapter23 friend-function-friend-class
PPT
Friend function 6
PPTX
Inline Functions and Default arguments
PPTX
Inline function in C++
PPTX
Inline function
PPT
видеоэкскурсия в красный берег(полная)
ODP
Patient safety program scd
PPTX
Woolley final presentation
PPTX
Cr chap 4
PPTX
Услуги стадиона «Динамо»
PPTX
Foodcombining visuals
PPTX
Reliable Drupal Partner
PDF
PPTX
Doing things with manuscripts: CLIR post doc-seminar, Bryn Mawr College 7.31....
PPTX
Our encounter with d8
DOCX
Classroom Management
PPT
Nota tajuk 4- Fungsi RBT
Chapter23 friend-function-friend-class
Friend function 6
Inline Functions and Default arguments
Inline function in C++
Inline function
видеоэкскурсия в красный берег(полная)
Patient safety program scd
Woolley final presentation
Cr chap 4
Услуги стадиона «Динамо»
Foodcombining visuals
Reliable Drupal Partner
Doing things with manuscripts: CLIR post doc-seminar, Bryn Mawr College 7.31....
Our encounter with d8
Classroom Management
Nota tajuk 4- Fungsi RBT
Ad

Similar to Friends function and_classes (20)

PPT
12-170211073924.ppthihello heoobchdhdhdhhd
PPTX
cse l 5.pptx
PPTX
Friend function
PPTX
friend class & friend function by Komal V Rokade.pptx
PPTX
friend_derivedclasspresentation1234.pptx
PPTX
L9_Friend Function.pptxyeyeyeyeyyeyeyeye
PDF
Friend function in c++
PPTX
Friend Function
PPTX
OOP Week 3 Lecture 2.pptx
PPTX
Friend_Function_in_CPP its use and examples.
PPTX
Inheritance, friend function, virtual function, polymorphism
PPTX
+2 CS class and objects
PPTX
Friend function in c++
PDF
22 scheme OOPs with C++ BCS306B_module1.pdf
PPTX
Study of Friend Function in C++ with examples
PDF
Lec_15_OOP_ObjectsAsParametersFriendClasses.pdf
PPT
static member and static member fumctions.ppt
PPTX
424570636-friend-function-1-pptx.hiipptx
PPT
Classes in C++ computer language presentation.ppt
PPTX
Friend function
12-170211073924.ppthihello heoobchdhdhdhhd
cse l 5.pptx
Friend function
friend class & friend function by Komal V Rokade.pptx
friend_derivedclasspresentation1234.pptx
L9_Friend Function.pptxyeyeyeyeyyeyeyeye
Friend function in c++
Friend Function
OOP Week 3 Lecture 2.pptx
Friend_Function_in_CPP its use and examples.
Inheritance, friend function, virtual function, polymorphism
+2 CS class and objects
Friend function in c++
22 scheme OOPs with C++ BCS306B_module1.pdf
Study of Friend Function in C++ with examples
Lec_15_OOP_ObjectsAsParametersFriendClasses.pdf
static member and static member fumctions.ppt
424570636-friend-function-1-pptx.hiipptx
Classes in C++ computer language presentation.ppt
Friend function

Recently uploaded (20)

PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Cell Structure & Organelles in detailed.
PDF
Complications of Minimal Access Surgery at WLH
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Pre independence Education in Inndia.pdf
PPTX
Institutional Correction lecture only . . .
PDF
RMMM.pdf make it easy to upload and study
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Cell Structure & Organelles in detailed.
Complications of Minimal Access Surgery at WLH
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Pre independence Education in Inndia.pdf
Institutional Correction lecture only . . .
RMMM.pdf make it easy to upload and study
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Insiders guide to clinical Medicine.pdf
Week 4 Term 3 Study Techniques revisited.pptx
Pharma ospi slides which help in ospi learning
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Final Presentation General Medicine 03-08-2024.pptx
O7-L3 Supply Chain Operations - ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Supply Chain Operations Speaking Notes -ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
human mycosis Human fungal infections are called human mycosis..pptx

Friends function and_classes

  • 1. OBJECT ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING Friends Classes and Functions
  • 2. Friend functionsFriend functions  A friend function of a class is defined outside that class's scope, yet has the right to access the non-public (and public) members of the class  Let’s say you want a function to operate on objects of two different classes and access their private data, you would need to use a friend function CPS235:Inheritance 2
  • 3. Example ofExample of friendfriend functionsfunctions class beta; //req for frifunc declaration class alpha { private: int data; public: alpha():data(3) {} friend int frifunc(alpha, beta); //friend func declaration }; class beta { private: int data; public: beta():data(7) {} friend int frifunc(alpha,beta); //friend func declaration };CPS235:Inheritance 3 A class cannot be referred to until it has been declared
  • 4. Example ofExample of friendfriend functionsfunctions int frifunc(alpha a, beta b) //friend func defined { return (a.data + b.data); } void main() { alpha aa; beta bb; cout<<frifunc(aa,bb)<<endl; //friend func called getch(); } CPS235:Inheritance 4
  • 5. friendfriend classesclasses  The member functions of a class can all be made friends at the same time when you make the entire class a friend  If class alpha declares class beta as a friend class, then all member functions of class beta can access private data of class alpha  It is also possible to declare only one member function of another class to be a friend CPS235:Inheritance 5
  • 6. friendfriend classesclasses class beta; class alpha { private: int data; public: alpha():data(3) {} friend class beta; //friend class declaration }; class beta { public: void func(alpha a) { cout<<"alpha's data: "<<a.data; } }; CPS235:Inheritance 6
  • 7. friendfriend classesclasses void main() { alpha aa; beta bb; bb.func(aa); getch(); } CPS235:Inheritance 7
  • 8. Class member functions asClass member functions as friendsfriends class CSquare; class CRectangle { int width, height; public: int area () {return (width * height);} void convert (CSquare a); }; class CSquare { private: int side; public: void set_side (int a) {side=a;} friend void CRectangle::convert(CSquare); }; CPS235:Inheritance 8
  • 9. Class member functions asClass member functions as friendsfriends void CRectangle::convert (CSquare a) { width = a.side; height = a.side; } int main () { CSquare sqr; CRectangle rect; sqr.set_side(4); rect.convert(sqr); cout << rect.area(); getch(); return 0; } CPS235:Inheritance 9
  • 10. Summary (friends)Summary (friends)  Even though the prototypes for friend functions appear in the class definition, friends are not member functions  Friend declarations can be placed anywhere in a class definition either in public, private or protected sections  Violates the data hiding principle of classes, so it should be avoided as much as possible  Friendship is granted, not taken i.e., for class B to be a friend of class A, class A must explicitly declare that class B is its friend  The friendship relation is neither symmetric nor transitive CPS235:Inheritance 10