Friend Class in C++ Example 1 | C++ Tutorial | Mr. Kishore

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi welcome to her HIIT this is Kishore and today we are going to discuss about friend class already in previous session we have discussed that what is a friend function and how to introduce yourself unction to the class today we are going to discuss about friend class okay regarding to the previous session a friend function is a non-member function different function is a non-member function of a class but it is able to access the private data of those classes where it is introduced as a friend here the point is only the friend function can access the private data but I want to access the whole data from another class okay I want to access the whole data of a class from another class in this situation we are having only the chances that is friend class that is why a friend class is just introduce it as a friend to another class whenever if class is introduced as a friend to another class now this class is able to access all the private members of that class and here in this situation the introduced class is called the introduced to the class is called container class under the declared class where we have declared the front class is called container class okay here we are having container class and as well as container class container class which is the existing class means which is the primary class whenever this class is introduced as a friend to another class that class is called container class and this relationship is called this relationship is called commish Commission okay thus for here I am going to explain what is friend function and what is friend class okay first of all I am going to give one example on friend class how to introduce one friend class first off for example general header files hash include iostream dot h and hash include some corner dot h ok and here one more thing is we can declare friend class in two different ways one is directly declaring the friend class in one class and axing in another class second one is by declaring the object of one class in another class we can access the private member's now I will show you the both methods first how to introduce the friend class suppose there is a class called test one okay here some data members like a B now and B are private data members and as per C++ data hiding concept private data should not be available outside the class okay here private data is not available outside of the class then how to access the private data means we should have to declare public member function that is our here in public area okay generally we are going to write some functions now the point is here I want to access this data through friend function or friend class that is why what I have to do is I have to declare a friend class inside this class okay the means I want the access this private data from another class but actually the rule is what the private data should be accessible within the class only but I want to access this private data from another class for this we should have to declare another class as a friend to this class that is why here friend which class for example testa - okay now friend class testa - here watch it test one is what the first class okay and here it is having the now I want to access this private data from testa 2 plus for this first of all how to introduce the testa to class as a friend now ok both our friends here void get a B suppose it is a number function to access the a B values okay here I want to enter some data C out enter a B values X C in a B now the function closed class also closed and here watch this test one is the Class A B are the private numbers and to access this private data or is it a member function called get heavy through the gate JB I am reading the values for a and B but there is no put function now I want to print this a B values from tester to class that is why first of all how to define the class tester - next here there is no data requirement why because already we are having the data in test one class I want to access a be from test - - that is why here there is no need of any data directly I am going to public friction next I want to access this data through tester - function that is why here one function called put TB here the point is put KB is the member of test - - okay KB is the number of tests - and through the put KB function I want to access the a B of test one that is why here in brackets you should have to send object as argument because of the private data is not available outside the class okay the rule is what private data should not be visible the private data is not visible outside of the class then how it is possible means only through the class type parameters now class type parameter is called what object that is why here X - 1 t1 now t1 is the object of test 1 now I am going to access t1 AV here see out a equal to p1 dot a tender now it is going to print a value first of all and next slide C out B equals t2 or t1 dot B and function closure class also closed now watch this is number actually ABR available or declared in this one but we are asking a B from test two because of tester to e is declared as a friend to test one okay in this situation only we can access the test one data through the test for example now I am going to make a comment now it is giving error here okay undefined symbol a undefined symbol B otherwise justa - or test 1 colon colon a is not accessible test 1 : : B is not accessible we are going to get that error that's what to avoid this error you should how to introduce esta - as a friend that is why I am going to remove the comment now catch-22 is the friend okay already we have discussed that whenever we are having the friend they can access our data means our mobile everything ok like that is nothing but the friend class concept next here what happened we are declaring testable object inside the tester - ok in this situation okay in this situation test one is called container class now it is the container class untested - is having what test - one object that means what happened one class contains the object of another class now in this situation this class is called container class okay now it is the existing class and this existing class object is declaring another class and that class is called container class and this relationship is called composition this relationship is called composition that's why composition is the process of composition is the process of declaring one class object in another class declaring the object of one class within another class this one is called composition now with this what happened one class data is reused in another class okay now test one a B is reused in test tube that is why composition ellos the concept of reusability okay the friend class ellos the concept of reusability means once again we are using the existing data in another class okay but here it doesn't tell use the extensibility it is a most important thing now generally in introduced they are asking the question what is the difference between composition and inheritance okay actually it is looking like inheritance concept but it is quite different why because in in evidence what happens we are we are deriving a class we are deriving a class from existing class that is why the derived class is having both base class properties under derived class properties okay observe the difference between composition and inheritance in composition what happening just we are declaring a existing class object in another class due to this what happens that existing class data once again used in derived class means in container class that means it allows only what reusability concept and it never ellos extensibility that means these properties are not added to test tuna here astute we is able to access the test one data but test one data is never added to test two but in incidence what happens from test one we are deriving the test to do this is what happens now the tester - is having both the test one data on this own data also for example here ABR there here XY are there now the test - two data members a B and XY okay therefore here the point here a B data members here XY data members both are available in inheritance what happens now the derived class okay the newly created class is called derived class on the derived class contains both of the base class numbers as well as derived class members that is why inheritance provides the concept of reusability and extensibility but composition allows only the reusability it is the difference between inheritance and composition okay and to achieve this one we should have to declare this class as a friend to this class okay and this concept is called a composition next how to call these number functions of all this member function now main function required okay later first of all we have to declare the objects tester on t1 tested to t2 now to class objects are created one is from test one another one is tested to Lex here get a B member function is the member of this one that is why p1 dot get a V okay now when this function is called what happens Co train is working that is why here when get AV is called enter a B values now the user is entering some data now they are going to store in both a and B next we have to print this data but print function is available in test two and testator is what friend from friend class that I hear T 2 dot put t AV we do not put AV of which data I want to print test of all AV data that is why we access the test one AV data from T 2 you should have to send the T 1 object because of this function definition contains object as argument which class object test own class that is why here put t AV is having T 1 as the argument now T 1 okay here it is the actual parameter it is a formal parameter now this one is going to bring to the t1 and t1 be because of t2 is the friend of t1 ok then only it is possible next get CH program finish ok it is how to introduce one class as your friend to another class okay it is one kind of axing class members from another class now i will show you how to access the class members from another class in different manner okay we have discussed into is no one is what defining of friend class another one is what declaring the object in another class directly okay it is how to access the one class data members from other class ok thank you for watching [Music]
Info
Channel: Naresh i Technologies
Views: 80,609
Rating: undefined out of 5
Keywords: Friend Function in C++, Friend Function, friend function program, C++ Tutorial, C++ Training, Kishore, Naresh IT, Friend Class, Friend Class in C++, C++ Examples
Id: noRJK8wU3BI
Channel Id: undefined
Length: 15min 4sec (904 seconds)
Published: Fri Apr 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.