Question: Please help!. I tried implementing those 3 classes of code below, but at the moment I am stuck, I am confused on what to do

Please help!. I tried implementing those 3 classes of code below, but at the moment I am stuck, I am confused on what to do about implementing those 3 classes of code below. Please put me through.

Objective:

The objective of this homework is to write a templated class which contains operator overloading, exceptions, which allows for the safe use of arrays..

Task 1: Implement the templated SmartArray class!

Your task is to extend the SmartArray class done in class. Specifically, implement the following functions:

Operator+ : Given two SmartArrays, return a new SmartArray with the elements of the two concatenated together.

operator== : Given two SmartArrays, return true if the two are the same length and contain the same data elements.

count(T) : Given some value, return the number of times that element exists in the SmartArray

Task 2: Add tests to validate the 3 new functions above. In total you should have at least 3 tests per class member function.

#ifndef SMART_ARRAY_H #define SMART_ARRAY_H

/* @file SmartArray.cpp @author < Fill Me In > @date < Fill Me In >

@description Implements a class for an array with bounds checking. Implementatoin is below the declaration in this same file. */

#include #include #include

// There is no need to change this!

using namespace std;

template class SmartArray{ public: SmartArray(int); // Defines how large the array is SmartArray(int, T); // Defines how large the array is and the initial value SmartArray(const SmartArray &); ~SmartArray(); SmartArray& operator=(const SmartArray &other);

T& operator[](int); // Set data in array

int size() const; SmartArray operator+(const SmartArray &) const; bool operator==(const SmartArray &) const; int count(T) const; string getAsString() const; private: SmartArray(); T* data; int array_size; };

/************** Now the implementation ******************************/

template ostream& operator<<(ostream &o, const SmartArray &r){ o << r.getAsString(); return o; }

template SmartArray::SmartArray(int size){ array_size = size; data = new T[size]; for(int i = 0; i < size; i++){ data[i] = T(); } }

template SmartArray::SmartArray(int size, T thing){ array_size = size; data = new T[size]; for(int i = 0; i < size; i++){ data[i] = thing; } }

template SmartArray::SmartArray(const SmartArray &other){ array_size = other.array_size; data = new T[array_size]; for(int i = 0; i < array_size; i++){ data[i] = other.data[i]; } }

template int SmartArray::size() const{ return array_size; }

template string SmartArray::getAsString() const{ stringstream s; s << "["; for(int i = 0; i < array_size; i++){ s << data[i]; if(array_size - 1 != i){ s << ","; } } s << "]"; return s.str(); }

template SmartArray::~SmartArray(){ delete[] data; data = NULL; }

template T& SmartArray::operator[](int location){ //Set data in array if(location >= array_size || location < -array_size){ throw logic_error("Out of bounds"); } if(location < 0){ location = location + array_size; } return data[location];

#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!