Created
December 11, 2016 19:12
-
-
Save l3kn/214d62edfa34b118077597ad9f240a6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
class Book | |
{ | |
string m_title; | |
string m_author; | |
string m_isbn; | |
public: | |
Book(string title, string author, string isbn) | |
{ | |
m_title = title; | |
m_author = author; | |
m_isbn = isbn; | |
} | |
~Book() | |
{ | |
// TODO: | |
// Hier sollte noch irgendwas sinnvolles passieren, | |
// glaub ich | |
} | |
// ... | |
void setTitle(string title); | |
void setAuthor(string author); | |
void setISBN(string isbn); | |
string getTitle() { return m_title; } | |
string getAuthor() { return m_author; } | |
string getISBN() { return m_isbn; } | |
void print() { | |
cout << m_title << endl; | |
cout << m_author << endl; | |
cout << m_isbn << endl; | |
} | |
}; | |
class Student | |
{ | |
// array der geliehenen Buecher | |
Book* m_book[8]; | |
// Anzahl der aktuell geliehenen Bücher | |
int number_books = 0; | |
public: | |
Student() | |
{ | |
// ... | |
for (int i = 0; i < number_books; i++) | |
{ | |
m_book[i] = NULL; | |
} | |
} | |
~Student() | |
{ | |
// ... | |
for (int i = 0; i < number_books; i++) | |
{ | |
// TODO: hier ist noch irgendwas kaputt | |
/* delete m_book[i]; */ | |
} | |
} | |
void addBook(Book& book) | |
{ | |
if (number_books >= 8) { | |
cout << "Es wurden schon zu viele Bücher ausgeliehen"; | |
} else { | |
m_book[number_books] = &book; | |
number_books += 1; | |
} | |
} | |
void removeBook(Book& book) | |
{ | |
bool found = false; | |
for (int i = 0; i < number_books; i++) | |
{ | |
if (m_book[i] == &book) { | |
found = true; | |
// Falls es sich um das letzte Buch handelt, | |
// können wir es einfach löschen | |
// und die Anzahl der Bücher dekrementieren | |
if (i == (number_books - 1)) { | |
/* delete m_book[i]; */ | |
m_book[i] = NULL; | |
--number_books; | |
// Ansonsten müssen wir die entstandene Lücke | |
// in der Liste füllen, z.B. indem wir das | |
// letzte Buch der Liste dorthin verschieben | |
} else { | |
/* delete m_book[i]; */ | |
m_book[i] = m_book[number_books - 1]; | |
m_book[number_books - 1] = NULL; | |
--number_books; | |
} | |
// Abbrechen damit, | |
// falls der Student mehrmals das gleiche Buch geliehen hat, | |
// nur eine Kopie entfernt wird | |
break; | |
} | |
} | |
if (!found) { | |
cout << "Das zu löschende Buch konnte nicht gefunden werden"; | |
} | |
} | |
void printBooks() | |
{ | |
for (int i = 0; i < number_books; i++) | |
{ | |
m_book[i]->print(); | |
} | |
} | |
}; | |
/** This is the entry point of the program. */ | |
int main(int argc, char* argv[]) | |
{ | |
cout << "----------------\n";; | |
Book book_1("Introduction to Algorithms", | |
"Th. Cormen, Ch. Leiserson, R. Rivest", | |
"0262531968"); | |
Book book_2("The C++ Programming Language", | |
"Bjarne Stroustrup", | |
"0201700735"); | |
Book book_3("Grundkurs Informatik", | |
"H. Ernst", | |
"3528257172"); | |
Student student; | |
student.addBook(book_1); | |
student.addBook(book_2); | |
student.addBook(book_3); | |
student.printBooks(); | |
cout << "----------------\n";; | |
student.removeBook(book_2); | |
student.printBooks(); | |
cout << "----------------\n";; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment