SimplifyC++ Article
Comprehensive C++ Learning through a Practical Library Management System Project
Comprehensive C++ Learning through a Practical Library Management System Project
The best way to learn C++ is by working on a comprehensive and practical project that covers core and advanced concepts. Here's an expanded version of the Library Management System example, designed to help learners grasp essential programming principles, master memory management, and work with the Standard Template Library (STL), all while avoiding third-party libraries.
Objectives of the Project:
Learn Object-Oriented Programming (OOP):
Properly structure code by dividing responsibilities among objects.
Understand and apply inheritance and interfaces for extensibility when needed.
Master Data Management with STL:
Use containers like
std::vectorandstd::mapeffectively.Learn file operations using the
fstreamlibrary.
Implement Proper Memory Management:
Avoid memory leaks through safe allocation and deallocation.
Use smart pointers if required to enhance efficiency.
Handle Errors Gracefully:
Use exception handling to manage runtime errors.
Validate user input to ensure the program runs smoothly.
Optimize Performance:
Employ efficient loops and algorithms.
Implement search and sort operations for better scalability.
Expanded Project Details
1. Splitting Code into Multiple Files
To improve maintainability and clarity, split the project into three main files:
Header File (
library.h): Contains class definitions and function declarations.Implementation File (
library.cpp): Contains the implementation of the classes and functions.Main File (
main.cpp): Contains the main function and the program's workflow.
2. New Features
Borrowing and Returning Books:
Add functionality to allow users to borrow and return books. Ensure a book is available before borrowing.
Book Categories:
Classify books into categories (e.g., Science Fiction, History, Technology) and allow users to filter books by category.
Tracking Borrowing Dates:
Record borrowing dates and implement deadlines for returning books.
Technical Details
Detailed Class Design
Book Class
The Book class models the attributes and behavior of a book in the library.
class Book {private: int id; std::string title; std::string author; std::string category; // Book category int year; bool isBorrowed; // Borrow status
public: Book(int bookID, const std::string& bookTitle, const std::string& bookAuthor, const std::string& bookCategory, int bookYear) : id(bookID), title(bookTitle), author(bookAuthor), category(bookCategory), year(bookYear), isBorrowed(false) {}
int getID() const { return id; } std::string getTitle() const { return title; } std::string getAuthor() const { return author; } std::string getCategory() const { return category; } int getYear() const { return year; } bool getBorrowStatus() const { return isBorrowed; }
void borrowBook() { isBorrowed = true; } void returnBook() { isBorrowed = false; }
void display() const { std::cout << std::setw(5) << id << " | " << std::setw(20) << title << " | " << std::setw(15) << author << " | " << std::setw(10) << category << " | " << std::setw(4) << year << " | " << (isBorrowed ? "Borrowed" : "Available") << "\n"; }};Library Class
The Library class manages the collection of books and their operations.
class Library {private: std::vector<Book> books;
public: void addBook(int id, const std::string& title, const std::string& author, const std::string& category, int year) { books.emplace_back(id, title, author, category, year); std::cout << "Book added successfully!\n"; }
void deleteBook(int id) { auto it = std::remove_if(books.begin(), books.end(), [id](const Book& book) { return book.getID() == id; }); if (it != books.end()) { books.erase(it, books.end()); std::cout << "Book deleted successfully!\n"; } else { std::cout << "Book not found!\n"; } }
void borrowBook(int id) { for (auto& book : books) { if (book.getID() == id) { if (book.getBorrowStatus()) { std::cout << "Book is already borrowed!\n"; } else { book.borrowBook(); std::cout << "Book borrowed successfully!\n"; } return; } } std::cout << "Book not found!\n"; }
void returnBook(int id) { for (auto& book : books) { if (book.getID() == id) { if (!book.getBorrowStatus()) { std::cout << "Book is not currently borrowed!\n"; } else { book.returnBook(); std::cout << "Book returned successfully!\n"; } return; } } std::cout << "Book not found!\n"; }
void displayBooksByCategory(const std::string& category) const { bool found = false; for (const auto& book : books) { if (book.getCategory() == category) { book.display(); found = true; } } if (!found) { std::cout << "No books found in category: " << category << "\n"; } }
void displayAllBooks() const { if (books.empty()) { std::cout << "No books in the library.\n"; return; }
std::cout << std::setw(5) << "ID" << " | " << std::setw(20) << "Title" << " | " << std::setw(15) << "Author" << " | " << std::setw(10) << "Category" << " | " << std::setw(4) << "Year" << " | " << "Status\n"; std::cout << std::string(70, '-') << "\n";
for (const auto& book : books) { book.display(); } }};Expanded Main Function
The main program allows users to interact with the library system through a menu-driven interface.
int main() { Library library;
int choice; while (true) { std::cout << "\nLibrary Management System\n"; std::cout << "1. Add Book\n"; std::cout << "2. Delete Book\n"; std::cout << "3. Borrow Book\n"; std::cout << "4. Return Book\n"; std::cout << "5. Display Books by Category\n"; std::cout << "6. Display All Books\n"; std::cout << "7. Exit\n"; std::cout << "Enter your choice: "; std::cin >> choice;
if (choice == 7) { std::cout << "Exiting program. Goodbye!\n"; break; }
switch (choice) { case 1: { int id, year; std::string title, author, category; std::cout << "Enter Book ID: "; std::cin >> id; std::cin.ignore(); std::cout << "Enter Book Title: "; std::getline(std::cin, title); std::cout << "Enter Book Author: "; std::getline(std::cin, author); std::cout << "Enter Book Category: "; std::getline(std::cin, category); std::cout << "Enter Book Year: "; std::cin >> year; library.addBook(id, title, author, category, year); break; } case 2: { int id; std::cout << "Enter Book ID to delete: "; std::cin >> id; library.deleteBook(id); break; } case 3: { int id; std::cout << "Enter Book ID to borrow: "; std::cin >> id; library.borrowBook(id); break; } case 4: { int id; std::cout << "Enter Book ID to return: "; std::cin >> id; library.returnBook(id); break; } case 5: { std::string category; std::cout << "Enter category: "; std::cin.ignore(); std::getline(std::cin, category); library.displayBooksByCategory(category); break; } case 6: library.displayAllBooks(); break; default: std::cout << "Invalid choice. Please try again.\n"; } }
return 0;}Summary
This expanded project covers a broad range of C++ topics, from basic syntax to advanced concepts like object-oriented design, memory management, and file handling. By implementing this project step-by-step, learners can gain a deeper understanding of C++ and develop skills that are applicable in real-world scenarios.