// constr3.cpp #include #include #include using namespace std; class Point { int x; int y; char *name; //a c-string public: Point(char * ); Point(const Point&); //copy constructor ~Point(){delete [] name;} //destructor Point& operator=(const Point&); //copy assignment operator friend ostream& operator<<(ostream& cout, const Point& p) { cout << p.name << ' ' << p.x << ' ' << p.y ; return cout; } }; Point::Point(char *nn): x(0), y(0) { // a default constructor name = new char[1+strlen(nn)]; strcpy(name, nn); } Point::Point(const Point& p): x(0), y(0){ // copy constructor name = new char[1+strlen(p.name)]; strcpy(name, p.name); } Point& Point::operator=(const Point& rhs) { if (this != &rhs) { //this is of type Point&*; &rhs is the address of rhs x = rhs.x; y = rhs.y; if (name != 0) { delete [] name; name = new char[1+strlen(rhs.name)]; strcpy(name, rhs.name); } } return *this; //*this is of type Point& } void print(Point p) { cout << p << endl; } int main () { Point a("rose"); print(a); Point b("violet"); b = a; cout << b << endl; return 0; }