A community for students. Sign up today!
Here's the question you clicked on:
 0 viewing
 2 years ago
C++ question.
I need help writing a function (operator+) that concatenates arrays for a class. More info inside.
 2 years ago
C++ question. I need help writing a function (operator+) that concatenates arrays for a class. More info inside.

This Question is Closed

nphuongsun93
 2 years ago
Best ResponseYou've already chosen the best response.0template <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; } }
Ask your own question
Ask a QuestionFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.