Hello, I need help with templates. I have been very confused with calling them in the main(). I understand the general idea of templates and overloading functions, but actually doing it in c++ is proving to be difficult. I think i called the constructor and copy constructor successfully so far. My main task is listed below, please help me accomplish this.
Problem statement:
Add a template to the arrayi.h class so that elements of the arrays can be integer, float or double. Also add a subtraction operator to the class.
Task using the template implementations (in client_driver.cpp):
Create arrays A1, A2 and A3. A1 is size 4 with elements 6 7 8 9 A2 is size 7.
with elements 1 2 3 4 5 6 7. Assign A3 the difference of A1 and A2.
//arrayi.h file (header file)
//arrayi.t file (template implementations)Code:#ifndef ARRAYI_H_
#define ARRAYI_H_
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
template<typename T>
class Array;
template<typename T>
ostream &operator<< (ostream& output, const Array<T> &a);
template<typename T>
istream &operator>> (istream& input, Array<T> &a);
template<typename T>
class Array
{
public:
Array(int = 10); //constructor
Array(const Array &); //copy constructor
~Array(); //destructor
int getSize() const; //return size
Array &operator = (const Array &);
int operator==(const Array &) const;
int operator != (const Array &) const;
int &operator[] (int);
Array operator + (const Array&);
Array operator - (const Array&);
static int getArrayCount(); //get count of existing
//array objects
friend ostream &operator<< <>(ostream &output, const Array<T> &a);
friend istream &operator>> <>(istream &input, Array<T> &a);
private:
int *ptr; //ptr to first array element
int size; //size of the array
static int arrayCount; // #of arrays instantiated
};
#include "arrayi.t"
#endif
//arrayi.cppCode:#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
// Initialize static data member at file scope
template <typename T>
T Array<T>::arrayCount = 0; // no objects yet
// Default constructor for class Array
template <typename T>
Array<T>::Array(int arraySize)
{
++arrayCount; // count one more object
size = arraySize; // default size is 10
ptr = new int[size]; // create space for array
assert(ptr != 0); // terminate if memory not allocated
int i;
for (i = 0; i < size; i++)
ptr[i] = 0; // initialize array
}
// Copy constructor for class Array
template <typename T>
Array<T>::Array(const Array &init)
{
++arrayCount; // count one more object
size = init.size; // size this object
ptr = new int[size]; // create space for array
assert(ptr != 0); // terminate if memory not allocated
cout << "copy constr arrayi " << endl;
int i;
for (i = 0; i < size; i++)
ptr[i] = init.ptr[i]; // copy init into object
cout << endl << "copy constructor working" << endl;
}
// Destructor for class Array
template <typename T>
Array<T>::~Array()
{
--arrayCount; // one fewer objects
delete [] ptr; // reclaim space for array
}
// Get the size of the array
template <typename T>
int Array<T>::getSize() const
{
return size;
}
// Overloaded assignment operator
template <typename T>
Array<T> &Array<T>::operator=(const Array &right)
{
if (&right != this)
{ // check for self-assignment
delete [] ptr; // reclaim space
size = right.size; // resize this object
ptr = new int[size]; // create space for array copy
assert(ptr != 0); // terminate if memory not allocated
int i;
for (i = 0; i < size; i++)
ptr[i] = right.ptr[i]; // copy array into object
}
//this points to ptr to int, *this returns ptr value
//ie, the address of the array
return *this; // enables x = y = z;
}
// + operator for arrays
template <typename T>
Array<T> Array<T>::operator + (const Array& right)
{
int large, small;
if (size > right.size)
{
large = size;
small = right.size;
}
else
{
large = right.size;
small = size;
}
Array z(large);
int i;
for (i = 0; i < small; i++)
z.ptr[i] = ptr[i] + right.ptr[i];
for (i = small; i < large; i++)
{
if (right.size == small)
z.ptr[i] = ptr[i] ;
else z.ptr[i] = right.ptr[i] ;
}
cout << "# of arrays instantiated " << getArrayCount() << endl;
return z;
}
// Determine if two arrays are equal and
// return 1 if true, 0 if false.
template <typename T>
int Array<T>::operator==(const Array &right) const
{
if (size != right.size)
return 0; // arrays of different sizes
int i;
for (i = 0; i < size; i++)
if (ptr[i] != right.ptr[i])
return 0; // arrays are not equal
return 1; // arrays are equal
}
// Determine if two arrays are not equal and
// return 1 if true, 0 if false.
template <typename T>
int Array<T>::operator!=(const Array &right) const
{
if (size != right.size)
return 1; // arrays of different sizes
int i;
for (i = 0; i < size; i++)
if (ptr[i] != right.ptr[i])
return 1; // arrays are not equal
return 0; // arrays are equal
}
// Overloaded subscript operator
template <typename T>
int &Array<T>::operator[](int subscript)
{
// check for subscript out of range error
assert(0 <= subscript && subscript < size);
return ptr[subscript]; // reference return creates lvalue
}
// Return the number of Array objects instantiated
template <typename T>
int Array<T>::getArrayCount()
{
return arrayCount;
}
// Overloaded input operator for class Array;
// inputs values for entire array.
template <typename T>
istream &operator>>(istream &input, Array<T> &a)
{
int i;
for (i = 0; i < a.size; i++)
input >> a.ptr[i];
return input; // enables cin >> x >> y;
}
// Overloaded output operator for class Array
template <typename T>
ostream &operator<<(ostream &output, const Array<T> &a)
{
int i;
for (i = 0; i < a.size; i++)
{
output << a.ptr[i] << ' ';
if ((i + 1) % 10 == 0)
output << endl;
} //end for
if (i % 10 != 0)
output << endl;
return output; // enables cout << x << y;
}
//client_driver.cppCode:#include "arrayi.h"
Code:#include "arrayi.h"
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
int main()
{
Array<int> intObject; //call constructor
Array<int> intObjectCopy(intObject); //call copy constructor
//rest of code here
}