ya_zay4eg

//48_friend

Jul 14th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. //48_friend
  2.  
  3. /*
  4. friend of a class is sth. that totally separated
  5. from a class but still has access to the stuff of
  6. the class
  7. */
  8.  
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. class StankFist {
  13. public:
  14.     StankFist (){stinkyVar=0;}//consr. that set sV equal to 0
  15.     ;
  16. private:
  17.     int stinkyVar;
  18.     friend void stinkysFriend (StankFist &sfo);//as a parameter takes &sfo StankFist object
  19. };// friend has access to all that stuff inside StankFist object
  20. // a class has to give away his friendship
  21. void stinkysFriend (StankFist &sfo) { //(bob)//shows where we takes a parametr
  22. sfo.stinkyVar=99;//using sfo (object)  we have access to all functions and objects in class
  23. cout<<sfo.stinkyVar<<endl;
  24. }
  25.  
  26. int main()
  27. {
  28.     StankFist bob;//stinkyVar=0, because it's constructor
  29.     stinkysFriend(bob);//stinkysFriend function takes StankFist object (sfo) as a parameter
  30. }
Advertisement
Add Comment
Please, Sign In to add comment