Chapter 9 - Worked Exercises 2
Exercise 1
Your task is to create a set of classes that will be useful to in creating a library cataloguing system.
Part 1
You are given the following class for Book that represents a physical book:
struct Book {
std::string name;
std::string author;
int release_year;
// To be implemented in Part 1
Book(std::string some_name, std::string some_author, int some_year);
std::string to_string();
};Complete the implementation for the constructor for the Book class.
Complete the implementation for the to_string() function, which returns a string in the following format:
<book_title>, <author_initials> (<release_year>)
Example
Book book("Harry Potter and the Philosopher's Stone", "Joanne Rowling", 1997);
std::cout << book.to_string() << std::endl;Prints:
Harry Potter and the Philosopher's Stone, J.R. (1997)
Part 2
You are given the following class for Bookshelf that represents a physical bookshelf:
struct Bookshelf {
const int capacity;
const std::string category;
std::vector<Book*> books;
// To be implemented in Part 2
bool addBook(std::string name, std::string author, int year);
std::vector<std::string> getAllBooks();
}The specifications for the Bookshelf class is as follows:
Each
Bookshelfhas a specified capacity, denoting the maximum number of books it can carry.Each
Bookshelfhas a specified category, denoting the category of the books it contains.Each
Bookshelfhas anstd::vectorofBook*s, representing the books that it contains.
Create a suitable constructor for the Bookshelf class.
Complete the addBook function, which takes in parameters corresponding to that of a book:
returns
trueif the book was successfully added to the bookshelf, andfalseotherwisea book can be added to the bookshelf if there is sufficient capacity to
you can assume that any books added using this function matches the bookshelf's given category
Complete the getAllBooks function which returns a vector of strings containing the string representations of its books, this is the Book.to_string() function you have made earlier.
Create a suitable destructor for the Bookshelf class, ensuring any Books that it has dynamically allocated will be cleaned up on the destruction of the Bookshelf object.
Example
Bookshelf bs(5);
bs.addBook("Harry Potter and the Philosopher's Stone", "Joanne Rowling", 1997);
bs.addBook("To Kill a Mockingbird", "Harper Lee", 1960);
bs.addBook("The Great Gatsby", "Scott Fitzgerald", 1925);
bs.addBook("1984", "George Orwell", 1984);
auto books = bs.getAllBooks();
for (auto book: books) {
std::cout << book << std::endl;
}Prints:
Harry Potter and the Philosopher's Stone, J.R. (1997)
To Kill a Mockingbird, H.L. (1960)
The Great Gatsby, S.F. (1925)
1984, G.O. (1984)Part 3
You are given the following Library class that represents a physical library:
struct Library {
const int capacity_per_bookshelf;
std::vector<Bookshelf*> bookshelves;
// To be implemented in Part 3
void addBook(std::string category, std::string name, std::string author, int year);
int getNumBookshelves();
std::vector<std::string> getAllBooks(std::string category);
};The specification for the Library class is as follows:
A
Librarymay contain zero to infinite number of bookshelves.Each bookshelf of the
Librarycan hold the same number of books.
Create a suitable constructor for the Library class.
Complete the addBook function, which takes in the same parameters as the addBook function in the Bookshelf class except with one extra parameter denoting the category that the book belongs to.
The function should add the book to the first bookshelf it finds with the matching category. If one does not exist, it will create one and add the book to it.
If the first bookshelf it finds is full, it will continue searching for the next available bookshelf. If none exists, it will create one and add the book to it.
Complete the getNumBookshelves function, which returns the number of bookshelves currently contained by the Library.
Complete the getAllBooks function, which accepts a string parameter denoting the category of books it wants to retrieve, and returns a vector of strings containing the string representation of all the books matching that category.
Create a suitable destructor for the Library class, that ensures that all Bookshelves dynamically allocated during its lifetime will be properly destroyed. Coupled with the destructor you have created in Part 2, this would also destroy all the books allocated by each Bookshelf.
Example
Library library(25); // 25 books per bookshelf in this library
std::cout << "There are currently " << library.getNumBookshelves() << " bookshelves" << std::endl;
library.addBook("fantasy", "Harry Potter and the Philosopher's Stone", "Joanne Rowling", 1997);
library.addBook("fantasy", "A Game of Thrones", "George Martin", 1996);
library.addBook("thriller", "To Kill a Mockingbird", "Harper Lee", 1960);
std::cout << "There are currently " << library.getNumBookshelves() << " bookshelves" << std::endl;
auto fantasy_books = library.getAllBooks("fantasy");
std::cout << "Fantasy books:" << std::endl;
for (auto book: fantasy_books) {
std::cout << book << std::endl;
}
auto thriller_books = library.getAllBooks("fantasy");
std::cout << "Thriller books:" << std::endl;
for (auto book: thriller_books) {
std::cout << book << std::endl;
}Prints:
There are currently 0 bookshelves.
There are currently 2 bookshelves.
Fantasy books:
Harry Potter and the Philosopher's Stone, J.R. (1997)
A Game of Thrones, G.M. (1996)
Thriller books:
To Kill a Mockingbird, H.L. (1960)Last updated
Was this helpful?