Building a Simple Library Management System in C++
Overview
In this post, we’ll explore a simple Library Management System developed in C++ that enables basic library functions such as adding, searching, and listing books as well as issuing and returning books. This system demonstrates key object-oriented programming (OOP) concepts, making it an ideal mini-project for students and those looking to enhance their coding skills.
Project Features
The Library Management System comprises two user roles:
- Admin: Responsible for managing books in the library.
- Student: Can search, issue, and return books.
Key functionalities include:
- Adding books with attributes like title, author, pages, and price.
- Listing all books or books by a specific author.
- Searching for books by title.
- Issuing and returning books, tracked by an
isIssued
status.
Code Walkthrough
1. Book Class
The Book
class encapsulates the properties of a book, including its title, author, number of pages, price, and issuance status.
class Book {
public:
string title;
string author;
int pages;
float price;
bool isIssued;
Book(string t = “”, string a = “”, int p = 0, float pr = 0.0)
: title(t), author(a), pages(p), price(pr), isIssued(false) {}
};
2. Library Class
The Library
class acts as a container for Book
objects, providing methods to manage the books:
- addBook(): Adds a new book to the library.
- displayBooks(): Displays details of all books.
- searchBook(): Searches for a book by its title.
- listBooksByAuthor(): Lists books by a particular author.
- issueBook(): Marks a book as issued.
- returnBook(): Marks a book as returned.
class Library {
private:
static const int MAX_BOOKS = 100;
Book books[MAX_BOOKS];
int count;
public:
Library() : count(0) {}
void addBook(const Book& book) {
if (count < MAX_BOOKS) {
books[count] = book;
count++;
cout << “Book added successfully!” << endl;
} else {
cout << “Library is full, cannot add more books.” << endl;
}
}
void displayBooks() {
if (count == 0) {
cout << “No books in the library.” << endl;
return;
}
for (int i = 0; i < count; i++) {
cout << “Title: ” << books[i].title
<< “, Author: ” << books[i].author
<< “, Pages: ” << books[i].pages
<< “, Price: $” << books[i].price
<< “, Issued: ” << (books[i].isIssued ? “Yes” : “No”) << endl;
}
}
// Other methods for search, issue, return, etc., omitted for brevity.
};
3. User Menus
To enhance usability, the system provides a menu-based interface for both admin and student users, allowing interaction with the library.
void adminMenu(Library& library) {
// Admin menu logic for adding, displaying, and managing books.
}
void studentMenu(Library& library) {
// Student menu logic for searching, issuing, and returning books.
}
int main() {
Library library;
int userType;
do {
cout << “Welcome to the Library Management System” << endl;
cout << “Select User Type: ” << endl;
cout << “1. Admin” << endl;
cout << “2. Student” << endl;
cout << “3. Exit” << endl;
cin >> userType;
switch (userType) {
case 1:
adminMenu(library);
break;
case 2:
studentMenu(library);
break;
case 3:
cout << “Exiting the system…” << endl;
break;
default:
cout << “Invalid user type selected. Please try again.” << endl;
}
} while (userType != 3);
return 0;
}
Key Concepts and Benefits
This mini-project is a great way to learn and apply OOP principles like encapsulation and modularity. It also introduces fundamental concepts such as:
- Array-based storage: Using a fixed-size array to store books.
- Menu-based navigation: Simple console menus for a better user experience.
- State management: Tracking book issuance using a boolean variable.
Possible Improvements
While this system covers the basics, there is room for future enhancements, such as:
- Implementing persistent storage using file I/O.
- Adding user authentication for admin and student roles.
- Enhancing the search functionality with more criteria.
Source Code File And Access Link for the Bank Management System
Library_Management_System_report