π§© C++ programming tutorial for beginners Β· Class 11 Β· Lesson 18 of 18 Β· Free
Capstone: Library Management System in C++
β± About 50 minutes Β· βΆ run real C++ here β nothing to install Β· no sign-in
In this lesson you will
- Design Book, Member and Library classes with clear jobs
- Write issue and return rules with limits and late fines
- Save the catalogue to a file and drive the program with typed commands
Your school library still uses a register: who took which book, and when. Books go missing, fines are guessed, and nobody knows what is on the shelf. In this capstone you build the core of a library system in C++, using classes, vectors, maps, files and input from Lessons 1β17.
How do you plan a library management system in C++?
Start with the nouns: a library has books and members, and members borrow books. Each noun becomes a class. The verbs (add, search, issue, return) become member functions.
Step 1 is the Book class. Each book has an id, a title, an author and a flag that says if it is on the shelf. The Library keeps the books in a vector (Lesson 12).
Keep data private and give the class functions that do the work. Then the rest of the program cannot break the rules by mistake. Try it: add a fourth book and run again.
101 Wings of Fire by A. P. J. Abdul Kalam [on shelf] 102 Malgudi Days by R. K. Narayan [on shelf] 103 Godaan by Premchand [on shelf] 3 books
How do you search for a book in C++ by id or title?
Searching by id returns a pointer to the book, or nullptr if there is none. The caller must check before using it. (std::optional from Lesson 10 is another good choice.)
Searching by title should ignore capital letters, because people type "godaan" or "GODAAN". We turn both texts into lower case before comparing, and use find to allow part of a title.
Try it: search for "days" and then for "Kalam" (that one fails: we only search titles).
Id 102: Malgudi Days Id 999: not found Search "FIRE": found 101: Wings of Fire
How do you issue a book with rules in C++?
Real libraries have rules: the book must exist, it must be on the shelf, and a student may hold at most 2 books. The issue function checks each rule in order.
Instead of printing inside the Library class, issue() returns a result code. An enum class is a type with a fixed list of named values, like Result::Ok. The main program decides what message to show: the values count 0, 1, 2, 3, so static_cast<int> turns a result into an index for the message list.
Members are kept in a map from roll number to the ids they hold (Lesson 5). Try it: change the limit to 3 and run again.
Roll 7, book 101: issued Roll 9, book 101: already issued Roll 7, book 102: issued Roll 7, book 103: limit of 2 books reached Roll 9, book 555: no such book
How do you calculate a late fine for a library book in C++?
Our school's rule: a book may be kept for 14 days. After that the fine is Rs 2 per extra day, but never more than Rs 100.
Put a rule like this in its own small function. It is easy to test with many values, and easy to change when the rule changes.
The loop below tests the edges: exactly 14 days, 15 days, and a very late book. Testing edge values is how you catch off-by-one mistakes. Try it: change the cap to Rs 50.
Kept 3 days: fine Rs 0 Kept 14 days: fine Rs 0 Kept 15 days: fine Rs 2 Kept 20 days: fine Rs 12 Kept 64 days: fine Rs 100 Kept 200 days: fine Rs 100
How do you save the library to a file and load it back in C++?
Without a file, every issued book is forgotten when the program ends. We save one book per line, with the parts separated by | (titles can contain spaces and commas, but not |).
Loading reads each line with getline, then splits it with getline(ss, part, '|') like the CSV example in Lesson 11. stoi is avoided on purpose: in standard C++ it throws for bad text, so we read numbers with >> instead.
The program below saves two books, then loads them into a fresh vector to prove the data survived.
101 Wings of Fire (on shelf) 102 Malgudi Days (issued)
How do you add a command menu to a C++ program?
The user types commands like "issue 102" or "list". The program reads the first word with cin >> cmd, then reads the extra numbers that command needs.
A loop keeps reading until the user types quit (or the input ends). Unknown commands get a friendly message instead of a crash.
The commands in the input box are typed for you. Try it: add a command "return 101" before quit and run again.
> list 101 Wings of Fire 102 Malgudi Days > issue 102 OK: Malgudi Days issued > issue 102 Cannot issue Malgudi Days now > list 101 Wings of Fire 102 Malgudi Days (issued) > fly Commands: list, issue <id>, return <id>, quit > quit Bye
π€ Predict the output
Think like C++: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
struct Book { string title; bool available; };
vector<Book> books = {{"Godaan", true}, {"Gitanjali", false}, {"Malgudi Days", true}};
int onShelf = count_if(books.begin(), books.end(), [](const Book& b) { return b.available; });
cout << onShelf << " of " << books.size() << "\n";Question 2. What does this print?
auto fine = [](int days) {
int late = days - 14;
if (late <= 0) return 0;
return min(late * 2, 100);
};
cout << fine(14) << " " << fine(20) << " " << fine(90) << "\n";Question 3. Careful: [] on a map creates a missing key. What does this print?
map<int, vector<int>> borrowed; borrowed[7].push_back(101); borrowed[7].push_back(103); borrowed[9].push_back(102); cout << borrowed.size() << " " << borrowed[7].size() << " " << borrowed[5].size() << " " << borrowed.size() << "\n";
π§© Your challenge
Finish the library. issueBook already works. Write returnBook(id, daysKept): if the book exists and is issued, put it back on the shelf and return the late fine (14 free days, then Rs 2 per day, at most Rs 100); otherwise return -1. Then write report(), which prints how many books are on the shelf and how many are issued. Expected output: Issue 102: OK Issue 101: OK Return 102 after 20 days: fine Rs 12 Return 103 after 5 days: fine Rs -1 On shelf: 2, issued: 1
Issue 102: OK Issue 101: OK Return 102 after 20 days: fine Rs 12 Return 103 after 5 days: fine Rs -1 On shelf: 2, issued: 1
β οΈ Common mistakes (and how to fix them)
β Wrong
struct Book { int id; bool available; };
vector<Book> books = {{101, true}, {102, true}};
for (Book b : books) {
if (b.id == 102) b.available = false;
}
cout << "102 available? " << books[1].available << "\n";Compiles and runs, but prints: 102 available? 1 β
β Right
struct Book { int id; bool available; };
vector<Book> books = {{101, true}, {102, true}};
for (Book& b : books) {
if (b.id == 102) b.available = false;
}
cout << "102 available? " << books[1].available << "\n";Book b is a copy, so the book in the vector is never issued. Use Book& b when the loop must change the real objects.
β Wrong
#include <iostream>
#include <string>
using namespace std;
class Book {
string title = "Godaan";
public:
string getTitle() { return title; }
};
void show(const Book& b) {
cout << b.getTitle() << "\n";
}
int main() {
Book b;
show(b);
return 0;
}
Line 12: error: 'this' argument to member function 'getTitle' has type 'const Book', but function is not marked const
β Right
#include <iostream>
#include <string>
using namespace std;
class Book {
string title = "Godaan";
public:
string getTitle() const { return title; }
};
void show(const Book& b) {
cout << b.getTitle() << "\n";
}
int main() {
Book b;
show(b);
return 0;
}
Through a const reference you may only call const member functions. Mark functions that only read data as const.
β Wrong
vector<string> titles = {"Wings of Fire", "Godaan"};
string wanted = "wings of fire";
bool found = false;
for (const string& t : titles) {
if (t == wanted) found = true;
}
cout << (found ? "Found" : "Not found") << "\n";Compiles and runs, but prints: Not found β
β Right
vector<string> titles = {"Wings of Fire", "Godaan"};
string wanted = "wings of fire";
auto lower = [](string s) {
for (char& c : s) c = static_cast<char>(tolower(static_cast<unsigned char>(c)));
return s;
};
bool found = false;
for (const string& t : titles) {
if (lower(t) == lower(wanted)) found = true;
}
cout << (found ? "Found" : "Not found") << "\n";== on strings is case-sensitive, so "wings" is not "Wings". Convert both to lower case before comparing user searches.
π Remember
- Plan with nouns (classes) and verbs (member functions) before coding.
- Keep data inside classes and let functions enforce the rules.
- Return a pointer, optional or result code for "not found" and check it.
- Put rules like fines in small functions and test the edge values.
- Save to a file with a clear separator and load it back with getline.
- Read commands in a loop and handle unknown input politely.
Finished the quiz and the challenge?
Questions students ask
How do I make a library management system project in C++?
Start with a Book class and a Library class that holds a vector of books. Add search, issue and return functions one at a time, testing each. Then add members, fines, a file to save data, and a command menu.
Which C++ concepts does a library management system use?
Classes and objects, constructors, vectors and maps, references, const member functions, file streams and input loops. It is a good way to practise the whole course in one program.
How can I extend this project further?
Add due dates using real dates, a search by author, a list of overdue books, a password for the librarian, or separate files for books and members. Each feature is a new function plus a new command.
Should a real library app store data in a text file?
Small tools can. Larger systems use a database, which handles many users at once and fast searching. You meet databases and SQL in the Class 12 course.