Here is an improved version of the implementation of class Point. Notice the improved copy constructor. In this version, it copies the point representation.#include <cmath>
#include <iostream>
#include "point.h"
Point::Point(){
point_representation = new double[2];
point_representation[0] = 0.0;
point_representation[1] = 0.0;
}
Point::Point(double x_coord, double y_coord){
point_representation = new double[2];
point_representation[0] = x_coord;
point_representation[1] = y_coord;
}
Point::Point(Point& p){ // Better copy constructor
point_representation = new double[2];
point_representation[0] = p.getx();
point_representation[1] = p.gety();
}
Point::~Point(){
std::cout << "Deleting point" << "(" << getx() << "," << gety() << ")" << std::endl;
delete[] point_representation;
}
double Point::getx () const{
return point_representation[0];
}
double Point::gety () const{
return point_representation[1];
}
void Point::move(double dx, double dy){
point_representation[0] += dx;
point_representation[1] += dy;
}
std::ostream& operator<<(std::ostream& s, const Point& p){
return s << "(" << p.getx() << "," << p.gety() << ")" ;
}