Source of TestStuff20160127.cpp


  1: //TestStuff20160127.cpp

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

  7: #include "time.h"

  9: int main(int argc, char* argv[])
 10: {
 11:     //int i = 6;
 12:     //cout << i << endl;
 13:     //cout << &i << endl; //& is the "address of" operator

 15:     //int* iPtr; //iPtr is a "pointer variable", point at an int location
 16:     //iPtr = &i; //iPtr can contain the address of an "ordinary" variable i
 17:     //cout << iPtr << endl; //Shows same value as &i
 18:     //cout << *iPtr << endl; //iPtr is "dereferenced"
 19:     //                         //to give the value at its address

 21:     //iPtr = new int; //Now iPtr points at a location on "the heap"
 22:     //cout << *iPtr << endl; //Outputs a garbage value
 23:     //*iPtr = 15; //Put 15 in that location on the heap
 24:     //cout << *iPtr << endl; //Confirm that 15 is indeed there

 26:     //delete iPtr; //Return the storage to the heap if it's no longer needed
 27:     //cout << *iPtr << endl; //Garbage again
 28:     //iPtr = nullptr; //Says explicitly that iPtr does not point at anything
 29:     //cout << *iPtr << endl; //So now we get a program crash

 31:     //The sizeof operator returns the number of bytes occupied by a type
 32:     //or a variable of a certain type
 33:     //int i = 6;
 34:     //cout << sizeof i << endl;
 35:     //cout << sizeof (int) << endl;
 36:     //int a[] = {1, 2, 3, 4, 5};
 37:     //cout << sizeof (a) << endl;

 39:     //int a[] = {1, 2, 3, 4, 5}; //an ordinary array of int values
 40:     //int* iptr = &a[0]; //iptr points at the first of those values
 41:     //cout << *iptr << endl; //shows the first value in the array
 42:     //iptr = &a[3]; //iptr points at the 4th value in the array
 43:     //cout << *iptr << endl; //shows the 4th value in the array

 45:     //int a[] = { 1, 2, 3, 4, 5 };
 46:     //cout << *a << endl; //OK because the name of an array is also a pointer
 47:     //                    // to its first element
 48:     //int* iPtr = a; //Equivalent to int* iPtr = &a[0];
 49:     //cout << *iPtr << endl; //Shows the first element of the array

 51:     //We can do "pointer arithmetic" if our pointer points into an array.
 52:     //That is we can increment and decrement such a pointer variable, or
 53:     //add or subtract an integer to or from the pointer value, and this
 54:     //arithmetic is "smart" in the sense that the pointer value will move
 55:     //the required number of bytes (4 for ints, 8 for doubles, for example).
 56:     //iPtr++;
 57:     //cout << *iPtr << endl;
 58:     //cout << *(iPtr + 2) << endl;


 61:     //int a[] = {1, 2, 3, 4, 5};
 62:     //cout << sizeof (a) << endl;
 63:     //int* iPtr = a;
 64:     //cout << *iPtr << endl;
 65:     //iPtr++;
 66:     //cout << *iPtr << endl;
 67:     //a++;  //Can't do this because a is "const"

 69:     //int* iPtr = new int[5];
 70:     //cout << sizeof (iPtr) << endl;
 71:     //iPtr[0] = 1;
 72:     //iPtr[1] = 2;
 73:     //iPtr[2] = 3;
 74:     //iPtr[3] = 4;
 75:     //iPtr[4] = 5;
 76:     //for (int i = 0; i < 5; i++)
 77:     //{
 78:     //        cout << iPtr[i] << " ";
 79:     //}
 80:     //cout << endl;

 82:     //int a[] = {1, 2, 3, 4, 5};
 83:     //cout << *a << endl;
 84:     //cout << a << endl;

 86:     //char s1[] = {'H', 'i'};
 87:     //cout << *s1 << endl;
 88:     //cout << s1 << endl;

 90:     //char s2[] = {'H', 'i', '\0'};
 91:     //cout << *s2 << endl;
 92:     //cout << s2 << endl;

 94:     //char s3[] = "Hi";
 95:     //cout << *s3 << endl;
 96:     //cout << s3 << endl;

 98:     //char s[] = "Hello, world!";
 99:     //cout << s+7 << endl;
100:     //cout << *(s+7) << endl;

102:     //What is the output of the following code
103:     //if the command-line input is the following
104:     //four-word sentence?
105:     //That thing hardly passes.
106:     //for (int i = 1; i < argc; i++)
107:     //{
108:     //        cout << *(argv[i]+i);
109:     //}
110:     //cout << endl;

112:     //for (int i = 1; i < argc; i++)
113:     //{
114:     //        cout << argv[i]+i << endl;
115:     //}

117:     //char s[] = "Hello, world!";
118:     //char* s = "Hello, world!";
119:     //cout << s + 7 << endl;
120:     //cout << *(s + 7) << endl;


123:     Time t1(1, 2, 3);
124:     cout << t1 << endl;
125:     t1.increment();
126:     cout << t1 << endl;

128:     Time* tPtr = new Time();
129:     cout << tPtr << endl;
130:     cout << *tPtr << endl;

132:     (*tPtr).increment();
133:     //Parentheses needed since . has higher precedence than *

135:     cout << *tPtr << endl;

137:     tPtr->increment();
138:     //Equivalent to, and more concise than, (*tPtr).increment() above

140:     cout << *tPtr << endl;
141: }