Implement the Array class that is defined below and test it in the main program.
ID: 3804712 • Letter: I
Question
Implement the Array class that is defined below and test it in the main program. The main program must test all the member functions including the overloaded operators that are defined in the class.
#ifndef array_h
#define array_h
#include <iostream>
using namespace std;
class Array { // Class declaration
friend const Array operator+(const Array & a1, const Array &a2);
friend bool operator==(const Array & a, const Array &b);//Equality test
friend ostream & operator <<(ostream & os, const Array & a);
friend istream & operator >>(istream & is, const Array & a);
public:
Array(int = 10); //Initialize the array with 0 values, default size =10
Array(const Array & a); // copy constructor
~Array();//Destructor
int getSize();// return the size of the array.
const Array & operator=(const Array & a);//assignement operator
Array & operator+(int x);//+ operator
bool operator!=(const Array & a) const;//Not equal test
Array operator-();//Negate (unary operation)
Array & operator++();//pre increment
Array & operator+=(const Array & a);
private:
int size; // size of created array
int * arr;
};
#endif
Test your class with the following main (your class should work with this main without any modifications):
#include<iostream>
#include "Array.h"
#include<cmath>
using namespace std;
void main()
{
Array a(5), b(5);
cout << "Please Enter the array A : ";
cin >> a;
cout << "Please Enter the array B : ";
cin >> b;
cout << "A = " << a << " B = " << b << endl;
Array c = (++a + b);
cout << "C = ++A + B = " << c << endl;
cout << "A after the ++ = " << a << endl;
cout << "-C = " << -c << endl;
cout << "C = " << c << endl;
c = a;
cout << "C = A = " << c << endl;
cout << "C == A = " << (c == a) << endl;
cout << "C != A = " << (c != a) << endl;
c += a;
cout << "C += A = " << c << endl;
Array d = (c + 5);
cout << "D = C + 5 = " << d << endl;
}
Sample Output:
EL CAWindowslsystem32Mcmd.exe lease Enter the array A lease Enter the array B 2 2 2 2 2 2 2 2 2 1 1 1 1 ++A 4 4 4 4 4 after the 2 2 2 2 4 -4 4 4 4 4 4 A 4 4 4 4 4 11 0 Then C 4 0 4 4 4 2 Press any key to continueExplanation / Answer
#include <iostream>
template <class T>
class Array {
public:
explicit Array(const int&);
Array(const int&, const T&);
Array(const Array&);
virtual ~Array();
T getVal(const int&) const;
void setVal(const int&, const T&);
T& operator[] (T);
Array& operator= (const Array&);
template <class U>
friend std::ostream& operator<< (std::ostream&, const Array<U>&);
private:
T* arr;
int size;
};
template <class T>
Array<T>::Array(const int& init_size) {
arr = new T[init_size];
size = init_size;
}
template <class T>
Array<T>::Array(const int& init_size, const T& init_value) {
arr = new T[init_size];
size = init_size;
for (int i = 0; i < size; ++i) {
arr[i] = init_value;
}
}
template <class T>
Array<T>::Array(const Array& original) {
size = original.size;
arr = new T[size];
for (int i = 0; i < size; ++i) {
arr[i] = original.arr[i];
}
}
template <class T>
Array<T>::~Array() {
delete [] arr;
}
template <class T>
T Array<T>::getVal(const int& index) const {
return arr[index];
}
template <class T>
void Array<T>::setVal(const int& index, const T& value) {
arr[index] = value;
}
template <class T>
T& Array<T>::operator[] (T index) {
return arr[index];
}
template <class T>
Array<T>& Array<T>::operator= (const Array& copy) {
if (arr == copy.arr)
return *this;
size = copy.size;
delete [] arr;
arr = new T[size];
for (int i = 0; i < size; ++i) {
arr[i] = copy.arr[i];
}
return *this;
}
template <class T>
std::ostream& operator<< (std::ostream& out, const Array<T>& self) {
out << "[ ";
for (int i = 0; i < self.size-1; ++i) {
out << self.arr[i] << ", ";
}
out << self.arr[self.size-1] << " ]";
return out;
}