10_PITF: Erste Pointer wieder zu smart Pointern geändert

This commit is contained in:
Johannes Theiner 2019-01-03 08:43:17 +01:00
parent 5c46cc3d66
commit cc16e9d094
8 changed files with 41 additions and 26 deletions

View File

@ -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) { void Bank::addAccount(BankAccount * account) {
accounts.insert(account); accounts.insert(account);
} }

View File

@ -12,6 +12,7 @@ private:
std::set<Money *> money; std::set<Money *> money;
public: public:
explicit Bank(std::string name); explicit Bank(std::string name);
explicit Bank(Bank* bank);
void addAccount(BankAccount * account); void addAccount(BankAccount * account);
void addMoney(Money * money); void addMoney(Money * money);
std::set<BankAccount *> getAccounts(); std::set<BankAccount *> getAccounts();

View File

@ -1,8 +1,14 @@
#include <utility>
#include "BankAccount.h" #include "BankAccount.h"
#include "CashFactory.h" #include "CashFactory.h"
BankAccount::BankAccount(Person * owner, std::string name) : name(std::move(name)) { BankAccount::BankAccount(std::shared_ptr<Person> owner, std::string name) : name(std::move(name)), owner(std::move(owner)) {
this->owner = owner;
}
BankAccount::BankAccount(BankAccount *bankAccount) : owner(std::move(bankAccount->getOwner())), name(std::move(bankAccount->getName())) {
} }
BankAccount *BankAccount::operator+(Money *money) { BankAccount *BankAccount::operator+(Money *money) {
@ -29,6 +35,6 @@ Money BankAccount::getMoney() {
return money; return money;
} }
Person *BankAccount::getOwner() { std::shared_ptr<Person> BankAccount::getOwner() {
return owner; return owner;
} }

View File

@ -2,6 +2,7 @@
#define C_C_BANKACCOUNTS_H #define C_C_BANKACCOUNTS_H
#include <unordered_map> #include <unordered_map>
#include <memory>
#include "Money.h" #include "Money.h"
#include "Cash.h" #include "Cash.h"
@ -10,15 +11,16 @@
class BankAccount { class BankAccount {
private: private:
std::string name; std::string name;
Person * owner; std::shared_ptr<Person> owner;
Money money; Money money;
public: public:
explicit BankAccount(Person * owner, std::string name); explicit BankAccount(std::shared_ptr<Person> owner, std::string name);
explicit BankAccount(BankAccount* bankAccount);
BankAccount* operator+(Money * money); BankAccount* operator+(Money * money);
Money* operator-(int value); Money* operator-(int value);
bool operator<(BankAccount * bankAccount); bool operator<(BankAccount * bankAccount);
std::string getName(); std::string getName();
Person * getOwner(); std::shared_ptr<Person> getOwner();
Money getMoney(); Money getMoney();
}; };

View File

@ -22,7 +22,6 @@ HeapObject::~HeapObject(){
bool HeapObject::assertionsHold(){ bool HeapObject::assertionsHold(){
assert(ctorCount == newCount); // all objects have been allocated on heap assert(ctorCount == newCount); // all objects have been allocated on heap
std::cout << ctorCount << " == " << dtorCount << std::endl;
assert(ctorCount == dtorCount); // all objects have been deleted assert(ctorCount == dtorCount); // all objects have been deleted
return true; return true;
} }

View File

@ -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() { std::string Person::getName() {
return name; return name;
} }

View File

@ -11,6 +11,7 @@ private:
std::vector<Money *> money; std::vector<Money *> money;
public: public:
explicit Person(std::string name); explicit Person(std::string name);
explicit Person(Person* person);
std::string getName(); std::string getName();
}; };

View File

@ -1,5 +1,5 @@
#include <iostream> #include <iostream>
#include <memory>
#include <set> #include <set>
#include <random> #include <random>
#include <ctime> #include <ctime>
@ -47,18 +47,17 @@ std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) {
return os; return os;
} }
std::set<Bank *> banks; std::set<std::unique_ptr<Bank>> banks;
void setup() { void setup() {
std::unique_ptr<Bank> bank1 = std::make_unique<Bank>(new Bank("Bank 1"));
std::unique_ptr<Bank> bank2 = std::make_unique<Bank>(new Bank("Bank 2"));
std::unique_ptr<Bank> bank3 = std::make_unique<Bank>(new Bank("Bank 3"));
Bank *bank1 = new Bank("Bank 1"); std::shared_ptr<Person> max = std::make_shared<Person>(new Person("Max Müller"));
Bank *bank2 = new Bank("Bank 2"); std::shared_ptr<Person> marius = std::make_shared<Person>(new Person("Marius Müller"));
Bank *bank3 = new Bank("Bank 3"); std::shared_ptr<Person> alex = std::make_shared<Person>(new Person("Alex Maier"));
Person *max = new Person("Max Müller");
Person *marius = new Person("Marius Müller");
Person *alex = new Person("Alex Maier");
bank1->addAccount(new BankAccount(max, "Konto 1")); bank1->addAccount(new BankAccount(max, "Konto 1"));
@ -71,15 +70,15 @@ void setup() {
bank1->addAccount(new BankAccount(marius, "Konto 2")); bank1->addAccount(new BankAccount(marius, "Konto 2"));
bank3->addAccount(new BankAccount(marius, "Konto 3")); bank3->addAccount(new BankAccount(marius, "Konto 3"));
banks.insert(bank1); banks.insert(std::move(bank1));
banks.insert(bank2); banks.insert(std::move(bank2));
banks.insert(bank3); banks.insert(std::move(bank3));
} }
void simulate() { void simulate() {
auto *cashFactory = new CashFactory(); auto *cashFactory = new CashFactory();
for (Bank *bank : banks) { for (auto && bank : banks) {
std::cout << std::endl; std::cout << std::endl;
std::cout << "======================== " << bank->getName() << " ========================" << std::endl; std::cout << "======================== " << bank->getName() << " ========================" << std::endl;
std::cout << std::endl; std::cout << std::endl;
@ -103,11 +102,10 @@ void simulate() {
} }
void tearDown() { void tearDown() {
for (Bank *bank : banks) { for (auto && bank : banks) {
for (BankAccount *bankAccount : bank->getAccounts()) { for (BankAccount *bankAccount : bank->getAccounts()) {
delete(bankAccount); delete(bankAccount);
} }
delete(bank);
} }
} }