From 12ddb5d6c640b605e41ee8787b5617b665a69dfe Mon Sep 17 00:00:00 2001 From: Unknown <prinsij@mcmaster.ca> Date: Wed, 21 Sep 2016 19:53:14 -0400 Subject: [PATCH] Implement Coord.h --- src/coord.cpp | 26 ++++++++++++++++++++++++++ src/include/coord.h | 5 +++-- 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/coord.cpp diff --git a/src/coord.cpp b/src/coord.cpp new file mode 100644 index 0000000..c04f2cc --- /dev/null +++ b/src/coord.cpp @@ -0,0 +1,26 @@ +#include "include/coord.h" +#include <string> + +Coord::Coord(int x, int y) + : x(x) + , y(y) + {} + +int& Coord::operator[](int dimension) { + switch (dimension) { + case 0: + return this->x; + case 1: + return this->y; + } + throw "bad dimension " + std::to_string(dimension); + return this->x; +} + +Coord Coord::operator+(const Coord& other) { + return Coord(this->x + other.x, this->y + other.y); +} + +Coord Coord::operator-(const Coord& other) { + return Coord(this->x - other.x, this->y - other.y); +} diff --git a/src/include/coord.h b/src/include/coord.h index 77f141a..29d6bf8 100644 --- a/src/include/coord.h +++ b/src/include/coord.h @@ -4,9 +4,10 @@ class Coord { public: + Coord(int, int); int& operator[](int); - Coord operator+(Coord); - Coord operator-(Coord); + Coord operator+(const Coord&); + Coord operator-(const Coord&); private: int x; -- GitLab