Files
  • main.cpp
  • arraydb.cpp
  • arraydb.hpp
  • outofbounds.hpp
main.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//M687M738
//Assignment 6
//array class to template

#include "arraydb.cpp"
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <exception>
#include <new>
using namespace std;


/*Description:
Asked to take Arraydb code from class and create templates for generic type using array db that accepts any basic type. Test each type in main function. 
*/

/*Psuedo:
First, Declare the arry using the overloaded class template, and define the array of the array.
Next, assign values withing the index of the standard array.
Then, initialize the array via the standard array.
Then, create default object for each basic type.
Then, create objects via class template via overloaded constructor. Display to screen.
Then, Check the array reaches the final index of the array. Display to screen.
*/

int main (void)
{

  int indexSize;
  cout << "Please enter an index: " << endl;
  cin >> indexSize;
  
  try
  {
    while(indexSize < 0)
     new short[indexSize];
     new int[indexSize];
     new long[indexSize];
     new long long [indexSize];
     new double[indexSize];
     new long double[indexSize];
     new float[indexSize];
     new char[indexSize];
     new bool[indexSize];
  }

  catch (const bad_alloc& e)
  {
    cout << "Allocation failed." << endl << e.what() << endl;

  }

   
   // Basic array types declared with overloaded constructor
   ArrayDb<short> shortArray(indexSize, 1);
   ArrayDb<int> intArray(indexSize, 13);
   ArrayDb<long> longArray(indexSize, 133);
   ArrayDb<long long> longLongArray(indexSize, 1313);
   ArrayDb<double> doubleArray(indexSize, 13.13);
   ArrayDb<long double> longDoubleArray(indexSize, 13.3);
   ArrayDb<float> floatArray(indexSize, 13.3f);
   ArrayDb<char> charArray(indexSize, 'f');
   ArrayDb<bool> boolArray(indexSize, 1);


  try 
  {
    shortArray [5];
    intArray [5];
    longArray [5];
    longLongArray [5];
    doubleArray [5];
    longDoubleArray [5];
    floatArray [5];
    charArray [5];
    boolArray [5];
    

  }

  catch (OutOfBounds& i)
    {
     
     cout << "Out of bounds caught!" << endl << endl;
     cout << i.what() << endl << endl;
     
    }
    

    cout << "Overloaded constructor arrays:\n" 
        << "short array:\t" << shortArray << endl
        << "int array:\t" << intArray << endl
        << "long array:\t" << longArray << endl
        << "long long array:\t" << longLongArray << endl   
		    << "double array:\t"<< doubleArray << endl
        << "long double array:\t"<< longDoubleArray << endl
		    << "float array:\t"<< floatArray << endl
		    << "char array:\t"<< charArray << endl  
        << "bool array:\t"<< boolArray << endl;


   //standard arrays
   short shortNum[5]= {5,7,9,2,4};
   int intNum[5] = {14, 30, 24, 25, 27};
   long longNum[5] = {400, 52, 3697, 44, 5000};
   long long longlongNum[5] = {40000, 16777, 50022, 4545, 6633};
   double doubleNum[5] = {6.444, 3.1415, 2.64, 13.1313, 4.444};
   long double longDoubleNum[5] = {33.1514, 47.893, 77.777, 500.56, 1000.01};
   float floatNum[5] = {1.12f, 2.16f, 3.14f, 5.59f, 7.77f};
   char chars[5] = {'j','o','s','e','f'};
   bool bools[5] = {1,0,1,0,1};
  
   // Initialize ArrayDb arrays from standard arrays
   ArrayDb<short> sArray (shortNum, 5);
   ArrayDb<int> iArray (intNum, 5);
   ArrayDb<long> lArray (longNum, 5);
   ArrayDb<long long> llArray (longlongNum, 5);
   ArrayDb<double> dArray (doubleNum, 5);
   ArrayDb<long double> ldArray (longDoubleNum, 5);
   ArrayDb<float> fArray (floatNum, 5);
   ArrayDb<char> cArray (chars, 5);
   ArrayDb<bool> bArray (bools, 5);
   
   cout << "ArrayDb arrays initialized from standard arrays:\n" 
        << "short:\t" << sArray << endl
        << "int:\t" << iArray << endl
        << "long:\t" << lArray << endl
        << "long long:\t" << llArray << endl
		    << "double:\t" << dArray << endl
        << "long double array:\t" << ldArray << endl
		    << "float array:\t" << fArray << endl
		    << "char array:\t" << cArray << endl  
        << "bool array:\t" << bArray << endl;
  
   // Default objects
   ArrayDb<short> defShort;
   ArrayDb<int> defInt;
   ArrayDb<long> defLong;
   ArrayDb<long long> defLongLong;
   ArrayDb<double> defDouble;
   ArrayDb<long double> defLongDouble;
   ArrayDb<float> defFloat;
   ArrayDb<char> defChar;
   ArrayDb<bool> defBool;

   // overloaded operator assigns array from default object via overloaded constuctor template
   cout << "Created short array\n";
   defShort = sArray;
   cout << "Copied short array into object:\n" << defShort 
        << endl;

   cout << "Created int array\n";
   defInt = iArray;
   cout << "Copied int array into object:\n" << defInt 
        << endl;

   cout << "Created long array\n";
    defLong = lArray;
   cout << "Copied long array into object:\n" <<  defLong 
        << endl;
 
   cout << "Created long long array\n";
   defLongLong = llArray;
   cout << "Copied long long array into object:\n" << defLongLong 
        << endl;

   cout << "Created double array\n";
   defDouble = dArray;
   cout << "Copied double array into object:\n" << defDouble 
        << endl;

   cout << "Created long double array\n";
   defLongDouble = ldArray;
   cout << "Copied long double array into object:\n" << defLongDouble
        << endl;
   
   cout << "Created float array\n";
   defFloat = fArray;
   cout << "Copied float array into object:\n" << defFloat 
		<< endl;
   
   cout << "Created char array\n";
   defChar = cArray;
   cout << "Copied char array into object:\n" << defChar 
		<< endl;

   cout << "Created bool array\n";
   defBool = bArray;
   cout << "Copied bool array into object:\n" << defBool 
		<< endl;
      
   //testing that program will reach end of index, displays information in final index of each array
   cout <<"Final index in default short array: "<< defShort[4] << endl << endl;
   cout <<"Final index in default int array: "<< defInt[4] << endl << endl;
   cout <<"Final index in default long array: "<< defLong[4] << endl << endl;
   cout <<"Final index in default long long array: "<< defLongLong[4] << endl << endl;
   cout <<"Final index in default double array: "<< defDouble[4] << endl << endl;
   cout <<"Final index in default long double array: "<< defLongDouble[4] << endl << endl;
   cout <<"Final index in default float array: "<< defFloat[4] << endl << endl;
   cout <<"Final index in default char array: "<< defChar[4] << endl << endl;
   cout <<"Final index in default bool array: "<< defBool[4] << endl << endl;

   return 0;
}