Library Management System
Last Updated :
29 Aug, 2025
The Library Management System is a console-based C++ application that demonstrates effective use of the Standard Template Library (STL). It simulates a basic library system where users can manage books - add, remove, search, update, and display them in a sorted manner.
The project is structured to provide a real-world application of Object-Oriented Programming in STL containers and algorithms, making it an excellent learning experience for beginners in C++.
Features
Our Library System provides the following key functionalities:
- Add new books to library (tile, author, ISBN (International Standard Book Number), copies).
- Remove a book by ISBN
- Search for a book by title or ISBN
- Update book details if ISBN already exists
- Display all books sorted alphabetically by title
- Menu-driven dashboard for easy user interaction
C++ Concepts and STL features
This project integrates the following C++ and STL components:
- Classes and Objects for data abstraction.
- Access Specifiers, Constructors, Methods
- STL Containers: std::vector, std::map
- STL Algorithms: std::sort, std::find_if, std::remove_if
- Formatted I/O using iomanip
- Modular Function Design, Conditionals, Loops
- Dynamic memory management
Project Requirements
To compile and run the project, we will need:
- C++ compiler: g++, clang++, or an IDE (Code::Blocks, VS Code, Dev C++)
- C++ 17 or later (for consistent STL behavior)
- Basic understanding of C++ classes, STL containers, and functions.
Implementation Steps
Step 1: Define the Book Class
This class represents each book in the library. It stores data like title, author, ISBN, and number of copies. It also provides methods to access or modify this data.
C++
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Book {
private:
string title;
string author;
string isbn;
int copies;
public:
// Constructor to initialize book data
Book(string t, string a, string i, int c)
: title(t), author(a), isbn(i), copies(c) {}
// Getter methods to access book details
string getTitle() const { return title; }
string getAuthor() const { return author; }
string getISBN() const { return isbn; }
int getCopies() const { return copies; }
// Setter methods to update book details
void setTitle(std::string t) { title = t; }
void setAuthor(std::string a) { author = a; }
void setCopies(int c) { copies = c; }
// Display book details in a formatted way
void display() const {
cout << left << std::setw(25) << title
<< setw(20) << author
<< setw(15) << isbn
<< setw(10) << copies << "\n";
}
};
- setw from <iomanip> is used to align the output.
- const after method names means they don't modify the object.
- Getter and setter methods allow safe access and updates to private data.
Step 2: Define the Library Class Using STL
This class will manage a collection of books using std::vector<Book>. It handles adding, removing, searching, and displaying books.
The functions inside the Library Class
- Add or update book based on ISBN
C++
void addBook(const Book& book) {
auto it = find_if(books.begin(), books.end(), [&](Book& b) {
return b.getISBN() == book.getISBN();
});
if (it != books.end()) {
cout << "Book with same ISBN exists. Updating details.\n";
it->setTitle(book.getTitle());
it->setAuthor(book.getAuthor());
it->setCopies(book.getCopies());
} else {
books.push_back(book);
cout << "Book added successfully.\n";
}
}
- Checks if a book with the same ISBN already exists in the library.
- If it exists, updates the existing book's title, author, and copy count
- If not, adds the new book to the collection.
2. Remove book by ISBN
C++
void removeBook(const string& isbn) {
auto it = remove_if(books.begin(), books.end(), [&](Book& b) {
return b.getISBN() == isbn;
});
if (it != books.end()) {
books.erase(it, books.end());
cout << "Book removed successfully.\n";
} else {
cout << "No book found with that ISBN.\n";
}
}
- Searches for books matching the given ISBN.
- Removes all books with that ISBN from the library.
- Prints a success message if removed, otherwise indicates no book was found.
3.Search for book by title
C++
void searchByTitle(const string& title) {
bool found = false;
for (auto& b : books) {
if (b.getTitle() == title) {
b.display();
found = true;
}
}
if (!found)
cout << "No book found with that title.\n";
}
- Iterates through all books to find matches by title.
- Displays all books matching the given title.
- Prints a message if no books are found with that title.
4. Search for book by ISBN
C++
void searchByISBN(const string& isbn) {
auto it = find_if(books.begin(), books.end(), [&](Book& b) {
return b.getISBN() == isbn;
});
if (it != books.end())
it->display();
else
cout << "No book found with that ISBN.\n";
}
- Searches for a book with the exact ISBN using find_if.
- Displays the book if found.
- Prints a message if no book matches the ISBN.
5.Display all books sorted by title
C++
void displayBooks() {
if (books.empty()) {
cout << "Library is empty.\n";
return;
}
sort(books.begin(), books.end(), [](Book& a, Book& b) {
return a.getTitle() < b.getTitle();
});
cout << left << setw(25) << "Title"
<< setw(20) << "Author"
<< setw(15) << "ISBN"
<< setw(10) << "Copies" << "\n";
cout << string(70, '-') << "\n";
for (const auto& b : books) {
b.display();
}
}
- Checks if the library is empty and prints a message if so.
- Sorts the book collection by title in ascending order.
- Displays all books in a formatted table showing title, author, ISBN, and copies.
Complete Library Class Code
C++
#include <vector>
#include <algorithm>
using namespace std;
class Library {
private:
vector<Book> books; // STL container to store Book objects
public:
// Add or update book based on ISBN
void addBook(const Book& book) {
auto it = find_if(books.begin(), books.end(), [&]( Book& b) {
return b.getISBN() == book.getISBN();
});
if (it != books.end()) {
cout << "Book with same ISBN exists. Updating details.\n";
it->setTitle(book.getTitle());
it->setAuthor(book.getAuthor());
it->setCopies(book.getCopies());
} else {
books.push_back(book);
cout << "Book added successfully.\n";
}
}
// Remove book by ISBN
void removeBook(const string& isbn) {
auto it = remove_if(books.begin(), books.end(), [&]( Book& b) {
return b.getISBN() == isbn;
});
if (it != books.end()) {
books.erase(it, books.end());
cout << "Book removed successfully.\n";
} else {
cout << "No book found with that ISBN.\n";
}
}
// Search for book by title
void searchByTitle(const string& title) {
bool found = false;
for (const auto& b : books) {
if (b.getTitle() == title) {
b.display();
found = true;
}
}
if (!found)
cout << "No book found with that title.\n";
}
// Search for book by ISBN
void searchByISBN(const string& isbn) {
auto it = find_if(books.begin(), books.end(), [&](Book& b) {
return b.getISBN() == isbn;
});
if (it != books.end())
it->display();
else
cout << "No book found with that ISBN.\n";
}
// Display all books sorted by title
void displayBooks() {
if (books.empty()) {
cout << "Library is empty.\n";
return;
}
sort(books.begin(), books.end(), [](Book& a, Book& b) {
return a.getTitle() < b.getTitle();
});
cout << left << setw(25) << "Title"
<< setw(20) << "Author"
<< setw(15) << "ISBN"
<< setw(10) << "Copies" << "\n";
cout << string(70, '-') << "\n";
for (auto& b : books) {
b.display();
}
}
};
Step 3: Menu-Driven Interface in main()
A loop-based menu that continuously prompts the user to perform actions on the library system.
C++
int main() {
Library lib;
int choice;
do {
cout << "\n====== Mini Library Management System ======\n";
cout << "1. Add Book\n";
cout << "2. Remove Book by ISBN\n";
cout << "3. Search Book by Title\n";
cout << "4. Search Book by ISBN\n";
cout << "5. Display All Books (Sorted by Title)\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // Clear newline from input buffer
string title, author, isbn;
int copies;
switch (choice) {
case 1:
cout << "Enter Title: ";
getline(cin, title);
cout << "Enter Author: ";
getline(cin, author);
cout << "Enter ISBN: ";
getline(cin, isbn);
cout << "Enter Number of Copies: ";
cin >> copies;
lib.addBook(Book(title, author, isbn, copies));
break;
case 2:
cout << "Enter ISBN to remove: ";
getline(cin, isbn);
lib.removeBook(isbn);
break;
case 3:
cout << "Enter Title to search: ";
getline(cin, title);
lib.searchByTitle(title);
break;
case 4:
cout << "Enter ISBN to search: ";
getline(cin, isbn);
lib.searchByISBN(isbn);
break;
case 5:
lib.displayBooks();
break;
case 6:
cout << "Exiting the program.\n";
break;
default:
cout << "Invalid choice. Try again.\n";
}
} while (choice != 6);
return 0;
}
Expected Output:
Summary
The mini Library Management System project provides a complete application of:
- OOP Principles
- STL containers and algorithms
- Handle user input and formatted output
- Implement a real-world scenario with clean and maintainable code.
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems