Advertisement
Felanpro

friend keyword

Apr 2nd, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. Friend keyword:
  2. Lets a function acces members from a class,
  3.  
  4. Example:
  5.  
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. class FelixClass
  11. {
  12. public:
  13.     FelixClass()
  14.     {
  15.         regVar = 0;
  16.             }
  17.  
  18. private:
  19.     int regVar;
  20.  
  21.     friend void printCrap(FelixClass &felixobject);
  22.  
  23. };
  24.  
  25. void printCrap(FelixClass &felixobject)
  26. {
  27.     felixobject.regVar = 99;
  28.     cout << felixobject.regVar << endl;
  29. }
  30.  
  31. int main()
  32. {
  33.     FelixClass felixobject2;
  34.     printCrap(felixobject2);
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement