Lecture Note MA251 Aug 17
Lecture Note MA251 Aug 17
Tutorial 3
P. S. Mandal, IITG
Friend functions
• In principle, private and protected members of a
class cannot be accessed from outside the same
class in which they are declared. However, this
rule does not affect friends.
• If we want to declare an external function as
friend of a class, thus allowing this function to
have access to the private and protected
members of this class.
• Need to declare a prototype of this external
function within the class, and preceding it with
the keyword friend:
P. S. Mandal, IITG
Example: friend functions
#include<iostrem> int main () {
using namespace std;
CRectangle rect, rectb;
class CRactangel {
int x, y; rect.set_values (2,3);
public: rectb =
void set_values(int, int); duplicate(rect);
int area(){
cout << rectb.area();
return(x*y);
} return 0;
friend CRectangle duplicate(CRectangle); }
};
void CRactangel::set_values(int a, int b){
x = a; y = b;
▪ Notice that neither in the declaration of
}
duplicate() nor in its later use in main()
CRectangle duplicate(CRectangle rect_p){
CRectangle rect_m;
have considered duplicate a member of
rect_m.x = rect_p.x*2;
class CRectangle.
rect_m.y = rect_p.y*2; ▪ It simply has access to its private and
return(rect_m); protected members without being a
P. S. Mandal, IITGmember.
}
Example: Friend classes
#include <iostream> int main () {
using namespace std;
CSquare sqr;
class CSquare; // forward declaration CRectangle rect;
class CRectangle {
int x, y;
public: sqr.set_side(4);
int area () {return (x * y);} rect.convert(sqr);
void convert (CSquare a);
};
cout << rect.area();
class CSquare { return 0;
private: }
int side;
public:
void set_side (int a) {side=a;}
friend class CRectangle; // friend class
};
void CRectangle::convert (CSquare a) { Like friend function, we can also define a class
x = a.side; as friend of another one, yielding that first
y = a.side;
} class (CRectangle) access to the protected and
private members of the second (CSquare) one.
P. S. Mandal, IITG
Friendship is not transitive
• CRectangle is considered as a friend class by
CSquare, but CRectangle does not consider
CSquare to be a friend, so CRectangle can access
the protected and private members of CSquare
but not the reverse way.
• Of course, we could have declared also CSquare
as friend of CRectangle if we wanted to.
• Another property of friendships is that they are
not transitive: The friend of a friend is not
considered to be a friend unless explicitly
specified.
P. S. Mandal, IITG
Assignments to be done in class today
1. Program an array implementation of a dynamic stack with
changing size. Consider initial stack size is 2. When an
element is inserted in the stack which is “FULL”, the stack
capacity dynamically becomes double of the earlier size and
the element is inserted appropriately. In the same way, the
stack should release additional, unused memory that it might
have acquired dynamically.
2. Given integer array of size n, then arrange the elements in
sorted order. To do so read one integer at a time and check if
it is already in the given array, using binary search.
P. S. Mandal, IITG