In: Computer Science
Please write a C++ program.
Please rewrite your Array (including the operator overloading)
into a template.
And rewrite your main function to test your template for integer
array and double array.
Following is my complete code:
#include <iostream>
using namespace std;
class Array {
private:
// Pointer to memory block to store integers
int* data;
// Maximum size of memory block
int cap;
// Stores number of integers in an array
int num;
public:
// Constructor
Array(int size);
// Default constructor
Array();
// Copy Constructor
Array(const Array&);
// Destructor
~Array();
// Copies element of a's into this. It is used in both copy
constructor and assignment function
void copy(const Array& a);
// Assignment operator overloading
Array& operator=(const Array& a);
// Deletes old elements, sets new size and allocates new
elements
void setSize(int n);
// Adds elements at the end
int push_back(int);
// Removes last element
int pop_back();
// Removes element at index
int remove(int i);
// Inserts element at given index
int insert(int num, int i);
// Returns capacity of the vector or array
int capacity();
// Returns size of array
int size();
// Displays all element
void display();
// Clears all elements
int Clear();
// operator *
Array operator*(const Array& a);
// subscript operator for const objects returns rvalue
int operator[]( int ) const;
// operator<<
friend ostream& operator<<(ostream& out, const
Array& a);
};
Array::Array(int size)
{
cap = size;
data = new int[cap];
num = 0;
}
Array::Array()
{
num = 0;
data = 0;
}
// Copy constructor
Array::Array(const Array& a)
{
num = a.num;
data = new int[num];
// Copy a's elements
copy(a);
}
void Array::copy(const Array& a)
{
// Copy a's elements into the elements of *this
int* p = data + num;
int* q = a.data + num;
while (p > data)
*--p = *--q;
}
Array::~Array()
{
delete[] data;
}
Array& Array::operator=(const Array& a)
{
if (this != &a) // avoids self assignment
{
setSize(a.num);
copy(a);
}
return *this;
}
void Array::setSize(int n)
{
if (n != num)
{
// Delete old elements
delete[] data;
// Set new count,
num = n;
// And allocate new elements
data = new int[n];
}
}
int Array::push_back(int el)
{
if (num <= cap)
{
data[num] = el;
num++;
return num;
}
else
{
cout << "Array capacity out of bound";
return -1;
}
}
int Array::pop_back()
{
if (num > 0)
{
int x = data[num - 1];
num--;
return x;
}
else
{
cout << "Array empty";
return -1;
}
}
int Array::remove(int i)
{
if (num > 0)
{
int x = data[i];
for (int p = i; p < num; p++)
data[p] = data[p + 1];
num--;
return x;
}
else
{
cout << "Array empty";
return -1;
}
}
int Array::insert(int n, int i)
{
if (num + 1 <= cap)
{
for (int p = num - 1; p >= i; p--)
data[p + 1] = data[p];
data[i] = n;
num++;
return num;
}
else
{
cout << "Array capacity out of bound";
return -1;
}
}
void Array::display()
{
if (num == 0)
cout << "Array empty";
else
{
for (int k = 0; k < num; k++)
cout << data[k] << " ";
cout << endl;
}
}
int Array::capacity()
{
return cap;
}
int Array::size()
{
return num;
}
int Array::Clear()
{
num = 0;
return num;
}
// function to multiply arrays this and a (element by element)
and return the resultant array
Array Array:: operator*(const Array& a)
{
Array result(*this); // create result array same as this using copy
constructor
// size of arrays are same
if(num == a.num)
{
// loop to multiply elements in the same index
for(int i=0;i<num;i++)
{
result.data[i] *= a.data[i];
}
}else // size not equal
cout<<"ERROR: Arrays to be multiplied should be of same
size"<<endl;
return result; // return the resultant array
}
// subscript operator for const objects returns rvalue
int Array:: operator[]( int index) const
{
// validate index, if valid return the value at index
if(index >=0 && index < num)
return data[index];
else // invalid index, return -1
cout<<"ERROR: Invalid index"<<endl;
return -1;
}
// operator<<
ostream& operator<<(ostream& out, const Array&
a)
{
if (a.num == 0) // array is empty
out << "Array empty";
else
{ // loop over the array a, displaying elements of a
for(int k = 0; k < a.num; k++)
out << a.data[k] << " ";
}
return out;
}
int main()
{
cout << "For Integer data type" << endl;
Array a(10);
a.push_back(1);
a.push_back(2);
a.push_back(3);
cout << "Array Elements are: " << endl;
a.display();
Array b;
b = a; // Assignment
cout << "Array Elements after assignment: " <<
endl;
b.display();
Array c = a;
cout << "Array Elements copied using copy construcor: " << endl;
c.display();
cout << a.remove(1) << endl;
a.display();
a.insert(10, 1);
a.display();
c = a*b;
cout<<"operator[]:"<<endl;
cout<<"a: ";
for(int i=0;i<a.size();i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"operator<<"<<endl;
cout<<"b: "<<b<<endl;
cout<<"Array multiplication: "<<endl;
cout<<"c = a*b: "<<c<<endl;
cout << "capacity" << a.capacity() <<
endl;
cout << "size of an array" << a.size() <<
endl;
cout << a.pop_back() << endl;
cout << "size of an array" << a.size() <<
endl;
a.Clear();
a.display();
return 0;
}
//end of program
// C++ program to convert Array to template class and test it with Integer and Double data type
#include <iostream>
using namespace std;
template <class T>
class Array {
private:
// Pointer to memory block to store type T
T* data;
// Maximum size of memory block
int cap;
// Stores number of integers in an array
int num;
public:
// Constructor
Array(int size);
// Default constructor
Array();
// Copy Constructor
Array(const Array&);
// Destructor
~Array();
// Copies element of a's into this. It is used in both copy
constructor and assignment function
void copy(const Array& a);
// Assignment operator overloading
Array& operator=(const Array& a);
// Deletes old elements, sets new size and allocates new
elements
void setSize(int n);
// Adds elements at the end
int push_back(T);
// Removes last element
T pop_back();
// Removes element at index
T remove(int i);
// Inserts element at given index
int insert(T num, int i);
// Returns capacity of the vector or array
int capacity();
// Returns size of array
int size();
// Displays all element
void display();
// Clears all elements
int Clear();
// operator *
Array operator*(const Array& a);
// subscript operator for const objects returns rvalue
T operator[]( int ) const;
// operator<<
template <typename U>
friend ostream& operator<<(ostream& out, const
Array<U>& a);
};
template <class T>
Array<T>::Array(int size)
{
cap = size;
data = new T[cap];
num = 0;
}
template <class T>
Array<T>::Array()
{
num = 0;
cap = 0; // set cap to 0
data = 0;
}
// Copy constructor
template <class T>
Array<T>::Array(const Array<T>& a)
{
cap = a.cap; // set cap to a's cap
num = a.num;
data = new T[num];
// Copy a's elements
copy(a);
}
template <class T>
void Array<T>::copy(const Array<T>& a)
{
// Copy a's elements into the elements of *this
T* p = data + num;
T* q = a.data + num;
while (p > data)
*--p = *--q;
}
template <class T>
Array<T>::~Array()
{
delete[] data;
}
template <class T>
Array<T>& Array<T>::operator=(const
Array<T>& a)
{
if(this != &a) // avoids self assignment
{
setSize(a.num);
copy(a);
}
return *this;
}
template <class T>
void Array<T>::setSize(int n)
{
if (n != num)
{
// Delete old elements
delete[] data;
// Set num to 0, since all elements will be deleted
num = 0;
cap = n; // set cap to n
// And allocate new elements
data = new T[n];
}
}
template <class T>
int Array<T>::push_back(T el)
{
// number of elements < cap, add el at the end
if (num < cap)
{
data[num] = el;
num++;
return num;
}
else
{
cout << "Array capacity out of bound";
return -1;
}
}
template <class T>
T Array<T>::pop_back()
{
if (num > 0)
{
T x = data[num - 1];
num--;
return x;
}
else // empty array, return default element
{
cout << "Array empty";
return T();
}
}
template <class T>
T Array<T>::remove(int i)
{
if(num > 0)
{
T x = data[i];
for (int p = i; p < num; p++)
data[p] = data[p + 1];
num--;
return x;
}
else // array empty, return default element
{
cout << "Array empty";
return T();
}
}
template <class T>
int Array<T>::insert(T n, int i)
{
if (num + 1 <= cap)
{
for (int p = num - 1; p >= i; p--)
data[p + 1] = data[p];
data[i] = n;
num++;
return num;
}
else
{
cout << "Array capacity out of bound";
return -1;
}
}
template <class T>
void Array<T>::display()
{
if (num == 0)
cout << "Array empty";
else
{
for (int k = 0; k < num; k++)
cout << data[k] << " ";
cout << endl;
}
}
template <class T>
int Array<T>::capacity()
{
return cap;
}
template <class T>
int Array<T>::size()
{
return num;
}
template <class T>
int Array<T>::Clear()
{
num = 0;
return num;
}
// function to multiply arrays this and a (element by element)
and return the resultant array
template <class T>
Array<T> Array<T>:: operator*(const Array<T>&
a)
{
Array<T> result(*this); // create result array same as this
using copy constructor
// size of arrays are same
if(num == a.num)
{
// loop to multiply elements in the same index
for(int i=0;i<num;i++)
{
result.data[i] *= a.data[i];
}
}else // size not equal
cout<<"ERROR: Arrays to be multiplied should be of same
size"<<endl;
return result; // return the resultant array
}
// subscript operator for const objects returns rvalue
template <class T>
T Array<T>:: operator[]( int index) const
{
// validate index, if valid return the value at index
if(index >=0 && index < num)
return data[index];
else // invalid index, return -1
cout<<"ERROR: Invalid index"<<endl;
return T();
}
// operator<<
template <class T>
ostream& operator<<(ostream& out, const
Array<T>& a)
{
if (a.num == 0) // array is empty
out << "Array empty";
else
{ // loop over the array a, displaying elements of a
for(int k = 0; k < a.num; k++)
out << a.data[k] << " ";
}
return out;
}
int main()
{
// test the template class
cout << "For Integer data type" << endl;
Array<int> a(10);
a.push_back(1);
a.push_back(2);
a.push_back(3);
cout << "Array Elements are: " << endl;
a.display();
Array<int> b;
b = a; // Assignment
cout << "Array Elements after assignment: " <<
endl;
b.display();
Array<int> c = a;
cout << "Array Elements copied using copy construcor: " << endl;
c.display();
cout << a.remove(1) << endl;
a.display();
a.insert(10, 1);
a.display();
c = a*b;
cout<<"operator[]:"<<endl;
cout<<"a: ";
for(int i=0;i<a.size();i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"operator<<"<<endl;
cout<<"b: "<<b<<endl;
cout<<"Array multiplication: "<<endl;
cout<<"c = a*b: "<<c<<endl;
cout << "capacity" << a.capacity() <<
endl;
cout << "size of an array" << a.size() <<
endl;
cout << a.pop_back() << endl;
cout << "size of an array" << a.size() <<
endl;
a.Clear();
a.display();
cout <<endl<<endl<<"For Double data type"
<< endl;
Array<double> d1(10);
d1.push_back(1.4);
d1.push_back(2.5);
d1.push_back(3.1);
cout << "Array Elements are: " << endl;
d1.display();
Array<double> d2;
d2 = d1; // Assignment
cout << "Array Elements after assignment: " <<
endl;
d2.display();
Array<double> d3 = d1;
cout << "Array Elements copied using copy construcor: " << endl;
d3.display();
cout << d1.remove(1) << endl;
d1.display();
d1.insert(10.2, 1);
d1.display();
d3 = d1*d2;
cout<<"operator[]:"<<endl;
cout<<"d1: ";
for(int i=0;i<d1.size();i++)
cout<<d1[i]<<" ";
cout<<endl;
cout<<"operator<<"<<endl;
cout<<"d2: "<<d2<<endl;
cout<<"Array multiplication: "<<endl;
cout<<"d3 = d1*d2: "<<d3<<endl;
cout << "capacity" << d1.capacity() <<
endl;
cout << "size of an array" << d1.size() <<
endl;
cout << d1.pop_back() << endl;
cout << "size of an array" << d1.size() <<
endl;
d1.Clear();
d1.display();
return 0;
}
//end of program
Output: