summaryrefslogtreecommitdiff
path: root/graphs/cpp/exception/player.cc
diff options
context:
space:
mode:
Diffstat (limited to 'graphs/cpp/exception/player.cc')
-rw-r--r--graphs/cpp/exception/player.cc34
1 files changed, 34 insertions, 0 deletions
diff --git a/graphs/cpp/exception/player.cc b/graphs/cpp/exception/player.cc
new file mode 100644
index 0000000..4a0b378
--- /dev/null
+++ b/graphs/cpp/exception/player.cc
@@ -0,0 +1,34 @@
+#include "player.hh"
+
+Player::Player(const std::string& name, int age, int guess)
+ : name_(name)
+ , age_(age)
+ , guess_(guess)
+{
+ if (name.empty())
+ throw InvalidArgumentException{ "Name can't be empty." };
+ if (age < 0)
+ throw InvalidArgumentException{
+ "Well, it seems you are not born yet."
+ };
+ if (age > 150)
+ throw InvalidArgumentException{ "Sorry gramp, too old to play." };
+}
+
+int Player::get_guess() const
+{
+ return guess_;
+}
+bool Player::operator==(const Player& p) const
+{
+ return this == &p;
+}
+bool Player::operator!=(const Player& p) const
+{
+ return this != &p;
+}
+
+std::ostream& operator<<(std::ostream& os, const Player& p)
+{
+ return os << p.name_ << '(' << p.age_ << ')';
+}