Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          application-operator-overload/vers2/point.cc - Definition of then Point application operator.Lecture 6 - slide 21 : 40
Program 5

// Definition of class Point members - including the call operator that accesses x or y.

#include <cmath>
#include <iostream>
#include "point.h"

Point::Point(): x{0.0}, y{0.0}{
}

Point::Point(double x_coord): x{x_coord}, y{0}{
}


Point::Point(double x_coord, double y_coord): x{x_coord}, y{y_coord}{
}


double Point::getx () const{
  return x;
}

double Point::gety () const{
  return y;
}

void Point::move(double dx, double dy){
    x += dx; y += dy;
}

double Point::distance_to(Point p){
    return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}

double Point::operator()(int i) const{
  if (i == 1)
    return x;
  else if (i == 2)
    return y;
  else throw IndexProblem();  
}

std::ostream& operator<<(std::ostream& s, const Point& p){
  return s << "(" << p.getx() << "," << p.gety() << ")" ;
}