From 93def2de2991282bac9e320b209657f76cb31177 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Mon, 28 Nov 2022 11:24:47 +0100 Subject: week 4 klaar? --- oop2w4/MijnKlasse.cpp | 20 ++++++++++++++++++++ oop2w4/MijnKlasse.h | 13 +++++++++++++ oop2w4/main.cpp | 15 +++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 oop2w4/MijnKlasse.cpp create mode 100644 oop2w4/MijnKlasse.h diff --git a/oop2w4/MijnKlasse.cpp b/oop2w4/MijnKlasse.cpp new file mode 100644 index 0000000..55f8af6 --- /dev/null +++ b/oop2w4/MijnKlasse.cpp @@ -0,0 +1,20 @@ +#include + +#include "MijnKlasse.h" + +MijnKlasse::MijnKlasse(int n) { + _n = n; + _thread = new std::thread(this->showNum, this->_n); +} + +MijnKlasse::~MijnKlasse() { + if (_thread != nullptr) { + _thread->join(); + delete _thread; + _thread = nullptr; + } +} + +void MijnKlasse::showNum(int n) { + std::cout << std::endl << n << " from thread " << std::this_thread::get_id(); +} diff --git a/oop2w4/MijnKlasse.h b/oop2w4/MijnKlasse.h new file mode 100644 index 0000000..f8b74de --- /dev/null +++ b/oop2w4/MijnKlasse.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +class MijnKlasse { + private: + std::thread* _thread = nullptr; + int _n = 0; + public: + MijnKlasse(int n); + ~MijnKlasse(); + static void showNum(int n); +}; diff --git a/oop2w4/main.cpp b/oop2w4/main.cpp index e69de29..ad3f2cd 100644 --- a/oop2w4/main.cpp +++ b/oop2w4/main.cpp @@ -0,0 +1,15 @@ +#include +#include + +#include "MijnKlasse.h" + +int main() { + MijnKlasse* instanties[100]; + + for (int n = 0; n < 100; n++) + instanties[n] = new MijnKlasse(n); + for (int n = 0; n < 100; n++) + delete instanties[n]; + + std::cout << std::endl << "Ready" << std::endl; +} -- cgit v1.2.3