daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest Jan 13th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. Class TestClass {
  4. public:
  5.         // localVariable exists only inside doThings() - once doThings() finishes, localVariable will be destroyed.,
  6.     inline void doThings(int localVariable = 10)
  7.     {
  8.         // Because classVariable is a member of TestClass, it exists as long as the instance of the class does
  9.         classVariable = localVariable;
  10.  
  11.     }
  12.  
  13.     int classVariable = 5;
  14.    
  15. };
  16.  
  17.  
  18. int main ()
  19. {
  20.  
  21.     TestClass dongs = new TestClass();
  22.  
  23.     // Prints "5", because when we create the class, classVariable defaults to 5
  24.     printf("%d", dongs.classVariable); 
  25.  
  26.     dongs.doThings(2020);
  27.     // Prints "2020", because we gave the value 2020 to doThings, and it created localVariable with value of 2020, and then copied that value in to classVariable
  28.     printf("%d", dongs.classVariable);
  29.  
  30.     dongs.doThings();
  31.     // Prints 10, because the default value of localVariable is 10. We didn't give it a value, so it uses default.
  32.     printf("%d", dongs.classVariable);
  33.  
  34.  
  35.     /*
  36.     * No where inside main can we ever say: dongs.localVariable - it doesn't exist in the class, it only exists *inside* the function doThings.
  37.     * We can never go TestClass.classVariable or TestClass::classVariable, because TestClass is the name of the class.
  38.     * To access any function or variable, we need to create an instance of the class (first line of main()). UNLESS, the function or variable is static - then it persists even if we don't create an instance of the class
  39.     */
  40.  
  41.  
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top