Library class
- C++ course
- Intermediate
- Lesson 66/116
- 6 min
Set up collection management with the Library class.
Library class
The Library class manages the book collection. It holds a std::vector<Book> books member. The addBook(const Book&) method adds a new book, and count() returns the number of books.
We take the previous lesson's Book class and add the Library class on top. std::vector provides a dynamic array and push_back appends to the end. The const reference parameter passes the book without copying. This preserves performance for large objects.
addBook and count
The addBook method uses push_back to append a new book to the vector. The count method returns the number of books via books.size(). Both are declared in the public section.
Example: lib.addBook(Book("Tutunamayanlar", "Oğuz Atay")); adds a book. lib.count() reads the count. The const reference parameter avoids the copy cost. With this design the Library class supports adding and counting books; in the next lessons we will add checkout and search.
Practice Add the Library class
Take the previous lesson's Book class. Below it add a Library class: in the private section std::vector<Book> books and std::vector<bool> borrowed; in the public section addBook(const Book&), count() and getBook(int) methods. In main add two books and print the count.
Starter code
#include <iostream>
#include <string>
#include <vector>
class Book {
private:
std::string title;
std::string author;
public:
Book(std::string t, std::string a) : title(t), author(a) {}
std::string getTitle() const { return title; }
std::string getAuthor() const { return author; }
};
// Define the Library class here
int main() {
// Create a Library, add two books, print the count
return 0;
}Sample solution
#include <iostream>
#include <string>
#include <vector>
class Book {
private:
std::string title;
std::string author;
public:
Book(std::string t, std::string a) : title(t), author(a) {}
std::string getTitle() const { return title; }
std::string getAuthor() const { return author; }
};
class Library {
private:
std::vector<Book> books;
std::vector<bool> borrowed;
public:
void addBook(const Book& b) { books.push_back(b); borrowed.push_back(false); }
size_t count() const { return books.size(); }
Book getBook(int idx) const { return books[idx]; }
};
int main() {
Library lib;
lib.addBook(Book("Tutunamayanlar", "Oğuz Atay"));
lib.addBook(Book("İnce Memed", "Yaşar Kemal"));
std::cout << lib.count();
return 0;
}Expected output
2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Declare the vector member
class Library {
private:
std::____<Book> books;
};
class Library {
private:
std::vector<Book> books;
};
Explanation: std::vector<Book> provides a dynamic array.
2 What does std::vector<Book> books; do?
std::vector<Book> books;- Holds a single Book
- Defines the Book class
- Defines a Book pointer
- Holds a collection of Books Correct answer
Explanation: vector<Book> holds multiple books in sequence.
3 Match each member to its role
| Term | Meaning |
|---|---|
std::vector<Book> books |
Collection member |
addBook(const Book&) |
Add method |
books.push_back(b) |
Append to end |
books.size() |
Read count |