In: Computer Science
c++
You will implement a template class with the following members:
Additionally you will need to create a short driver program that demonstrates your template class working with a few different numeric data types.
#include <iostream> using namespace std; template<class T> class Array { private: T *arr; int size; public: Array(int size) : size(size) { arr = new int[size]; } Array(T *arr, int size) : arr(arr), size(size) { } T max() { T result = arr[0]; for (int i = 0; i < size; ++i) { if (arr[i] > result) { result = arr[i]; } } return result; } T min() { T result = arr[0]; for (int i = 0; i < size; ++i) { if (arr[i] < result) { result = arr[i]; } } return result; } }; int main() { double d[] = {4.5, 2.9, 9.0, 1.2, 6.5}; Array<double> doubles(d, 5); cout << "Maximum is " << doubles.max() << endl; cout << "Minimum is " << doubles.min() << endl; int a[] = {2, 0, 1, 6, 9, 5}; Array<int> integers(a, 6); cout << "Maximum is " << integers.max() << endl; cout << "Minimum is " << integers.min() << endl; return 0; }