Hello everyone.
So in this we do I want to discuss about friend functions.
OK so let us take an example suppose I have a class drug now inside the class drug.
I have some private public and protected properties so private.
Protected and public.
OK.
So let's say x is a private property.
Why is a predicted property and Z is a public property.
OK.
So the private property X it is not accessible outside the truck class.
Okay.
Similarly the protected property right.
It is accessible.
Our tender truck last but only with the help of derived classes and Z.
This property is open to all.
We can access it anywhere.
Okay.
Now suppose I have a class bus.
Now the bus class has a print function okay.
And this print function it wants to access the private property of the class.
Okay you want to access the X..
Okay.
So how can this print function can access the private property of an entire class.
Okay.
So the answer is with the help of friend functions.
Okay.
So with the help of a friend functions a class a class can access the private and the protected properties
of the order class.
Okay so with the help of friend functions.
A class can access the private in the protected property of the class because the public properties
are already accessible to everyone.
Okay.
So what we have to do.
So what we have to do we have to make this print function a friend of this class.
Okay.
Now we all know friends knows it don't.
Okay I want to access the secret off track.
So the print has to become the friend.
Okay.
Different knows the secret.
So when the brain function will become the friend of the class.
It can access the secret property it can access all the secrets of the track class.
Okay.
In order to see in gold
okay.
So I have a class drug now inside the gastric.
We have decided that I have some I have some private property I have some predicted property and I will
have some public property
okay.
Now I have a class bus class bus has a function
so class bus has a function now what this function wants this function wants to excess excess the X
of drug.
This function was to exist the private and the public properties private and the protected properties
of the track class.
Okay so how can we do this.
So the only ways what we will do we will declare this print function as a friend of the class.
Okay so what is the syntax syntax is very simple.
First of all we have to write friend
then we have to write void then bus scope resolution operator the name of the function print.
Okay so what will happen now.
Okay so now the print function of the bus class is now a friend of the class.
Okay the print function of the bus class is now a friend of the class.
So this print function now can access all the secrets of the class okay.
But if it will then this file will get an added okay that it will be at this lane.
Let's end if I look okay so what is the added
okay.
So this will be Z okay.
So it will then this file we will get an error at this line so what is their ETA is at this line.
Compiler doesn't know what this bus.
Okay.
If you will see that it so bus has not been declared.
So what happened in C++
Okay so we are getting a compilation error at this length.
So what happens in C++.
So in C++ the execution starts from the mean but the compiler should start from the very first line.
Okay.
So the completion will be from top to bottom.
So at this line line number well we do not have bus.
Okay buses not declared.
So compiler will not go at line number 15 it will not search at this land the compiler will stop and
it will tell us that it doesn't know what is bus.
Okay.
So what we have to do we have to declare we have to write this code in front of before the class okay
above the track class so let's move the whole code
okay.
So listen this file
so at this time we are not getting any added.
Okay now one more thing that we have to we have to be careful.
So now this function is a friend of the track class.
So we cannot give the definition here.
Okay.
We cannot give the definition we have to give the definition outside the class.
Okay.
So I am given the definition here.
So for giving the definition the structure will be void bus scope resolution operator print okay.
So now in this function.
So now here within this function I can access all the secrets of the class private and protected properties.
Okay.
X Y and Z all are accessible.
Okay.
So for accessing the private properties and the protected properties first of all we will create an
object.
So correctly so I can access X..
Let's say existing I can access y.
So do not y as let's say 20 and let us also print the value of x and y so sealed do not X and do not
v
okay.
Now let us call this function brain function.
So I am creating an object must be and will call the function b dot print
okay.
Now listen this file
so I would produce coming out with 10 and 20.
Okay so I will code is working fine.
So our code is working fine okay so let's do one more thing.
Suppose I have a global function okay.
So I have a normal global function.
Let's say the name of the function is best okay.
Now this test function
this test function also wants to access the private and the predicted this track.
It also wants to access the private property of the class.
Okay.
So what we have to do we will do the same thing.
We will make this test function as the friend of the track class.
Okay.
So this index will be quite simple.
We will write friend then void and then test.
Okay.
So again if you will then this file we will get the compilation error at this line because this test
function does not belong to a class because we have written friend in before the test function.
So this test function doesn't belong to drug class and when we will reach line number it Dean we will
get compilation error saying that best is not defined okay because test is written below the track class.
Okay so what we have to do we have to move this code above.
Okay so what we can do we will write the declaration.
So this is the declaration okay.
And here we can write the definition.
So now we will not get completion at it.
Okay so.
Okay.
So let's copy this code only
okay.
So now let us call this function test from the mean.
Okay so test now let's define
Okay so 10 20 and again 10 20.
So the first one is due to by Sprint function.
The second one is due to the global function which is the friend of the class.
Okay.
So one thing to note here is this test and this print function.
They are not the part of the class.
Okay.
They are not the part of the class.
So.
So these two functions are not part of class.
So if I declare another function if I defined another function here let's have white display and inside
the display function.
So can I write C out this a can I write this.
So is this line correct.
Obviously this is correct.
So what is the meaning of this.
This may contain the address of the current object so I can access the private property inside the class.
So this is correct.
This implicitly means C out arrow x c out x.
Okay so this implicitly means this only.
Okay.
So what is the point I want to make here is.
Friend function do not have access to this point.
Okay so I am writing some points here.
Okay.
So first of all friend function do not have access to this point that way because friend functions are
not the part of the class.
Okay.
Friend function are not the part of the class.
So you do not have access to this pointer okay.
Now the second point is we can put the friend function under any excess modifier.
Okay so we can put these friend functions under any excess modifier.
There will be no change okay.
No effect.
So the point is the excess modifiers do not work for friend function.
Okay so I am writing here.
Access modify it
does not look for a friend function Y because the excess modified defined the accessibility of the members
of the class and friend functions are not part of the class since friend function not the part of the
class.
Then access modify it cannot be used for friend function.
So what I want to say here is so here the friend functions are in that the public we can move it to
protected and we can also move it to private okay.
There will be no changes okay.
Now what I want is so we have a bus class inside the bus class.
Let's say we have many functions so print one function similarly we have print to function print three
functions print for function and so on.
Okay so inside the bus class.
Suppose we have many functions okay.
And all these functions want to access the secrets of the class.
Okay.
All these functions wants to access the private and the protected properties of the upper class.
So there are two ways to do this.
So the first raise what we will do we will make all these functions as friend a function of their track
class.
Okay so this is the first Ruby.
The second way the shortcut is what we will do.
We will make the bus class as a friend of class.
Okay we will make bus class.
We will declare bus class as a friend of class.
So the syntax is very simple
okay.
So let's combine these two lines.
So what I want to do I want to make I want to make I want to declare I want to declare bus as a friend
after class.
So the syntax is very simple friend class bus okay.
So what will happen with the help of this line.
So with the help of this line what I want to say here is now the class bus is a friend of class.
Okay so all the functions all the all the member function of the bus class can access all the properties
of the truck class okay.
Every member function of the bus class is now a friend of the truck class.
Okay so these type of classes these type of classes are called Friend classes.
Okay so these type of classes are called Friend classes so we know what our friend classes.
Okay.
Now the fourth point is the friendship is one way so we have one with friendship.
So what is the meaning of when we.
Friendship.
So what is the meaning of a new friendship.
So class drug has declared bus as his friend.
So bus can access all the properties of the track class.
Okay but divisive outside is not true.
Okay bus can access all the properties of the truck class because truck has declared bus as its friend
but the vice MSA is not true.
Truck cannot access the private and the predicted properties of the bus class.
Okay because bus has not declared Brooke as its friend.
Now okay because bus has not declared drug as a stranger yet.
Okay so the friendship is one where friendship okay.
So when we friendship and we know what is a friend plus excess money if I will not vote for a friend
glasses and Frank glasses do not have access to this point.
Okay.
Friend functions do not have access to this point.
Okay so I hope everything is cleared.
Thank you.