KEMBAR78
Friend Functions and Friend Classes-2018 | PDF | Class (Computer Programming) | C++
0% found this document useful (0 votes)
3 views6 pages

Friend Functions and Friend Classes-2018

Uploaded by

Emmanuel Mrigo
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)
3 views6 pages

Friend Functions and Friend Classes-2018

Uploaded by

Emmanuel Mrigo
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/ 6

FRIEND FUNCTIONS AND FRIEND CLASSES

References:
1. Pratta S., “C++ Primer Plus”, 6th Edition, 2012, (Adison-Wesley) – Chapter 15, pp877-
889.
2. Dietel P. & Dietel H.,”C++ How to Program”, 8th Edition, 2012, (Prentice Hall) –
Chapter 10.4
3. Malik D.S., “C++ Programming”, 4th Edition, 2011, (Cengane Learning) – Chap.
4. Bjarne Stroustrup, “C++ Programming Language”, 4th Edition 2013, (Adison-Wesley)
– Chapter 16.3.2.
5. Android Apps: = Learn C/C++ chapter 10 & Microleaning of C++ chapter 9.5 & 9.6.

FRIEND FUNCTIONS
All chapters before this chapter, we’ve been preaching the virtues of keeping your data private.
However, you may occasionally find situations where you will find you have classes
and functions (non-class members) that need to work very closely together. For example, you
might have a class that stores data and a function (or another class) that displays the data on the
screen. Although the storage class and display code have been separated for easier maintenance,
the display code is really intimately tied to the details of the storage class. The friend functions can
serve, for example, to conduct operations between two different classes, and provide more efficient
access to data members than the function call.

Normally, the private and protected data members of a class can be accessed only by member
functions of that class. But, there is one exception. A friend function will be friendly with a class
even though it is not a member of that class and can be granted an access to private and protected
members of the class. A friend function is declared by the class that is granting access, so friend
functions are part of the class interface, like class methods.

We can define a function or a class as a friend of another one class, granting that function or
second class access to protected and private members of the one class. We make a function a
friend to a class by declaring a prototype of this external function within the class definition, and
by preceding it with the keyword friend in the declaring class;

Syntax: - friend returnType functionName (ClassObj & );

Friend member functions must be created first ((for non-class members) or (declared in their class
of origin)) before being made friends to a declaring class. Failure to do so may result to an error.

//Friend function that is Non-member of any class


#include <iostream>
using namespace std;

class Rectangle
{
int width, height; //class data members' declaration
public:
Rectangle(int w = 1, int h = 1)
{
width=w; height=h;
} //end of definition of parameterized constructor
friend void display(Rectangle& ); //argument is the reference of a class object
}; //end of class definition

void display(Rectangle& r)
{
cout <<"Rectangle Area: "<< r.width * r.height << endl;
} //end of class friend function definition

int main ()
{
Rectangle rect(5,10); //parameterized constructor called explicitly
display(rect); //friend function called as regular function, not through object
return 0;
}

The friend function display(rect) has an access to both private and protected members of the
Rectangle class object though it's not a class member function. It gets the width and height using
dot operator: r.width and r.height. If we do this inside main, we get an error because they are
private members and we can't access them outside of the class. But friend function to the class can
access the private members.

But what's the point of friend functions. In the above example, we could have made "display" as a
member function of the class instead of declaring it as a friend function to the class.

One friend function can be friendly to two or more classes. The friend function does not belong
to any class, but it can be used to access private data members of two or more classes as in the
following example.

//Non-Member function friend to two classes


#include <iostream>
using namespace std;

class Square; //class Square forward declaration or partial declaration as notice to compiler

class Rectangle
{
int width, height; // declaration of class private members
public:
Rectangle(int w = 1, int h = 1)
{
width=w;height=h;
}
friend void display(Rectangle&, Square&);//Rectangle& and Square& are obj references
};

class Square //Square class full definition


{
int side;

public:
Square(int s = 1)
{
side=s;
}
friend void display(Rectangle&, Square&); //Declration of friendship
};

void display(Rectangle &r, Square &s)


{
cout << "Rectangle: " << r.width * r.height << endl;
cout << "Square: " << s.side * s.side << endl;
}

int main ()
{
Rectangle rec(5,10);
Square sq(5);
display(rec, sq);//Objects are arguments
return 0;
}

Output is:
Rectangle: 50
Square: 25

Generally, the use of friend functions is out of an object-oriented programming methodology, so


whenever possible it is better to use members of the same class to perform operations with them
as in the following example getting exactly same output.

#include <iostream>
using namespace std;

class Rectangle
{
int width, height;

public:
Rectangle(int w = 1, int h = 1):width(w),height(h){}
void display() {
cout << "Rectangle: " << width * height << endl;
};
};

class Square
{
int side;

public:
Square(int s = 1):side(s){}
void display()
{
cout << "Square: " << side * side << endl;
};
};

int main ()
{
Rectangle rec(5,10);
Square sq(5);
rec.display();
sq.display();
return 0;
}

Friend-Rules
1. Friend Function is not in the scope of the class to which it has been declared as friend
2. It cannot be called using the object of the class.
3. Invoked (called) like normal function.
4. It cannot access members name directly and has to use an object name and dot
membership operator with each member name. e.g. (A . x);
5. Can be declared in either public or private attribute section.
6. Usually it has the objects as arguments.
7. Usually used in operator overloading.
8. Member function of one class can be friend functions of another class.

FRIEND CLASSES
As some programmers say, non-class member friend function seems to be contrary to the concept
of data hiding, encapsulation and OOP concept as a whole, so to address this challenge; we can
use member function of one class to be friend to another class. this is more safe way of designing
a friend function.

Just like functions are made friends of classes, we can also make one class to be a friend of another
class. Then, the friend class will have access to all members of the other class (private, protected
and public).

Syntax:
class z
{
…..
friend class x; // all members of x are friends to Z and can access protected and private data
}

#include <iostream>
using namespace std;

class Square;//declaration

class Rectangle
{
int width, height;

public:
Rectangle(int w = 1, int h = 1):width(w),height(h){}
void display()
{
cout << "Rectangle: " << width * height << endl;
};
void morph(Square &);
};

class Square
{
int side;

public:
Square(int s = 1):side(s){}
void display()
{
cout << "Square: " << side * side << endl;
};
friend class Rectangle;// all members of Rectangle are friends to Square
};//End of class Square definition
void Rectangle::morph(Square &s)
{
width = s.side;
height = s.side;
}

int main ()
{
Rectangle rec (5,10);
Square sq (5);
cout << "Before:" << endl;
rec.display();
sq.display();
rec.morph(sq);
cout << "\nAfter:" << endl;
rec.display();
sq.display();
return 0;
}

We declared Rectangle as a friend of Square so that Rectangle member functions could have
access to the private member, Square::side
In our example, Rectangle is considered as a friend class by Square but Rectangle does not
consider Square to be a friend, so Rectangle can access the private members of Square but not
the other way around.

Summary:
1. It allows all the functions of one class as the friend of another class.
2. Friendship is not mutual: - If A is a friend of B, then B is not a friend of A.
3. Friendship is not inferred: - If A is a friend of B and B is a friend of C, then C is neither a
friend of B nor a friend of A.
4. Friendship never inherited: - If A is a friend of B and C is inherited class of B, then A will
never be a friend of C.

DISADVANTAGES OF FRIENDSHIP
Despite the advantages of friendship seen before, there are two challenges when dealing with
friend functions and friend classes, these are;
1. Violation of the notion of data hiding, since a friend function is not a member function
but is allowed to access data members the same way member functions do.
2. Again it violates the whole concept of encapsulation. Encapsulation does not include non-
member functions
Whenever friend functions and friend classes are designed, a great care has to be taken or there
will be no need of classes (a class will have no meaning), and the whole concept of OOP will fail.

You might also like