// ========================================================================== // $Id: cctor.cpp,v 1.2 2013/10/09 15:10:09 jlang Exp $ // CSI2372 example Code for lecture 6 // ========================================================================== // (C)opyright: // // Jochen Lang // EECS, University of Ottawa // 800 King Edward Ave. // Ottawa, On., K1N 6N5 // Canada. // http://www.eecs.uottawa.ca // // Creator: jlang (Jochen Lang) // Email: jlang@eecs.uottawa.ca // ========================================================================== // $Log: cctor.cpp,v $ // Revision 1.2 2013/10/09 15:10:09 jlang // Updated examples for lecture on Oct. 9. // // Revision 1.1 2013/10/07 22:53:48 jlang // Synthesized copy cctor takes a const & // // ========================================================================== #include // #define WITH_OWN_CCTOR // #define WITH_NON_CONST_CCTOR class Point2D { protected: double d_x; double d_y; public: Point2D( double _x=0.0, double _y=0.0 ); // Copy constructor from const - same as synthesized #ifdef WITH_OWN_CCTOR Point2D( const Point2D& _oPoint ); #endif // Copy constructor - NOT the same as synthesized // but will still delete the synthesized cctor #ifdef WITH_NON_CONST_CCTOR Point2D( Point2D& _oPoint ); #endif void print() const; }; Point2D::Point2D( double _x, double _y ) : d_x(_x), d_y(_y) {} #ifdef WITH_OWN_CCTOR Point2D::Point2D( const Point2D& _oPoint ) : d_x(_oPoint.d_x), d_y(_oPoint.d_y) { std::cout<< "const CCtor" << std::endl; } #endif #ifdef WITH_NON_CONST_CCTOR Point2D::Point2D( Point2D& _oPoint ) : d_x(_oPoint.d_x), d_y(_oPoint.d_y) { std::cout<< "CCtor" << std::endl; } #endif void Point2D::print() const { std::cout << "( " << d_x << ", " << d_y << " )"; return; } int main() { const Point2D pA( 3.0, 2.0 ); std::cout << "const pA :"; pA.print(); std::cout << std::endl; Point2D pB( pA ); std::cout << "pB (constructed from const pA):"; pB.print(); std::cout << std::endl << std::endl; Point2D pC( 5.0, 4.0 ); std::cout << "pC :"; pC.print(); std::cout << std::endl; Point2D pD( pC ); std::cout << "pD (constructed from pC):"; pD.print(); std::cout << std::endl; return 0; }