diff options
Diffstat (limited to 'oop1w4')
-rw-r--r-- | oop1w4/Persoon.cpp | 22 | ||||
-rw-r--r-- | oop1w4/Persoon.h | 16 | ||||
-rw-r--r-- | oop1w4/Spel.cpp | 29 | ||||
-rw-r--r-- | oop1w4/Spel.h | 18 | ||||
-rw-r--r-- | oop1w4/main.cpp | 26 | ||||
l--------- | oop1w4/makefile | 1 |
6 files changed, 112 insertions, 0 deletions
diff --git a/oop1w4/Persoon.cpp b/oop1w4/Persoon.cpp new file mode 100644 index 0000000..de5195d --- /dev/null +++ b/oop1w4/Persoon.cpp @@ -0,0 +1,22 @@ +#include "Persoon.h" + +bool Persoon::operator < ( const Persoon& persoon ) const { + return naam < persoon.naam; +} + +Persoon::Persoon(std::string naam) { + this->naam = naam; +} + +const std::string& Persoon::getNaam() const { + return naam; +} + +int Persoon::getLeeftijd() const { + return leeftijd; +} + +void Persoon::setLeeftijd(int leeftijd) { + this->leeftijd = leeftijd; +} + diff --git a/oop1w4/Persoon.h b/oop1w4/Persoon.h new file mode 100644 index 0000000..df4a3f2 --- /dev/null +++ b/oop1w4/Persoon.h @@ -0,0 +1,16 @@ +#pragma once + +#include <string> + +class Persoon { + public: + Persoon(std::string naam); + const std::string& getNaam() const; + int getLeeftijd() const; + void setLeeftijd(int leeftijd); + bool operator < (const Persoon& other) const; + + private: + std::string naam; + int leeftijd; +}; diff --git a/oop1w4/Spel.cpp b/oop1w4/Spel.cpp new file mode 100644 index 0000000..5ddc355 --- /dev/null +++ b/oop1w4/Spel.cpp @@ -0,0 +1,29 @@ +#include "Spel.h" + +Persoon* Spel::getPersoon(const std::string naam) const { + auto iFind = scores.find(naam); + if (iFind == scores.end()) + return nullptr; + else + return const_cast<Persoon*>(&iFind->first); +} + +void Spel::setScore(std::string naam, int score) { + scores[naam] = score; +} + +void Spel::addScore(std::string naam, int score) { + scores[naam] += score; +} + +int Spel::getScore(std::string naam) const { + auto iFind = scores.find(naam); + if (iFind == scores.end()) + return 0; + else + return iFind->second; +} + +std::map<Persoon,int>& Spel::getScores() { + return scores; +} diff --git a/oop1w4/Spel.h b/oop1w4/Spel.h new file mode 100644 index 0000000..9ed04ba --- /dev/null +++ b/oop1w4/Spel.h @@ -0,0 +1,18 @@ +#pragma once + +#include <string> +#include <map> + +#include "Persoon.h" + +class Spel { + public: + void setScore(std::string naam, int score); + void addScore(std::string naam, int score); + int getScore(std::string naam) const; + Persoon* getPersoon(std::string naam) const; + std::map<Persoon,int>& getScores(); + + private: + std::map<Persoon, int> scores; +}; diff --git a/oop1w4/main.cpp b/oop1w4/main.cpp new file mode 100644 index 0000000..b81a711 --- /dev/null +++ b/oop1w4/main.cpp @@ -0,0 +1,26 @@ +#include "Spel.h"
+#include "Persoon.h"
+
+#include <iostream>
+
+int main()
+{
+ Spel spel;
+ spel.setScore("Bert", 15);
+ std::cout << spel.getScore("Bert") << std::endl; // uikomst: 15
+ Persoon *bert = spel.getPersoon("Bert");
+ bert->setLeeftijd(103);
+ spel.addScore("Bert", 16);
+ std::cout << spel.getScore("Bert") << std::endl; // uikomst: 31
+ bert = spel.getPersoon("Bert");
+ std::cout << bert->getLeeftijd() << std::endl; // uikomst: 103
+ spel.setScore("Joan", 512);
+ spel.setScore("Arthur", 1024);
+ for ( auto pair : spel.getScores() )
+ {
+ std::cout << pair.first.getNaam() << "\t"
+ << pair.first.getLeeftijd() << "\t"
+ << pair.second << std::endl;
+ }
+ return 0;
+}
diff --git a/oop1w4/makefile b/oop1w4/makefile new file mode 120000 index 0000000..a4e84c6 --- /dev/null +++ b/oop1w4/makefile @@ -0,0 +1 @@ +../week.mk
\ No newline at end of file |