Help, please, deal with operator overloading. How to write an overload for an assignment operator (=) and an addition operator (+) to this code fragment. It is necessary that the commented construction works correctly.
#include <iostream> #include <cstdlib> #include <ctime> class V { private: int dimension; double* ptrdata = 0; public: V(int _dimension = 1): dimension(_dimension){} V(const V &obj) : dimension(obj.dimension){} ~V(){ delete [] ptrdata; } void Fill(); void Show() const; }; int main() { V obj1(10); obj1.Fill(); obj1.Show(); V obj2(10); obj2.Fill(); obj2.Show(); V obj3(10); // obj3 = obj1 + obj2; // obj3.Show(); return 0; } void V::Fill() { srand((unsigned)time(NULL)); ptrdata = new double [dimension]; for (int i = 0; i < dimension; i++) { ptrdata[i] = rand() % 10 + 1; } } void V::Show() const { for (int i = 0; i < dimension; i++) std::cout << ptrdata[i] << " "; std::cout << std::endl; }
dimension
equality for the terms), and then perform the assignment. Only you still have the copy constructor (like the second one) not working - you copied the dimension, and the data? ... That's about the way you need to assign it - copy the dimension and transfer the data ... - Harry