0% found this document useful (0 votes)
33 views14 pages

Friend Functions & Friend Classes: Object Oriented Programming Spring 2020 Imran Siddiqi

Friend functions allow a function to access private and public members of a class that it is not a member of. Friend classes allow all member functions of one class to access private members of another class. Friendship is granted explicitly and is not symmetric or transitive.

Uploaded by

Muhammad Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views14 pages

Friend Functions & Friend Classes: Object Oriented Programming Spring 2020 Imran Siddiqi

Friend functions allow a function to access private and public members of a class that it is not a member of. Friend classes allow all member functions of one class to access private members of another class. Friendship is granted explicitly and is not symmetric or transitive.

Uploaded by

Muhammad Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Friend Functions & Friend Classes

Object Oriented Programming


Spring 2020
Imran Siddiqi

1
Friend 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
Example of friend functions
class alpha
{
private:
int data;
public:
alpha():data(3) {}
friend int frifunc(alpha); //friend func declaration
};

int frifunc(alpha a) //friend func defined


{
cout << a.data;
}
void main()
{
alpha aa;
frifunc(aa); //friend func called
getch();
}
Example of friend functions
class beta; //req for frifunc declaration
class alpha
{
private:
int data;
public:
alpha():data(3) {}
friend int frifunc(alpha, beta);
};
class beta
{
private:
int data;
public:
beta():data(7) {}
friend int frifunc(alpha,beta);
};
Example of friend functions
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();
}
friend classes

• 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
friend classes

class beta;
class alpha
{
private:
int data;
public:
alpha():data(3) {}
friend beta; //friend class declaration
};
class beta
{
public:
void func(alpha a)
{
cout<<"alpha's data: "<<a.data;
}
};
friend classes

void main()
{
alpha aa;
beta bb;
bb.func(aa);
getch();
}
friend classes
class Triangle;
class Point
{
private:
int x, y;
public:
Point() :x(0), y(0)
{}
Point(int u, int v)
{ x = u; y = v;}
void print()
{cout << "(" << x << ", " << y << ")" << endl;}

friend class Triangle;


}; 9
class Triangle {
Point p1, p2, p3;
public:
Triangle() :p1(0, 0), p2(0, 0), p3(0, 0)
{}
Triangle(Point varp1, Point varp2, Point varp3) {
p1 = varp1;
p2 = varp2;
p3 = varp3;
}
float Area()
{
return (1.0/2 * (p1.x*(p2.y - p3.y) + p2.x*(p3.y -
p1.y) + p3.x*(p1.y - p2.y)));
}

10
Class member functions as friends
class Triangle;
class Point
{
private:
int x, y;
public:
Point() :x(0), y(0)
{}
Point(int u, int v)
{ x = u; y = v;}
void print()
{cout << "(" << x << ", " << y << ")" << endl;}

friend float Triangle::Area();


11
};
class Triangle {
Point p1, p2, p3;
public:
Triangle() :p1(0, 0), p2(0, 0), p3(0, 0)
{}
Triangle(Point varp1, Point varp2, Point varp3) {
p1 = varp1;
p2 = varp2;
p3 = varp3;
}
float Area()
{
return (1.0/2 * (p1.x*(p2.y - p3.y) + p2.x*(p3.y -
p1.y) + p3.x*(p1.y - p2.y)));
}

12
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
Summary (friends)
class T {
public: Global function a() can access private
friend void a(); data of class T
int m();
private: m() can access private data in S
...
}; All functions of T can access private
data of class X
void a() {...}

class S {
public:
friend int T::m();
...
};
class X {
public:
friend class T;
...
};

You might also like