From e274b6b21832c8788a3fd6f36495618521506434 Mon Sep 17 00:00:00 2001 From: Matti Date: Mon, 11 Nov 2024 21:37:51 +0100 Subject: [PATCH] Update Demo-Cases --- Main.cpp | 99 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 70 insertions(+), 29 deletions(-) diff --git a/Main.cpp b/Main.cpp index b1e6219..7e39a70 100644 --- a/Main.cpp +++ b/Main.cpp @@ -106,6 +106,12 @@ void addContact(Kontaktdaten* person, std::vector *Telefonbuch) { // buch.push_back(person); } +void deleteAllContacts(std::vector *Telefonbuch) { + for (Kontaktdaten* pointer: *Telefonbuch) { + delete pointer; + } +} + void printAllContacts(std::vector *Telefonbuch) { for (Kontaktdaten * personpointer: *Telefonbuch) { Kontaktdaten person = *personpointer; @@ -179,31 +185,10 @@ void readContactsFromFile(std::vector *Telefonbuch, std::string * } } - infile.close(); } -/* -Kontaktdaten * createContact(std::string firstName, std::string lastName, int number) { - Kontaktdaten person = new Kontaktdaten; - - person.firstName = firstName; - person.lastName = lastName; - person.number = number; - - return & person; -} -*/ - -void TelefonbuchMain() { - std::string fileName = "../IO-Files/Telefonbuch.txt"; - std::vector Telefonbuch; - - for (int i=0; i<1; i++) { - readContactsFromFile(&Telefonbuch, &fileName); - std::cout << "Done with reading " << i << std::endl; - } - +void addFirstSetOfContacts(std::vector * Telefonbuch) { auto * Robin = new Kontaktdaten; auto * Saxo = new Kontaktdaten; auto * Jesus = new Kontaktdaten; @@ -220,6 +205,53 @@ void TelefonbuchMain() { Jesus->firstName = "Jesus"; Jesus->lastName = "Nazarett"; + addContact(Robin, Telefonbuch); + addContact(Saxo, Telefonbuch); + addContact(Jesus, Telefonbuch); +} + +void addSecondSetOfContacts(std::vector * Telefonbuch) { + auto * Bruno = new Kontaktdaten; + auto * Peter = new Kontaktdaten; + auto * Ralf = new Kontaktdaten; + + Bruno->number = 462581; + Bruno->firstName = "Bruno"; + Bruno->lastName = "Braunbär"; + + Peter->number = 110; + Peter->firstName = "Peter"; + Peter->lastName = "Polizei"; + + Ralf->number = 9999; + Ralf->firstName = "Ralf"; + Ralf->lastName = "Rotbart"; + + addContact(Bruno, Telefonbuch); + addContact(Peter, Telefonbuch); + addContact(Ralf, Telefonbuch); +} +/* +Kontaktdaten * createContact(std::string firstName, std::string lastName, int number) { + Kontaktdaten person = new Kontaktdaten; + + person.firstName = firstName; + person.lastName = lastName; + person.number = number; + + return & person; +} +*/ + +void TelefonbuchMain() { + std::string fileName = "../IO-Files/phonebook.thorsten"; + std::vector Telefonbuch; + + for (int i=0; i<1; i++) { + readContactsFromFile(&Telefonbuch, &fileName); + std::cout << "Done with reading " << i << std::endl; + } + /* *Robin->number = 123456; Robin->firstName = "Robin der Neue"; @@ -234,17 +266,26 @@ void TelefonbuchMain() { Jesus->lastName = "Nazarett"; */ - addContact(Robin, &Telefonbuch); - addContact(Saxo, &Telefonbuch); - addContact(Jesus, &Telefonbuch); + // addFirstSetOfContacts(&Telefonbuch); + addSecondSetOfContacts(&Telefonbuch); // printAllContacts(&Telefonbuch); saveAllContacts(&Telefonbuch, &fileName); + deleteAllContacts(&Telefonbuch); - delete Robin; - delete Saxo; - delete Jesus; -} + /* + * IDE beschwert sich bei Erstellen von * Robin über leaked Memory + * deleteAllContacts() sollte den Speicher befreien + * + * Zugriff auf *Robin nach delete Statement führt zu Fehler? + * -> IDE ist zu dumm? + */ + + // + // std::cout << Robin << std::endl; + // auto test = *Robin; + // std::cout << test.firstName << std::endl; + } #pragma endregion