Question

In: Computer Science

Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template....

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

Solutions

Expert Solution

// 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:


Related Solutions

c++ using class... define operator overloading and give simple example how we can use operator overloading...
c++ using class... define operator overloading and give simple example how we can use operator overloading by writing simple program in which different operators are used to add, subtract, multiply and division.
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators in the code main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() { rectangleType rectangle1(23, 45); //Line 1 rectangleType rectangle2(12, 10); //Line 2 rectangleType rectangle3; //Line 3 rectangleType rectangle4; //Line 4 cout << "Line 5: rectangle1: "; //Line 5 rectangle1.print(); //Line 6 cout << endl; //Line 7 cout << "Line 8: rectangle2: "; //Line...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Rewrite your program for part 1. Do not declare the array globally, declare it in the...
Rewrite your program for part 1. Do not declare the array globally, declare it in the loop function. This now requires that you add two parameters to your fill array and print array functions. You must now pass the array name and array size as arguments, when the program calls these functions. The program has the same behavior as problem 1, but illustrates the difference between globally and locally declared variables. The program code for part 1 was: int Array[15]...
Please write a complete C coding program (NOT C++) that has the following: (including comments) -...
Please write a complete C coding program (NOT C++) that has the following: (including comments) - declares two local integers x and y - defines a global structure containing two pointers (xptr, yptr) and an integer (z) - declares a variable (mst) by the type of previous structure - requests the values of x and y from the user using only one scanf statement - sets the first pointer in the struct to point to x - sets the second...
C++ PLEASE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- In this program, you will analyze an array
C++ PLEASE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- In this program, you will analyze an array of 10 characters storing a gene sequence. You will be given a subsequence of 3 characters to look for within this array. If the subsequence is found, print the message: Subsequence <XXX> found at index <i>. Where i is the starting index of the subsequence in the array. Otherwise, print Subsequence <XXX> not found. The array of characters and the subsequence will be given through standard input. Read them and...
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT