The Array Class sample driver (main)

This sample driver shows how the Array class is used.

The sample driver file   --   the whole thing: arraydriver.cpp

This sample main demonstrates how the functions of the Array class are used.
We'll go through main a little at a time starting with instantiating objects.
   // no objects yet
   cout << "# of arrays instantiated = "
        << Array::getArrayCount() << endl;

   // create two arrays and print Array count
   Array integers1(7), integers2;
   cout << "# of arrays instantiated = "
        << Array::getArrayCount() << endl << endl;

   // print integers1 size and contents
   cout << "Size of array integers1 is " << integers1.getSize() << endl
        << "Array after initialization:" << endl << integers1 << endl;

   // print integers2 size and contents
   cout << "Size of array integers2 is " << integers2.getSize() << endl
        << "Array after initialization:" << endl << integers2 << endl;
This code outputs:
# of arrays instantiated = 0
# of arrays instantiated = 2
Size of array integers1 is 7
Array after initialization:
0 0 0 0 0 0 0 
Size of array integers2 is 10
Array after initialization:
0 0 0 0 0 0 0 0 0 0
Since there are no Array objects instantiated, initially getArrayCount() displays zero, and after the objects integers1 and integers2 are instantiated, getArrayCount() displays two. Notice the use of the binary scope operator, :: , which ties the function getArrayCount() to the Array class. Because it's a static function, it's done this way.

The default constructor initializes all the elements of the member data int array, arrayPtr, to zero. The object integers1 is size 7, the parameter passed in, whereas integers2 has no parameter and is given the value 10 from the default parameter value in the constructor.

Now I give the objects values and try out the != operator.
   // input and print integers1 and integers2
   cout << "Input 17 integers:" << endl;
   cin >> integers1 >> integers2;
   cout << "After input, the arrays contain:" << endl
        << "integers1: " << integers1
        << "integers2: " << integers2 << endl;

   // use overloaded inequality (!=) operator
   cout << "Evaluating: integers1 != integers2" << endl;
   if (integers1 != integers2)
      cout << "They are not equal" << endl;
After the prompt, I type in the integers 1 through 17. And now you see the changed values in the int arrays and that they are determined to be not equal:
Input 17 integers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
After input, the arrays contain:
integers1: 1 2 3 4 5 6 7
integers2: 8 9 10 11 12 13 14 15 16 17
Evaluating: integers1 != integers2
They are not equal
Next the copy constructor and assignment operator are demonstrated:
   // create array integers3 using integers1 as an
   // initializer; print size and contents
   Array integers3(integers1);

   cout << endl << "Size of array integers3 is " << integers3.getSize() << endl
        << "Array after initialization:" << endl << integers3 << endl;

   // use overloaded assignment (=) operator
   cout << "Assigning integers2 to integers1:" << endl;
   integers1 = integers2;
   cout << "integers1: " << integers1 << endl
        << "integers2: " << integers2 << endl;

   // use overloaded equality (==) operator
   cout << "Evaluating: integers1 == integers2" << endl;
   if (integers1 == integers2)
      cout << "They are equal" << endl << endl;
The copy constructor makes the object integers3 an exact duplicate of integers1 (although it does have all its own memory for size and the int array). The assignment operator changes integers1 so that it looks exactly like integers2. It also has its own memory so that if you made an assignment, say   integers1[4] = 82;   integers2 would not change. And you can see that the objects integers1 and integers2 are equal.
Size of array integers3 is 7
Array after initialization:
1 2 3 4 5 6 7
Assigning integers2 to integers1:
integers1: 8 9 10 11 12 13 14 15 16 17
integers2: 8 9 10 11 12 13 14 15 16 17
Evaluating: integers1 == integers2
They are equal
The last part of main demonstrates accessing the object's int array's elements.
   // use overloaded subscript operator to create rvalue
   cout << "integers1[5] is " << integers1[5] << endl;

   // use overloaded subscript operator to create lvalue
   cout << "Assigning 1000 to integers1[5]" << endl;
   integers1[5] = 1000;
   cout << "integers1: " << integers1 << endl;

   // attempt to use out of range subscript
   cout << endl << "Attempt to assign 1000 to integers1[15]" << endl;
   integers1[15] = 1000;                           // ERROR: out of range
The object's integers1[5] is shown before and after a change in value to 1000. Finally, when an attempt is made to assign an int array element that is out of bounds, the assertion in the function causes the program to crash giving an out of range error. The exact error message is "failed assertion '0 <= subscript && subscript < size' " .
integers1[5] is 13
Assigning 1000 to integers1[5]
integers1: 8 9 10 11 12 1000 14 15 16 17
Attempt to assign 1000 to integers1[15]