Friend Function in C++

Friend Function in C++


Friend Function
Welcome to C++ Programming. In this article, we will talk about friend Function and friend Class trying to understand what do they mean and why are they useful in C++ design process. These will be the module outline we will take examples of Matrix Vector Multiplication and Linked List, and finally will end with somenotes. As you know as usual the outline will be available on the left of your slide So, let us first introduce the basic notion of friend function. On the left you have the view of ordinary function. So the situation is like this, that I have a class myclass which as some private data, it has a constructor, and it has; I am sorry if just ignore this line. I have this function written outside of this class which is trying to do something with the class. What is it doing? It takes an object of the class by reference parameter call by reference, and then it tries to print the data element component of that objective. Now, what do we know, this is private therefore, if I have a function outside. I do not have the rights to access this object. So, just consider this is not there. This will give rise to an error and you will get an error of this kind cannot access the private member declared in myclass, because the reason you know very well by now is this is a global function and this is a private data so you cannot access this data directly.

Now, let us look into the right hand side.In the right hand side, we have exactly the same code except that we have introduced this in the class, much in the way we define the member function but with the difference that this is this inclusion is prefixed by a keyword friend. Now this does not make display a member function of the class, display is not a member function of the class, it continues to bea global function. A special type of a global function which is friend of myclass, but with this included when I say that this global function display which takes an object a of myclass by reference and returns void, when I say that that is a friend of myclass then this does not remain an error this error disappears, this is now allowed. So the concept is like this as if you know in our home also if somebody would step in we would normally allow them only to the drawing room, which is kind of the public place and we will not allow them to the inner rooms which is our private space.

But if I have a friend stepping in then I will probably not keep him waiting in the drawing room rather I will take him to my inner rooms my bed room or kitchen and so on. So it is like a similar concept. So here, he has saying that this function the myclass is saying that the display function which is outside of this class which is not a member function is still a friend and therefore, the private data of this class will be exposed to this member. That is a basic idea of a friend function and therefore it will generate like this will give a compilation error but this will be a right, output will be produced. With this let us see a formal definition of what a friend function is, “A friend function of a class has access to the private and protected members of the class.” That is it can break the encapsulation which have still not discussed what are protected members but, hold that for a while when we discussed that this will become clear but they are pretty much like that private members only. So it must have a prototype included in the scope of the class and must be prefixed with friend, we have just in the example of the display function as to how do you write the signature of that function within the class with a friend in the front to say that this particular function is a friend of this class. 

Now this function does not belong to the class is not a member function therefore it is name is not qualified by the class name as we do for normal non friend member functions. It is not called with invoking an object because it is not a part of the class. A particular member function or a particular function can be friend of more than one class. Now, if you look at what all can be friend function; any global function could be a friend of a class, any member function of a different class could also be a friend of a class, or a function template could be a friend function.Now, function template again you do not know, but when you come to that you will understand how that works. With that, now let me introduce a more concrete problem. Here the problem is I have two classes, I have a vector class which keeps a linear vector of a size n, and have a matrix class which keeps a square matrix, not necessarily square matrix it is a keeps a matrix of m by n dimension. So, this is constructor forthe vector just to keep things simple so that I do not have to enter the values of the vector here I have set as if the constructor itself set certain values. Similarly, for the constructor of matrix as if when he construct the values are automatically initialized. I mean what values exist in that matrix or vector is not for our interest.

What is of our interest is? Now given a vector object and a matrix object I want to define a function which will be able to compute the product of them. So I want a function like this. A function prod which takes a matrix object here, I have taken it by a pointer takes a vector object multiplies them, of course following the rules of matrix vector multiplication and gives me the result which will of course in this case be a vector. What you see here is, this particular function prod and this is implementation I am not going through the implementation you can check it at a later point of time this is a typical matrix vector multiplication cout. 

Comments

Popular posts from this blog

Data Member and Static Keyword in C++

Use Of CPP