Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

nphuongsun93

  • 3 years ago

C++ question. I need help writing a function (operator+) that concatenates arrays for a class. More info inside.

  • This Question is Closed
  1. nphuongsun93
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    template <class T> class CArray { public: // constructors CArray(unsigned size = DEFAULT_SIZE, T element = static_cast<T>(0)); CArray(const CArray<T>& ar); // destructor ~CArray() { if(m_array != nullptr) delete[] m_array; } unsigned getSize() const { return m_size; } void print(ostream& sout, char delimiter = ' ') const; // operators CArray<T> operator=(const CArray<T>& array); T& operator[](unsigned index); const T& operator[](unsigned index) const; CArray<T> operator*=(unsigned scalar); CArray<T> operator*(unsigned scalar) const; CArray<T> operator+(const CArray<T>& array) const; CArray<T> operator+=(const CArray<T>& array); static const int DEFAULT_SIZE = 10; private: // properties T* m_array; unsigned m_size; }; template <class T> ostream & operator<<(ostream & sout, const CArray<T> &arrray); So this is the class. This is what I have for operator+. template <class T> CArray<T> CArray<T>::operator+ (const CArray<T>& array) const { CArray<T> temp(*this); temp.m_size = temp.m_size + array.m_size; for (unsigned i = 0; i < array.m_size; i++) { temp.m_array[m_size+i] = array.m_array[i]; } return temp; } template <class T> CArray<T> CArray<T>::operator+=(const CArray<T>& array) { return *this = *this + array; } }

  2. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy