ya_zay4eg

//49_this

Jul 18th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. //49_this
  2. #include <iostream>
  3. using namespace std;
  4. #include "Hannah.h"
  5.  
  6. int main()
  7. {
  8. Hannah ho (23);//first we must create an object
  9. ho.printCrap();
  10. }
  11. //-------------------------------------------------
  12. #ifndef HANNAH_H
  13. #define HANNAH_H
  14.  
  15.  
  16. class Hannah
  17. {
  18.     public:
  19.         Hannah(int);//if we sent it 20, it say's h is equal to 20
  20.         void printCrap ();
  21.     private:
  22.         int h;
  23. };
  24.  
  25. #endif // HANNAH_H
  26. //--------------------------------------------------
  27. #include <iostream>
  28. using namespace std;
  29. #include "Hannah.h"
  30.  
  31. Hannah::Hannah(int num)//if we send 23
  32. :h (num)// it takes 23 from main and set it equal to h
  33. {
  34. }
  35.  
  36. void Hannah::printCrap (){
  37. cout<<"h= "<<h<<endl;//c++ assumes that you working with current object
  38. cout<<"this->"<<this->h<<endl;//explicitly tells that you working with this current object
  39.                              /*the ad of ho get the object and the
  40.                                object have a variable h and h is equal to 23*/
  41.                               //keyword this identify a special kind of pointer
  42.                               //this stores the address of current object that you working with
  43.                               // the address of ho use that object & the object has a variable h
  44. cout<<"(*this).h= "<<(*this).h <<endl; //dereferencing pointer, it's taking a memory location of
  45.                                        // of the current object and calling a variables value (.h)
  46.                                         //we using memory location
  47. }
Advertisement
Add Comment
Please, Sign In to add comment