Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(C++) Please help, I'm stuck on class template arrays. How is the following reimplemented into a class template with a corresponding main program? The problem:

(C++) Please help, I'm stuck on class template arrays. How is the following reimplemented into a class template with a corresponding main program?

The problem:

Reimplement class Array from the following excerpt as a class template. Also write a main driver program that: 1) Asks the user for integer values, stores them in an integer Array, and then prints the values stored in the integer Array 2) Asks the user for string values, stores them in a string Array, and then prints the values stored in the string Array

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Array.h//

//Array class definition with overloaded operators #include "stdafx.h" #ifndef ARRAY_H #define ARRAY_H

#include

class Array { friend std::ostream& operator<<(std::ostream&, const Array&); friend std::istream& operator>>(std::istream&, Array&);

public: explicit Array(int = 10); Array(const Array&); ~Array(); size_t getSize() const;

const Array& operator=(const Array&); bool operator == (const Array&) const;

bool operator!=(const Array& right) const { return !(*this == right); }

int& operator[](int);

int operator[](int) const;

private: size_t size; int ptr; };

#endif

////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Array.cpp//

//Array class member- and friend-function definitions #include "stdafx.h" #include #include #include

#include "Array.h" // Array class definition using namespace std;

//default constructor for class Array (default size 10) Array::Array(int arraySize) : size{ (arraySize > 0 ? static_cast(arraySize) : throw invalid_argument{ "Array size must be greater than 0" }) }, ptr{ new int[size] {} } { /* empty body */ }

//copy constructor for class Array; //must receive a reference to an Array Array::Array(const Array& arrayToCopy) : size{ arrayToCopy.size }, ptr{ new int[size] } { for (size_t i{ 0 }; i < size; ++i) { ptr[i] = arrayToCopy.ptr[i]; //copy into object } }

//destructor for class Array Array::~Array() { delete[] ptr; //release pointer-based array space }

//return number of elements of Array size_t Array::getSize() const { return size; //number of elements in Array }

//overloaded assignment operator; //const return avoids: (a1 = a2) =a3 const Array& Array::operator=(const Array& right) { if (&right != this) { //avoid self-assignment //for Arrays of different sizes, deallocate original //left-side Array, then allocate new left-side Array if (size != right.size) { delete[] ptr; //release space size = right.size; //resize this object ptr = new int[size]; //create space for Array copy }

for (size_t i{ 0 }; i < size; ++i) { ptr[i] = right.ptr[i]; //copy array into object } }

return *this; //enables x=y=z, for example }

//determine if two Arrays are equal and //return true, otherwise return false bool Array::operator==(const Array& right) const { if (size != right.size) { return false; //arrays of different number of elements }

for (size_t i{ 0 }; i < size; ++i) { if (ptr[i] != right.ptr[i]) { return false; //Array contents are not equal } }

return true; //Arrays are equal }

//overloaded subscript operator for non-const Arrays; //reference return creates a modifiable lvalue int& Array::operator[](int subscript) { //check for subscript out-of-range error if (subscript < 0 || subscript >= size) { throw out_of_range{ "Subscript out of range" }; }

return ptr[subscript]; //reference return }

//overloaded subscript operator for const Arrays //const reference return creates an rvalue int Array::operator[](int subscript) const { //check for subscript out-of-range error if (subscript < 0 || subscript >= size) { throw out_of_range{ "Subscript out of range" }; }

return ptr[subscript]; //returns copy of this element }

//overloaded input operator for class Array; //inputs values for entire Array istream& operator>>(istream& input, Array& a) { for (size_t i{ 0 }; i < a.size; ++i) { input >> a.ptr[i]; }

return input; //enables cin >> x >> y; }

//overload output operator for class Array ostream& operator<<(ostream& output, const Array& a) { //output private ptr-based array for (size_t i{ 0 }; i < a.size; ++i) { output << a.ptr[i] << " "; }

output << endl; return output; //enables cout << x << y; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions