Hello, welcome to the sixth tutorial in the c++ programming series and in this part we'll be looking at different class. A first class is very similar to a friend function which we covered in the previous tutorial. It allows a class to access another classic property and protected variable that method. So let's go ahead and open up our project. So you can see also we can show you how to implement a frame class or news create a class called class one. Obviously, in your own code, you bear a naming convention that I am using in crypto method called well method.
And he is going to take a class to object which is going to be another class which we're going to implement in a moment in B. And what will then do is Create class two here. And we're gonna do a class to overload the constructor and that's going to take in an integer variable. We're going to do friend class. Class one. So it's from class one, the void print function created private integer variable int on and if we were to just run this man timber valid or not typing in class two, did you mean class one?
Basically, even though we have class two here is declared afterwards because declared afterwards the compiler saying, well, there's no classical class one that exists, simply because well declared afterwards. So what you have to do is, and we can't just switch them around, but if we switch him around, he won't work because we are you class one here. So what we have to do is just simply create a template class here. So we'll just do the bare bones and put a semicolon after we run that he succeeds, because it knows that there's a class called class two now, but it's not implemented until here, which is which is fine. And what we're going to do is implement the function so I've been doing class two colon colon, class two, for the gifted is just the constructor can be in temp, we'll set it equal to temp. The next thing we'll do is void class.
One method is this take a class to object and what we're going to do is make be equal to co2 dot poly. void class to print function and here we'll do STD C. On STD and line. And then what we can do is get rid of this piece of code here created class one object called OBJ. In class two object OBJ two and I put the value of freedom. We're going to do OBJ dot mefferd. They pass in the class to object into OBJ to print function, and now we want this we get the value of free printf.
The ones we get class one object which is This one here, it just has a regular method which takes the class to object and he had a public integer B. Then we created class to object and we pass in the value free to the constructor. So okay, this one, we have this passed in, and we get it set to free. And then the next thing is we do OBJ dot method, which is this one here. And what this does is it assigns the value of b, or finding the value of class to I to B, which is right here. And then OBJ to the print function, just print out the class to variable.
So obviously, we're not doing anything with B. As you can see, because it's a friend, we can actually access car CL tos aka this private variable, even though Luckily, it is private. It shouldn't be accessible place because we put different class. But if we were to remove this a second, the only error we get is he says, all in the private memory of class two. And basically it's because class two no longer a friend of class one. Oh yeah, that is really it.
So if you have the front class declared in this object in this class, then obviously, which one is declared, that is the one that can access this classes, private variables and methods. That's it for this tutorial, there is a little bit to take in that there's quite a lot to take in. It is quite complex, and the topics are going to get a little more complex. And we've done quite a lot of basics. So just watch it again. And again.
There's a lot of material on Google as well. They can access where the button say, and if you just have any questions, feel free to message us or support us on our systems Dakota UK email In the description, you can comment on the video, or just directly messages via YouTube, or the requirements for source code will also be in the description so you can just compare contrast. In the next part of the series we're going to look at class inheritance. And as usual, thank you for watching and hope you have a great day.