









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Page 1 of 15
Today, we are going to discuss a very interesting subject i.e. Friend Functions. We will see what is the relationship of friendship with our object-based programming. Before going into details of the subject, it is better to have a fresh look on the definition of „class‟. „Class is a user defined data type‟. The „class‟ provides encapsulation facility to the programmer. We can gather data at some place and some function that manipulates that data. In the previous lecture, two keywords, „private‟ and „public‟ were introduced. We define data members as „private‟ that are visible only from inside the class and hidden from the outside. However, „public data member functions‟ is the interface of the class available for outside world. Objects are accessed by these functions that can manipulate the private data of the class. We cannot access the private data of the class directly. This concept of data encapsulation and data hiding is very important concept in software engineering. It allows us to separate the interface from the implementation of the class i.e. we can hide how we have done the task and make visible what to do. It is critically important for large and complex systems. Sometimes, a need may arise to access the private data of the class from outside.
Let‟s talk about the concept of friendship. What you see on the screen during the lecture is the picture of the instructor. This is the public interface. That is all you know. What is inside his mind you never know. It is all „private‟. The instructor has access to his own mind and feelings. But you do not have access to that. Do you know any human being who has access to your mind and feelings? What we call that human being. He is known as friend. Normally other people don‟t know about our thoughts. Only friends know about it. Friends have access to the inner thoughts and have inner knowledge of a friend. Can we apply this definition to objects?
The friend functions of a class have access to the private data members of class. Despite being a good thing, there is possibility of vulnerability. We are opening our thoughts, inside view for somebody else. Without having 100% trust, it will be risky to make our thoughts and feelings public. We want that our private data is accessible to someone outside, not public for everybody. Otherwise, the data encapsulation and data-hiding concept will be violated. We keep the data members private and declare some specific functions that are not member of the class but friend of the class. As friends, they have access to the inside data structure of the class despite not
Page 2 of 15
being members.
To declare a friend function, we can put it anywhere in the class. According to the definition of the friend functions, they have access to the private data members of the class. These can also access the private utility functions of the class. The question arises where we should put the friend function whether in the private or public part of the class. Be sure that friend is a very strong statement. It is too strong to be affected by
be always outside the class. However, the prototype of the function will be written in the class. We use the keyword ‘friend’ before the prototype of the function.
friend return_type friend_function_name(int, char);
If we have a class, suppose „Date‟ and want to declare a friend function of this class. In the definition of the class, we will write the friend function‟s prototype with the keyword „friend‟. To access the private data, friend function will need the object. Therefore, usually in the parameter list of friend function, we provide the object of that class. Normally, the programmers work this way. As the friend function is not affected by the private or public keyword, so we can declare it anywhere inside the class definition. Programmers generally declare the friend functions at the top of the class definition. So, the friend functions are declared at the start of the class definition, followed by the private data and public data. This is a guideline. You can develop your own style. We normally make a header file of the class definition and implementation in the other file. The member functions are defined in the implementation file and compiled to get an object file. We declare the friend function in the class definition that is in the header file.
Let‟s go back to the definition of the friendship. I can declare you my friend and tell you about my inner thoughts and feelings. But it does not work both ways. In other words, friendship is granted, never taken. So, a class can declare a friend function and someone from outside the class cannot declare itself friend of a class. This is also an important concept. If someone from outside the class can declare itself friend of the class, then by definition that external function would have access to the private data member of the class. But this will negate the concept of the encapsulation and data hiding. It does not work this way. A function cannot declare itself friend of a class. Rather, a class has to declare itself that a function is friend of the class or not. So the class declares a friend function. These functions can not declare themselves friend of a class from outside. Once, the friend functions are declared and the class is compiled, no one from outside cannot make his function friend of your class. Outside functions can only view the interface of the class.
Let‟s summaries this concept. Friend functions are not member functions of the class. The class itself declares the friend functions. The prototype of friend functions is written in
Page 4 of 15
#include <iostream.h>
class myClass { friend void increment(myClass *, int);
private: int topSecret;
public: void display() { cout << "\n The value of the topSecret is " << topSecret; } myClass(); }; // constructor of the class myClass::myClass() { topSecret = 100; }
// Friend function definition void increment(myClass *a, int i) { a->topSecret += i; // Modify private data }
// showing the use of the friend function void main() { myClass x; x.display(); increment(&x, 10); x.display(); }
The output of the program is: The value of the topSecret is 100 The value of the topSecret is 110
Let‟s consider some complex example. We have two classes- myClass1 and myClass. Both classes have one private data member of type int i.e. int topSecret; Now we want to add the values of private data members of both the classes and display it on the screen. topSecret is a private data member of both the classes. One class can not see inside the other class. myClass1 and myClass2 are both separate classes. We need a function sitting
Page 5 of 15
outside the classes but can access the private data members of both the classes. Let‟s call the function as addBoth. This function will add the value of topSecret of myClass1 to topSecret of myClass2 and display the result on the screen. We need a function that can look inside both classes i.e. friend of both classes. We know that classes have to declare a function as friend.
The arguments of addBoth function will contain myClass1 and myClass2. In the definition of the myClass1, we will write the prototype of addBoth function as:
friend void addBoth(myClass1, myClass2);
Can we write this line in the definition of the myClass1? We know that if we refer some function as f(x) and the function f() is not defined or declared before this, the compiler will give an error that function f() is not defined. So we at least declare the function before main() so that compiler successfully compile the program. So there was declaration of the function before its being called. Now same problem is in our friend function prototype. We are referring both classes in it and our program does not know anything about myClass2. We can tackle this problem by writing a line before the definition of the class myClass1 as:
class myClass2;
It will declare that myClass2 is a class having its definition somewhere else. It is the same as we declare functions before main. After writing that statement, we can refer myClass in our code. The definition of the class myClass1 will be as:
class myClass { private: int topSecret;
public: void display() { cout << "\nThe value of the topSecret is " << topSecret; } myClass1();
friend void addBoth(myClass1, myClass2); };
myClass1::myClass1() { topSecret = 100; }
The definition of myClass2 is also similar to myClass.
Page 7 of 15
This is an interesting function. Despite not being the member of any class, it can access the data of both the classes. This function is friend of both the classes.
Here is the complete code of the program. /* A sample program showing the use of friend function, which access the private data members of two classes. */
#include <iostream.h>
class myClass2; // declaring the class for the friend function in myClass
// definition of the myClass class myClass { // private data members. Hidden private: int topSecret;
// interface of the class public: void display() { cout << "\nThe value of the topSecret is " << topSecret; } myClass1();
// friend function friend void addBoth(myClass1, myClass2); }; // definition of the constructor. MyClass1::myClass1() { topSecret = 100; }
// Definition of the myClass class myClass { // private data members. Hidden private: int topSecret;
// interface of the class public:
Page 8 of 15
void display() { cout << "\nThe value of the topSecret is " << topSecret; } myClass2();
// friend function friend void addBoth(myClass1, myClass2); };
// definition of the constructor. MyClass2::myClass2() { topSecret = 200; }
// The definition of the friend function which is adding the topSecret data member of both the classes.
Void addBoth(myClass1 a, myClass2 b) { cout << "\nThe value of topSecret in the myClass1 object is " << a.topSecret; cout << "\nThe value of topSecret in the myClass2 object is " << b.topSecret; cout << "\nThe sum of values of topSecret in myClass1 and myClass2 is " << a.topSecret + b.topSecret; }
// main program void main() { // declaring the objects and displaying the values myClass1 a; myClass2 b; a.display(); b.display(); // calling friend function and passing the objects of both the classes addBoth(a, b); }
The output of the program is;
The value of the topSecret is 100 The value of the topSecret is 200 The value of topSecret in the myClass1 object is 100 The value of topSecret in the myClass2 object is 200 The sum of values of topSecret in myClass1 and myClass2 is 300
Page 10 of 15
private: float value ;
public: myClass2 ( ) { value = 100 ; }
// friend functions friend float addBoth ( myClass1 , myClass2 ) ; friend float subBoth ( myClass1 , myClass2 ) ; friend float mulBoth ( myClass1 , myClass2 ) ; friend float divBoth ( myClass1 , myClass2 ) ; };
void main ( ) { myClass1 myClass1Obj ; //create an object of class myClass myClass2 myClass2Obj ; //create an object of class myClass char choice; cout << "Please enter one of the operator +, -, /, * " << "followed by Enter " << endl; cin >> choice;
if ( choice == '+' ) { cout << "The sum is : " << addBoth(myClass1Obj , myClass2Obj) << endl; } else if ( choice == '-' ) { cout << "The difference is : " << subBoth(myClass1Obj , myClass2Obj) << endl; } else if ( choice == '*' ) { cout << "The multiplication is : " << mulBoth(myClass1Obj , myClass2Obj) << endl; } else if ( choice == '/' ) { cout << "The division is : " << divBoth(myClass1Obj , myClass2Obj) << endl; } else { cout << "Enter a valid choice next time. The program is terminating" << endl;
Page 11 of 15
system ( "PAUSE" ) ; }
float addBoth ( myClass1 object1 , myClass2 object2 ) { return ( object1.value + object2.value ) ; }
float subBoth ( myClass1 object1 , myClass2 object2 ) { return ( object1.value - object2.value ) ; }
float mulBoth ( myClass1 object1 , myClass2 object2 ) { return ( object1.value * object2.value ) ; }
float divBoth ( myClass1 object1 , myClass2 object2 ) { return ( object1.value / object2.value ) ; }
Following is the output of the program. Please enter one of the operator +, -, /, * followed by Enter
The multiplication is : 20000
We have seen that a class can define friend functions for itself. Similarly a class can be declared as a friend class of the other class. In that case, the function of a class gets complete access to the data members and functions of the other class. So it is an interesting expansion of the definition that not only the functions but also a class can be a friend of the other class. The syntax of declaring a friend class is that within the class definition, we write the keyword friend with the name of the class. It is going to be a friend class. i.e. friend class-name ; We can also write the word class after the keyword friend and before the class name as friend class class-name ;
Now let‟s take another example of a class. Suppose, we have classes ClassOne and OtherClass. We want to make OtherClass a friend class of the ClassOne. So we declare OtherClass a friend class in the definition of the ClassOne as following.
Page 13 of 15
declare its friend classes in its declaration and cannot be a friend of other classes by declaring itself their friend. In the above example, ClassOne declares that OtherClass is my friend class. So otherClass can access all the data members and methods (private, public or utility functions) of ClassOne. It does not (and cannot) declare that I ( ClassOne ) am a friend class of OtherClass. So ClassOne has no access to private data members and methods of OtherClass. It can access these only if OtherClass declares ClassOne as its friend. This means that by using the keyword friend, a class gives rights of accessing its data members and methods to other classes and does not get the rights to access other classes.
By declaring friend functions and classes, we negate the concept of data hiding and data encapsulation and show the internal structure of the class to the friends. But the good thing in it is that a class declares its friends while the other functions or classes cannot look inside the class. The disadvantage of friend classes is that if we declare such a relationship of friendship for two classes, this will become a pair of classes. To explain it we go back to the concept of separating the interface and implementation. In case of change in the implementation of ClassOne , the private data structure will also change. For example, at first we have an integer variable int i ; and later, we need two more variables and we write it as int j, k, l ; As the implementation of ClassOne has now changed, the functions of OtherClass that wanted to manipulate the members of ClassOne will not work now. It is critically important that friend classes should be declared very carefully. When is it necessary? This can be understood by an example from mathematics. We have straight line in math. The equation of straight line is: y = mx
class Quadratic { //some methods private: double a, b, c ; };
Page 14 of 15
Now we have two classes i.e. StraightLine and Quadratic. In a mathematical problem, when we have given a parabola (a quadratic equation) and a straight line (straight line equation) and are asked to find the point at which the straight line intersects the parabola. To solve it, we setup equations and solve them simultaneously and find out the result, which may be in three forms. Firstly, there is the line that does not intersect the parabola. The second is that it intersects the parabola at one point (i.e. it is a tangential line) and third may be that the line intersects the parabola at two points.
When we setup these equations, we come to know that here the constants m, c (of straight line), a, b and c of quadratic equation are being used. So from a programming perspective if we had an object l1 of type StraighLine and an object q1 of type quadratic. And wanted to find the intersection of l1 with q1. Now here is a situation where we need either a friend function of both classes, so that it can manipulate the data of both classes, or need to declare both classes as friend classes of each other and then write their methods to find the intersection. Similarly we can have many other examples in which a class may need to look into the other class. But it is not some thing to be done all the time. It should be done only when necessary. Use of friend functions is normally a better idea. Using friend classes means that both the classes are linked with each other. If the code in any one of the class is modified i.e. its implementation is changed, we have to recompile both the classes. Due to change in one class, the other class also needs to be changed, necessitating the compilation of both the classes.
So we have lost the principle of separating the interface from the implementation. Now let‟s talk about the limitations of this friendship business. Firstly, there is no transitive dependency in friend declarations. Suppose I say student A is my friend and being a friend he knows my thoughts and ideas. Now the student A says ” student B is my friend” i.e. student B knows thoughts and ideas of student A. Does it mean that student B is also my friend? Does student B knows my thoughts and ideas? The answer is no. As I have not declared student B a friend of mine, so he ( student B ) does not know about my thoughts and ideas. The same applies to the friend definition for classes. The friendship is not transitive. It is not like „A is a friend of B and B is a friend of C, therefore A is a friend of C„. It does not work. A has to specifically declare „B is my friend and C is my friend‟ to make B and C friends of him. There is no transitive dependency in friend declarations.
Secondly, I can declare you to be my friend. This means I have unveiled my thoughts and ideas to you. But I cannot get your thoughts and ideas unless you declare me a friend of yours. So there is no association, which means A saying B is my friend does not imply in any way that A is a friend of B. Here B is a friend of A. But B has to declare „A‟ its friend. Thus the friend keyword produces one-way relationship.
The concept of classes allows us to separate implementation from interface. A class is a user defined data type. In a class, we declare private data members and utility functions so that they cannot be access from outside. Similarly, we declare some parts of