Skip to content

Instantly share code, notes, and snippets.

@mariusmg2
Last active August 29, 2015 14:08
Show Gist options
  • Save mariusmg2/f00c54af13f392d31221 to your computer and use it in GitHub Desktop.
Save mariusmg2/f00c54af13f392d31221 to your computer and use it in GitHub Desktop.
Solution to POO Lab test.
#include <iostream>
#include <string>
#include <cstdlib>
using std::cout;
using std::cin;
using std::string;
using std::endl;
class Book {
private:
string author, title;
bool rented;
public:
Book(string &author, string &title, bool rented) :
author(author), title(title), rented(rented) { }
inline string getAuthor(void) { return author; };
inline string getTitle(void) { return title; };
inline bool getStatus(void) { return rented ? true : false; };
};
class TehnicBook:public Book {
private:
int amount, releaseYear;
string language;
TehnicBook *head, *next;
public:
TehnicBook(string &author, string &title, bool rented, int amount, string &language, int releaseYear) :
Book(author, title, rented), amount(amount), releaseYear(releaseYear), language(language), head(NULL) { }
~TehnicBook(void) {
delete[] head;
};
void display(void);
void add(void);
void dellete(string&);
};
class Literature:public Book {
string bookType;
Literature *head, *next;
public:
Literature(string &author, string &title, bool rented, string &bookType):Book(author, title, rented) {
head = NULL;
this -> bookType = bookType;
};
~Literature(void) {
delete[] head;
};
void display(void);
void add(void);
};
void TehnicBook::add(void) {
string author, title, language;
int year, amount;
bool rented;
cout << endl << "Author: ", cin.ignore(), getline(cin, author);
cout << "Title: ", getline(cin, title);
cout << "Rented? (0/1): ", cin >> rented;
cout << "The amount of books: ", cin >> amount;
cout << "Language: ", cin.ignore(), getline(cin, language);
cout << "Release year: ", cin >> year;
TehnicBook *p = new TehnicBook(author, title, rented, amount, language, year);
p -> next = head;
head = p;
}
void TehnicBook::display(void) {
TehnicBook *p = head;
while(p) {
cout << "-----------------------------\n";
cout << "Author: " << p->getAuthor() << endl;
cout << "Title: " << p->getTitle() << endl;
cout << "Is " << ((p->getStatus()) ? "" : "not ") << "rented" << endl;
cout << "Amount of books: " << p -> amount << endl;
cout << "Language: " << p -> language << endl;
cout << "Release year: " << p -> releaseYear << endl;
cout << endl;
p = p -> next;
}
}
void Literature::add(void) {
string author, title, bookType;
bool rented;
cout << "\nAuthor: ", cin.ignore(), getline(cin, author);
cout << "Title: ", getline(cin, title);
cout << "Is rented? ", cin >> rented;
cout << "Book type (hardcover/...: ", cin.ignore(), getline(cin, bookType);
Literature *p = new Literature(author, title, rented, bookType);
p -> next = head;
head = p;
}
void Literature::display(void) {
Literature *p = head;
while(p) {
cout << "\n-----------------------------\n";
cout << "Author: " << p->getAuthor() << endl;
cout << "Title: " << p->getTitle() << endl;
cout << "Is rented? " << ((p->getStatus()) ? "yes" : "no") << endl;
cout << "Book type: " << p -> bookType << endl << endl;
p = p -> next;
}
}
int main(int argc, char const **argv) {
string blank = "";
TehnicBook *tehnicB = new TehnicBook(blank, blank, false, 0, blank, 0);
Literature *litB = new Literature(blank, blank, false, blank);
int opt;
for(;;) {
cout << "\n\n1) Add a tehnic book.\n";
cout << "2) Display all tehnic books.\n";
cout << "3) Add a literature book.\n";
cout << "4) Display all literature books.\n";
cout << "5) Exit.\n\n";
cout << "Your option: ", cin >> opt;
switch(opt) {
case 1:
tehnicB->add();
break;
case 2:
tehnicB->display();
break;
case 3:
litB->add();
break;
case 4:
litB->display();
break;
case 5:
exit(0);
default:
continue;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment