OOP Lecture 7 09112022 071844pm
OOP Lecture 7 09112022 071844pm
PROGRAMMING
FRIEND
FUNCTIONS &
CLASSES
2
FRIEND FUNCTION
A non-member function
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
3
COMPOSITION
class Point {
private:
int x;
int y;
public:
Point(int a = 0, int b = 0) {
x = a; y = b;
}
void print() {
cout << x << endl;
cout << y << endl;
}
• Telling the program that this function is
friend int addXY(Point); friend of class Point
4 }; • It is NOT a member of class Point
FRIEND FUNCTION
int addXY(Point a) {
int ans = a.x + a.y;
return ans;
}
int main()
{
Point p(2, 4);
p.print();
cout << "Adding x and y:" << addXY(p) << endl;
return 0;
}
5
FRIEND FUNCTION
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);
};
6
FRIEND FUNCTION
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();
}
7
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
8
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;
}
};
9
FRIEND CLASSES
void main()
{
alpha aa;
beta bb;
bb.func(aa);
getch();
}
10
FRIEND MEMBER FUNCTIONS
class A;
class B{
public:
void display(A obj);
};
class A{
int x;
public:
A(int a) { x = a;}
friend void B::display(A);
};
11
FRIEND MEMBER FUNCTIONS
12
FRIEND CLASSES
class Rect;
class Point{
private:
int x; int y;
public:
Point(int x = 0, int y = 0){
this->x = x; this->y = y;
}
void printPoint(){
cout << "(" << x << "," << y << ")" << endl;
}
void setX(int x){ this->x = x; }
void setY(int y){ this->y = y; }
int getX(){ return x; }
int getY(){ return y; }
friend class Rect;
13 };
FRIEND CLASSES
class Rect{
private:
Point topLeft;
Point bottomRight;
public:
Rect(Point tl, Point br){
topLeft = tl;
bottomRight = br;
}
int area(){
int width = bottomRight.x - topLeft.y;
int height = topLeft.y - bottomRight.y;
return width * height;
}
14
};
EXAMPLE: TRIANGLE AND POINT CLASSES
15
EXAMPLE: TRIANGLE AND POINT 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;
};
16
EXAMPLE: TRIANGLE AND POINT CLASSES
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)));
}
};
17
SUMMARY
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
18