From cc16e9d094b54533d1c3ad659579bdd038a206a2 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Thu, 3 Jan 2019 08:43:17 +0100 Subject: [PATCH] =?UTF-8?q?10=5FPITF:=20Erste=20Pointer=20wieder=20zu=20sm?= =?UTF-8?q?art=20Pointern=20ge=C3=A4ndert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/10_PITF/Testat/Bank.cpp | 4 ++++ src/10_PITF/Testat/Bank.h | 1 + src/10_PITF/Testat/BankAccount.cpp | 20 +++++++++++++------- src/10_PITF/Testat/BankAccount.h | 8 +++++--- src/10_PITF/Testat/HeapObject.cpp | 1 - src/10_PITF/Testat/Person.cpp | 4 ++++ src/10_PITF/Testat/Person.h | 1 + src/10_PITF/Testat/main.cpp | 28 +++++++++++++--------------- 8 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/10_PITF/Testat/Bank.cpp b/src/10_PITF/Testat/Bank.cpp index bd2002e..0a72659 100644 --- a/src/10_PITF/Testat/Bank.cpp +++ b/src/10_PITF/Testat/Bank.cpp @@ -7,6 +7,10 @@ Bank::Bank(std::string name) : name(std::move(name)) { } +Bank::Bank(Bank *bank) : name(bank->getName()) { + +} + void Bank::addAccount(BankAccount * account) { accounts.insert(account); } diff --git a/src/10_PITF/Testat/Bank.h b/src/10_PITF/Testat/Bank.h index 3dde3e3..41319f8 100644 --- a/src/10_PITF/Testat/Bank.h +++ b/src/10_PITF/Testat/Bank.h @@ -12,6 +12,7 @@ private: std::set money; public: explicit Bank(std::string name); + explicit Bank(Bank* bank); void addAccount(BankAccount * account); void addMoney(Money * money); std::set getAccounts(); diff --git a/src/10_PITF/Testat/BankAccount.cpp b/src/10_PITF/Testat/BankAccount.cpp index bb970d7..14c5579 100644 --- a/src/10_PITF/Testat/BankAccount.cpp +++ b/src/10_PITF/Testat/BankAccount.cpp @@ -1,23 +1,29 @@ +#include + #include "BankAccount.h" #include "CashFactory.h" -BankAccount::BankAccount(Person * owner, std::string name) : name(std::move(name)) { -this->owner = owner; +BankAccount::BankAccount(std::shared_ptr owner, std::string name) : name(std::move(name)), owner(std::move(owner)) { + } -BankAccount * BankAccount::operator+(Money * money) { +BankAccount::BankAccount(BankAccount *bankAccount) : owner(std::move(bankAccount->getOwner())), name(std::move(bankAccount->getName())) { + +} + +BankAccount *BankAccount::operator+(Money *money) { BankAccount::money = new Money(money->getValue() + getMoney().getValue()); return this; } -Money * BankAccount::operator-(int value) { - auto * cashFactory = new CashFactory(); +Money *BankAccount::operator-(int value) { + auto *cashFactory = new CashFactory(); return cashFactory->printCoin(value, CurrencyValue::USD); } -bool BankAccount::operator<(BankAccount * bankAccount) { +bool BankAccount::operator<(BankAccount *bankAccount) { return money.getValue() < bankAccount->money.getValue(); } @@ -29,6 +35,6 @@ Money BankAccount::getMoney() { return money; } -Person *BankAccount::getOwner() { +std::shared_ptr BankAccount::getOwner() { return owner; } diff --git a/src/10_PITF/Testat/BankAccount.h b/src/10_PITF/Testat/BankAccount.h index c0432ad..52c3f3a 100644 --- a/src/10_PITF/Testat/BankAccount.h +++ b/src/10_PITF/Testat/BankAccount.h @@ -2,6 +2,7 @@ #define C_C_BANKACCOUNTS_H #include +#include #include "Money.h" #include "Cash.h" @@ -10,15 +11,16 @@ class BankAccount { private: std::string name; - Person * owner; + std::shared_ptr owner; Money money; public: - explicit BankAccount(Person * owner, std::string name); + explicit BankAccount(std::shared_ptr owner, std::string name); + explicit BankAccount(BankAccount* bankAccount); BankAccount* operator+(Money * money); Money* operator-(int value); bool operator<(BankAccount * bankAccount); std::string getName(); - Person * getOwner(); + std::shared_ptr getOwner(); Money getMoney(); }; diff --git a/src/10_PITF/Testat/HeapObject.cpp b/src/10_PITF/Testat/HeapObject.cpp index 080327e..99eab40 100644 --- a/src/10_PITF/Testat/HeapObject.cpp +++ b/src/10_PITF/Testat/HeapObject.cpp @@ -22,7 +22,6 @@ HeapObject::~HeapObject(){ bool HeapObject::assertionsHold(){ assert(ctorCount == newCount); // all objects have been allocated on heap - std::cout << ctorCount << " == " << dtorCount << std::endl; assert(ctorCount == dtorCount); // all objects have been deleted return true; } diff --git a/src/10_PITF/Testat/Person.cpp b/src/10_PITF/Testat/Person.cpp index c33c5e1..166df44 100644 --- a/src/10_PITF/Testat/Person.cpp +++ b/src/10_PITF/Testat/Person.cpp @@ -6,6 +6,10 @@ Person::Person(std::string name) : name(std::move(name)){ } +Person::Person(Person *person) : name(std::move(person->getName())) { + +} + std::string Person::getName() { return name; } \ No newline at end of file diff --git a/src/10_PITF/Testat/Person.h b/src/10_PITF/Testat/Person.h index ffd9656..a919dd1 100644 --- a/src/10_PITF/Testat/Person.h +++ b/src/10_PITF/Testat/Person.h @@ -11,6 +11,7 @@ private: std::vector money; public: explicit Person(std::string name); + explicit Person(Person* person); std::string getName(); }; diff --git a/src/10_PITF/Testat/main.cpp b/src/10_PITF/Testat/main.cpp index 8bbd9e5..8d1d876 100644 --- a/src/10_PITF/Testat/main.cpp +++ b/src/10_PITF/Testat/main.cpp @@ -1,5 +1,5 @@ - #include +#include #include #include #include @@ -47,18 +47,17 @@ std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) { return os; } -std::set banks; +std::set> banks; void setup() { + std::unique_ptr bank1 = std::make_unique(new Bank("Bank 1")); + std::unique_ptr bank2 = std::make_unique(new Bank("Bank 2")); + std::unique_ptr bank3 = std::make_unique(new Bank("Bank 3")); - Bank *bank1 = new Bank("Bank 1"); - Bank *bank2 = new Bank("Bank 2"); - Bank *bank3 = new Bank("Bank 3"); - - Person *max = new Person("Max Müller"); - Person *marius = new Person("Marius Müller"); - Person *alex = new Person("Alex Maier"); + std::shared_ptr max = std::make_shared(new Person("Max Müller")); + std::shared_ptr marius = std::make_shared(new Person("Marius Müller")); + std::shared_ptr alex = std::make_shared(new Person("Alex Maier")); bank1->addAccount(new BankAccount(max, "Konto 1")); @@ -71,15 +70,15 @@ void setup() { bank1->addAccount(new BankAccount(marius, "Konto 2")); bank3->addAccount(new BankAccount(marius, "Konto 3")); - banks.insert(bank1); - banks.insert(bank2); - banks.insert(bank3); + banks.insert(std::move(bank1)); + banks.insert(std::move(bank2)); + banks.insert(std::move(bank3)); } void simulate() { auto *cashFactory = new CashFactory(); - for (Bank *bank : banks) { + for (auto && bank : banks) { std::cout << std::endl; std::cout << "======================== " << bank->getName() << " ========================" << std::endl; std::cout << std::endl; @@ -103,11 +102,10 @@ void simulate() { } void tearDown() { - for (Bank *bank : banks) { + for (auto && bank : banks) { for (BankAccount *bankAccount : bank->getAccounts()) { delete(bankAccount); } - delete(bank); } }