1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <iostream>
#include <stdexcept>
#include "VectorN.h"
using std::cout;
using std::endl;
using std::out_of_range;
int main()
{
int test = 1;
cout << "\nTest " << test++ << ": Default constructor and printing\n" << endl;
const VectorN v1;
cout << "v1: " << v1 << endl;
cout << "\nTest " << test++ << ": Array constructor and printing\n" << endl;
float ar2[] = {1.0, 2.0, 3.0};
const VectorN v2(ar2, 3);
cout << "v2: " << v2 << endl;
cout << "\nTest " << test++ << ": Clear and size\n" << endl;
VectorN v3(ar2, 3);
cout << "The size of v3: " << v3 << " is " << v3.size() << endl;
v3.clear();
cout << "After clearing, the size of v3: " << v3 << " is ";
cout << v3.size() << endl;
cout << "\nTest " << test++ << ": Subscripting\n" << endl;
float ar3[] = {-1.0, -3.0, -5.0, 7.0};
const VectorN v4(ar3, 4);
cout << "v4: " << v4 << endl;
cout << "v4[0]: " << v4[0] << " v4[1]: " << v4[1] << endl;
cout << "v4[2]: " << v4[2] << " v4[3]: " << v4[3] << endl;
VectorN v5(ar3, 4);
v5[0] = 17; v5[1] = 3; v5[2] = 0; v5[3] = -9;
cout << "v5: " << v5 << endl;
cout << "\nTest " << test++ << ": Copy constructor\n" << endl;
const VectorN v6(ar2, 3);
const VectorN v7 = v6;
cout << "v6: " << v6 << " size: " << v6.size() << endl;
cout << "v7: " << v7 << " size: " << v7.size() << endl;
VectorN v8(ar3, 4);
VectorN v9 = v8;
cout << "v8: " << v8 << " size: " << v8.size() << endl;
cout << "v9: " << v9 << " size: " << v9.size() << endl;
v8[1] = 300.0;
cout << "Changing..." << endl;
cout << "v8: " << v8 << " size: " << v8.size() << endl;
cout << "v9: " << v9 << " size: " << v9.size() << endl;
cout << "\nTest " << test++ << ": Assignment operator\n" << endl;
VectorN v10(ar3, 4);
VectorN v11;
cout << "v10: " << v10 << endl;
cout << "v11: " << v11 << endl;
v11 = v10;
v10[0] = 0.0;
cout << "v10: " << v10 << endl;
cout << "v11: " << v11 << endl;
cout << endl;
// Chained assignment
VectorN v12, v13;
cout << "v11: " << v11 << endl;
cout << "v12: " << v12 << endl;
cout << "v13: " << v13 << endl;
v13 = v12 = v11;
cout << "v11: " << v11 << endl;
cout << "v12: " << v12 << endl;
cout << "v13: " << v13 << endl;
cout << endl;
// Assignment to self
v13 = v13;
VectorN v14(ar2, 3);
cout << "v13: " << v13 << endl;
cout << "\nTest " << test++ << ": Addition and subtraction\n" << endl;
float ar4[] = {-2.0, 3.0, -1.0};
float ar5[] = {0, 1, 2, -3};
const VectorN v15(ar2, 3), v16(ar4, 3);
const VectorN v17(ar5, 4);
cout << "v13: " << v13 << endl;
cout << "v15: " << v15 << endl;
cout << "v16: " << v16 << endl;
cout << "v17: " << v17 << endl;
cout << endl;
cout << "v15 + v16 is " << v15 + v16 << endl;
cout << "v13 + v17 is " << v13 + v17 << endl;
return 0;
}
|