CSC112 Spring 2016 Examples
#include "point.h"123Point::Point()4{5this->_x = this->_y = 0;6}78Point::Point(double _x, double _y)9{10this->_x = _x;11this->_y = _y;12}1314//overloaded methods for accessing x and y15double16Point::x()17{18return _x;19}2021void22Point::x(double val)23{24_x = val;25}2627double28Point::y()29{30return _y;31}3233void34Point::y(double val)35{36_y = val;37}383940//overloaded addition and subtraction operators41Point42Point::operator+(Point &rhs)43{44Point result;4546result._x = _x + rhs._x;47result._y = _y + rhs._y;4849return result;50}5152Point &53Point::operator+=(Point &rhs)54{55_x += rhs._x;56_y += rhs._y;5758return *this; //return a reference to me59}6061Point62Point::operator-(Point &rhs)63{64Point result;6566result._x = _x - rhs._x;67result._y = _y - rhs._y;6869return result;7071}7273Point &74Point::operator-=(Point &rhs)75{76_x -= rhs._x;77_y -= rhs._y;7879return *this; //return a reference to me80}818283std::ostream &84operator<<(std::ostream &os, Point &rhs)85{86//write to the stream87os << "(" << rhs.x() << ", " << rhs.y() << ")";8889//return the stream90return os;91}92939495