What Is A COPY CONSTRUCTOR And When Is It Called

What Is A COPY CONSTRUCTOR And When Is It Called


  • 11 Dec, 2011

    A copy constructor is a method that accepts an object of the same class and copies it?s data members to the object on the left part of assignement:class Point2D{int x; int y;public int color;protected bool pinned;public Point2D() : x(0) , y(0) {} //default (no argument) constructorpublic Point2D( const Point2D & ) ;};Point2D::Point2D( const Point2D & p ){this->x = p.x;this->y = p.y;this->color = p.color;this->pinned = p.pinned;}main(){Point2D MyPoint;MyPoint.color = 345;Point2D AnotherPoint = Point2D( MyPoint ); // now AnotherPoint has color = 345

    Comments Received:
    Please give your suggestions and feedback:

2009-2016 downloadmela.com. All rights reserved.