Source of scope3.cpp


  1: //scope3.cpp
  2: //Illustrates class scope

  4: #include <iostream>
  5: using namespace std;

  7: #include "utilities.h"
  8: using Scobey::Pause;

 10: int i = 1;

 12: class C
 13: {
 14: public:
 15:     int valueOfI() { return i; }
 16:     void setValueOfI(int i) { this->i = 2*i+1; }
 17: private:
 18:     int i;  //This i has class scope. (It "belongs to" class C.)
 19: };


 22: int main()
 23: {
 24:     cout << endl << i << endl;
 25:     Pause(0, "", 1);
 26:     {
 27:         cout << i << endl;
 28:         Pause(0, "", 2);

 30:         i = 5;
 31:         cout << i << endl;
 32:         Pause(0, "", 3);

 34:         int i = 7;
 35:         cout << i << endl;
 36:         Pause(0, "", 4);

 38:         C c;
 39:         c.setValueOfI(i);
 40:         cout << c.valueOfI() << endl;
 41:         Pause(0, "", 5);
 42:     }
 43:     cout << i << endl;
 44:     Pause(0, "", 6);

 46:     C c;
 47:     c.setValueOfI(i);
 48:     cout << c.valueOfI() << endl;
 49:     Pause(0, "", 7);
 50: }

 52: /*
 53: Question:
 54: What values are output?

 56: Answer:
 57: 1 1 5 7 15 5 11
 58: */