// File: dmc2.cpp // // Implementation file of dmc.h // With copy constructor and assignment operator ; #include #include "dmc2.h" using namespace std ; int DMC::count = 0 ; const int DMC_SIZE = 10 ; // Default Constructor DMC::DMC() { ++count ; number=count ; cout << "Creating DMC Object #" << number << endl ; ptr = new int[DMC_SIZE] ; } // Copy Constructor DMC::DMC(const DMC& Y) { number = ++count ; cout << "\n\nIn Copy Constructor, creating DMC Object #" << number << endl ; ptr = new int[DMC_SIZE] ; for (int i = 0 ; i < DMC_SIZE ; i++) { ptr[i] = Y.ptr[i] ; } } // Destructor DMC::~DMC() { cout << "Destroying DMC Object #" << number << endl ; delete [] ptr ; } const DMC operator+(const DMC& X, const DMC& Y) { return DMC() ; } // Assignment DMC& DMC::operator=(const DMC& rhs) { cout << "\nIn DMC assignment operator host=#" << number << " rhs=#" << rhs.number << endl ; if (this != &rhs) { // check for self assignment delete [] ptr ; // free up old space ptr = new int[DMC_SIZE] ; // get new space // Copy rhs info to host // for(int i=0 ; i < DMC_SIZE ; i++) { ptr[i] = rhs.ptr[i] ; } } return *this ; // return host object } // Set value void DMC::Set(int n) { for (int i = 0 ; i < DMC_SIZE ; i++) { ptr[i] = n+i ; } } // Output operator ostream& operator<< (ostream& os, const DMC& X) { os << "[" ; for (int i = 0 ; i < DMC_SIZE ; i++) { os << X.ptr[i] ; if (i < DMC_SIZE - 1) { os << ", " ; } } os << "]" ; return os ; }