| static-members/alternative/point.cc - Implementation of class Point. | Lecture 4 - slide 25 : 40 Program 6 |
#include <cmath>
#include <iostream>
#include "point.h"
Point::Point(double x_coord, double y_coord): x(x_coord), y(y_coord){
}
Point::Point(): x(defaultPoint().x), y(defaultPoint().y) {
}
double Point::getx () const{
return x;
}
Point& Point::defaultPoint(){ // Returns a reference to
static Point pt(5,7); // local static variable
return pt;
}
double Point::gety () const{
return y;
}
void Point::move(double dx, double dy){
x += dx; y += dy;
}
double Point::distance_to(Point p) const{
return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}
Point Point::origo(){
return Point(0,0);
}
std::ostream& operator<<(std::ostream& s, const Point& p){
return s << "(" << p.getx() << "," << p.gety() << ")" ;
}