// ========================================================================== // $Id: copycontrol.cpp,v 1.5 2016/11/02 18:13:25 jlang Exp $ // CSI2372 example Code for lecture 12 // ========================================================================== // (C)opyright: // // Jochen Lang // SITE, University of Ottawa // 800 King Edward Ave. // Ottawa, On., K1N 6N5 // Canada. // http://www.site.uottawa.ca // // Creator: jlang (Jochen Lang) // Email: jlang@site.uottawa.ca // ========================================================================== // $Log: copycontrol.cpp,v $ // Revision 1.5 2016/11/02 18:13:25 jlang // Added stack example for copy control // // Revision 1.4 2016/10/17 16:28:31 jlang // Updated for showing effects of access modifiers and using declaration. // // Revision 1.3 2008/11/15 01:09:18 jlang // Base destructor should be virtual // // Revision 1.2 2008/11/15 01:04:17 jlang // Added alternative assignment of base // // Revision 1.1 2008/11/15 01:01:23 jlang // Example of assignment operator, copy constructor and destructor // overloading with class hierarchies // // // ========================================================================== #include #include using std::string; using std::cout; using std::endl; class Base { protected: string d_name; public: Base( const string& _name = string("Hello") ) : d_name(_name) {} // Just for printing Base( const Base& oB ) : d_name( oB.d_name ) { cout << "Base copy constructor!" << endl; } // Just for printing Base& operator=( const Base& oB ) { cout << "Base assignment operator!" << endl; // Check for self-assignment if ( this != &oB ) { d_name = oB.d_name; } } // get the name inline string getName() { return d_name; } // Just for printing virtual ~Base() { cout << "Base destructor!" << endl; } }; class Derived : private Base { int d_size; int* d_array; public: Derived( int _size ) : Base("Null"), d_size(_size) { d_array = new int[_size]; } // set all of the array to value Derived& operator=( int val ) { for ( int i=0; i(*this) = oD; delete [] d_array; d_size = oD.d_size; d_array = new int[d_size]; for ( int i=0; i