Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help!. I tried implementing those 3 classes of code below, but at the mom

ID: 3849782 • Letter: P

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

#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 <string>
#include <sstream>
#include <stdexcept>

// There is no need to change this!

using namespace std;

template <class T>
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<T> &);
~SmartArray();
SmartArray& operator=(const SmartArray<T> &other);

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

int size() const;

SmartArray<T> operator+(const SmartArray<T> &) const;
bool operator==(const SmartArray<T> &) const;
int count(T) const;

string getAsString() const;

private:

SmartArray();

T* data;
int array_size;
};

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

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

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

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


template <class T>
SmartArray<T>::SmartArray(const SmartArray<T> &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 <class T>
int SmartArray<T>::size() const{
return array_size;
}


template <class T>
string SmartArray<T>::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 <class T>
SmartArray<T>::~SmartArray(){
delete[] data;
data = NULL;
}


template <class T>
T& SmartArray<T>::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

Explanation / Answer

int i;

for(i=0;i<size2;i++)

a[i+size1] = a2[i];

template <class T>

bool operator==(const SmartArray<T> &a) {

if(this.size() == a.size(){

int i;

for(i=0;i<this.size();i++){

if(this[i] != a[i])

return false;

}

return true;

}

return false;

}

int count(T x){

int i;

int count_number= 0;

for(i=0;i<this.size();i++){

if(this[i] == x)

count_number++;

}

return count_number;

}