From ffae2cb77bfb3263146a2de0deedf6528d4df461 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Wed, 9 Nov 2022 11:48:12 +0100 Subject: repository aanpassen voor oop2 --- oop1w4/Persoon.cpp | 22 ++++++++++++++++++++++ oop1w4/Persoon.h | 16 ++++++++++++++++ oop1w4/Spel.cpp | 29 +++++++++++++++++++++++++++++ oop1w4/Spel.h | 18 ++++++++++++++++++ oop1w4/main.cpp | 26 ++++++++++++++++++++++++++ oop1w4/makefile | 1 + 6 files changed, 112 insertions(+) create mode 100644 oop1w4/Persoon.cpp create mode 100644 oop1w4/Persoon.h create mode 100644 oop1w4/Spel.cpp create mode 100644 oop1w4/Spel.h create mode 100644 oop1w4/main.cpp create mode 120000 oop1w4/makefile (limited to 'oop1w4') 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 + +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(&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& 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 +#include + +#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& getScores(); + + private: + std::map 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 + +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 -- cgit v1.2.3