You are on page 1of 20

Abstract

This C++ project uses the concepts of Object-Oriented Programming to create an abstract representation
of a library management system. It defines three classes: Student (a derived class representing students
without login requirements), Librarian (a derived class representing librarians with login credentials), and
Person (an abstract base class with a pure virtual function for login). A Book class and a Library class that
oversees a library of books are also included in the project. There are ways to add, remove, display, and
assign books to students in the Library class. The system loads and saves books using file input/output
(I/O). It offers a menu-driven interface for users to interact with the library; students can issue books
without authentication, while librarians must log in for certain activities.
Keywords: Library System, File Handling, Menu-Driven, Login System, Dynamic Memory, Inheritance,
Abstraction, Polymorphism, Encapsulation, User Input, Code Modularity, Error Handling, Menu Design,
Code Reusability.

1
Contents

Abstract ………………………………………………………………………………… 1
List of Figures ………………………………………………………………………………… 3
Chapter 1 Introduction 4
1. 1 Project introduction 4
1. 2 Objectives 4
1. 3 Brief description 4
Chapter 2 Technical description 6
2. 1 Object-Oriented Programming 6
2. 1. 1 Encapsulation
2. 1. 2 Inheritance
2. 1. 3 Polymorphism
2. 1. 4 Abstraction
2. 2 Classes 6
2. 3 File Operations 6
2. 4 User Interaction 6
2. 5 Input Handling 6
2. 6 Dynamic Memory Management 7
2. 7 Error Handling 7
2. 8 Data Persistence 7
2. 9 Modularization 7
2.10 Security 7
Chapter 3 Implementation 8
3. 1 Initial User Interface and Functionalities 8
3. 2 Header Includes 8
3. 3 Standard input/output stream 9
3. 4 Class Names 9
3. 4. 1 class Person
3. 4. 2 class Librarian
3. 4. 3 class Student
3. 4. 4 class Book
3. 4. 5 class Library
3. 5 Main function 14
Chapter 4 Basic Library Management System source code 16
Chapter 5 Class Diagram 20
Chapter 6 Conclusion 21
6. 1 Conclusion 21

2
List of Figures
Figure No. Figure Name Page No.
Fig 1: Class diagram 20

3
Chapter 1
Introduction
1. 1 Project introduction
The introductory chapter outlines the motivation behind creating a Library Management System and
discusses the significance of utilizing OOP concepts in software development. It introduces the main
entities in the system, namely Persons, Librarians, Students, and Books, highlighting their relationships
and responsibilities.

1. 2 Objectives

 Design and implement a Library Management System using C++ and Object-Oriented
Programming principles.
 Create a user-friendly system that allows librarians to efficiently manage library operations.
 Enable students to seamlessly issue books through an intuitive and interactive interface.
 Utilize key OOP concepts, including encapsulation, inheritance, and polymorphism, to enhance
code organization and maintainability.
 Establish a robust system architecture with well-defined classes such as `Person`, `Librarian`,
`Student`, `Book`, and `Library`.
 Implement a secure login system for librarians while allowing students to issue books without
authentication.
 Develop features for adding, deleting, and displaying books, ensuring a dynamic and up-to-date
library inventory.
 Incorporate file input output operations for data persistence, allowing the system to store and
retrieve book data between program executions.
 Design an interactive and menu-driven user interface to facilitate easy navigation and operation.
 Provide a foundation for further enhancements and extensions to the Library Management
System.

1. 3 Brief description
This C++ program implements a simple Library Management System using Object-Oriented
Programming principles. The system consists of classes such as `Person`, `Librarian`, `Student`,
`Book` and `Library` to model different entities and their interactions.

 Person Class: A base class representing a generic person with a virtual function `login()` for
login functionality.
 Librarian Class: Derived from `Person`, it represents a librarian with a username and password.
The `login()` function validates librarian credentials.
 Student Class: Also derived from `Person`, it represents a student who doesn’t require login
credentials.
 Book Class: Represents a book with attributes like title and author.

4
 Library Class: Manages the collection of books, including features such as adding, deleting,
displaying books, and issuing books to students. It uses file input-output operations for data
persistence.

The program includes a menu-driven interface where librarians can perform tasks like adding and
deleting books, while students can issue books without authentication. The system saves and loads book
data from a file (`library.txt`) for persistence between program executions. This project serves as a
foundational structure for a basic Library Management System and demonstrates the use of Object-
Oriented Programming concepts for code organization, modularity, and reusability. The interactive menu
and user-friendly design make it accessible for managing a small library system.

5
Chapter 2
Technical description
2. 1 Object-Oriented Programming
Object-oriented programming (OOP) is a programming paradigm that organizes and structures code based
on the concept of "objects." Objects are instances of classes, which are templates or blueprints for
creating objects. In OOP, software design and development revolve around the following key principles:
2. 1. 1 Encapsulation: This is the concept of bundling data (attributes) and the methods (functions) that
operate on the data into a single unit called an object. It restricts direct access to some of an object's
components and can prevent unintended interference and misuse of the data.
2. 1. 2 Inheritance: Inheritance is a mechanism that allows one class to inherit the properties and
behaviors of another class. It promotes code reuse and allows the creation of new classes that are based on
existing ones.
2. 1. 3 Polymorphism: Polymorphism allows objects of different classes to be treated as objects of a
common superclass. It enables flexibility and dynamic behavior in your code by allowing you to work
with objects in a more generic way.
2. 1. 4 Abstraction: Abstraction involves simplifying complex reality by modeling classes based on the
essential features they share. It hides the complex details while exposing only the necessary
functionalities.
2. 2 Classes:

 Person: An abstract base class with a pure virtual function login() to be overridden by derived
classes.
 Librarian: Derived from Person, represents a librarian with a username and password.
Implements the login() function.
 Student: Derived from Person, represents a student who doesn't require login credentials.
 Book: Represents a book with attributes title and author.
 Library: Manages a collection of books. Includes methods for adding, deleting, displaying
books, and issuing books to students.
2. 3 File Operations:
The Library class uses file input output operations for data persistence. It saves and loads book data from
a file (library.txt) during object creation and destruction.
2. 4 User Interaction:
The main function (main()) presents a menu-driven interface to the user using a do-while loop. Librarians
can add books, delete books, and display the library inventory after successful login. Students can issue
books without authentication. The program displays appropriate messages for successful or failed
operations.
2. 5 Input Handling:
Uses cin for user input, including handling strings with spaces using getline. Utilizes cin.ignore() to
handle newline characters left in the input buffer.

6
2. 6 Dynamic Memory Management:
Uses dynamic memory allocation for storing a collection of books in the vector<Book> within the Library
class.
2. 7 Error Handling:
Provides error messages for invalid user choices and unsuccessful operations (for example book not
found).
2. 8 Data Persistence:
Saves book data to a file (library.txt) when the program exits and loads data from the file when the
program starts.
2. 9 Modularization:
Divides functionality into classes for better code organization and reusability.
2.10 Security:
Implements a basic form of authentication for librarians using username and password.

This Library Management System demonstrates foundational principles of OOP and provides a user-
friendly interface for managing a small library.

7
Chapter 3
Implementation
3. 1 Initial User Interface and Functionalities:

 Add Book (Librarian):


Librarians can add books to the library. Books are added to an in-memory vector and persisted to
a file (library.txt).
 Delete Book (Librarian):
Librarians can delete books from the library based on the title. Books are removed from the in-
memory vector and updated in the file.
 Display Books:
Displays a list of all books in the library. Checks if the library is empty and provides appropriate
feedback.
 Issue Book (Student):
Students can issue books from the library. The chosen book is removed from the in-memory
vector and updated in the file.
 Exit:
Exits the program loop.

3. 2 Header Includes:

Header files usually have a .h or .hpp extension. These are include directives for standard C++ header
files:

8
 #include <iostream>: This library allows for basic input and output operations through
'cin' and 'cout'.
 #include <fstream>: This library enables reading from and writing to files using file streams like
'ifstream' and 'ofstream'.
 #include <vector>: This library provides the implementation of dynamic arrays (vectors) in C++.
Vectors are resizable arrays, allowing for efficient addition and removal of elements.
 #include <string>: This library provides functionality to handle strings in C++, including
creating, manipulating, and processing strings.
 #include <algorithm>: This library provides a collection of standard algorithms for operations
such as sorting, searching, and more.

3. 3 Standard input/output stream:


The line using namespace std; is a C++ directive that brings the entire std (standard) namespace into
scope. It allows you to use elements from the std namespace without qualifying them explicitly with std ::
. In C++, the Standard Template Library (STL) components, such as cout, cin, vector, string, etc., are part
of the std namespace. By including using namespace std; , we simplify our code by not having to prepend
std:: to these elements.

3. 4 Class Names:
3. 4. 1 class Person:

 Class Definition:
class Person { ... };: This defines a class named Person, which serves as a base class for other
classes related to people in a library management system.
 Login Function:
virtual bool login() const = 0;: This line declares a pure virtual function named login in the Person
class. The virtual keyword indicates that this is a virtual function, and the = 0 at the end signifies
that it is a pure virtual function. A pure virtual function has no implementation in the base class,
and any derived class must provide its own implementation.
 Purpose of login Function:

9
The login function is a placeholder for a login mechanism that subclasses (like Librarian and
Student) should implement according to their specific needs. The const specifier indicates that
this function does not modify the state of the object on which it is called.
 Virtual Function:
By making login a virtual function, it allows derived classes to provide their own specific
implementation of the login mechanism. This is crucial for achieving polymorphic behavior,
where the appropriate login behavior is determined based on the actual type of the object at
runtime.
 Pure Virtual Function:
Making login a pure virtual function (= 0) enforces that any class inheriting from Person must
implement their version of login. Classes that have pure virtual functions are termed abstract
classes, and they cannot be instantiated directly. Instead, they serve as a blueprint for deriving
concrete classes.
3. 4. 2 class Librarian:

 class Librarian : public Person:


This line defines the Librarian class, which is derived from the Person class. The Librarian class
inherits from the Person class, meaning it extends and specializes in the behavior defined in
Person.
 Private Member Variables:
string username; and string password;: These lines define private member variables to store the
username and password of a librarian.
 Constructor (Librarian):

10
Librarian (const string& uname, const string& pwd) : username(uname), password(pwd) {}: This
is the constructor for the Librarian class. It takes two parameters, uname and pwd, and initializes
the username and password member variables using the provided values.
 login Function Override:
bool login() const override { ... }: This line overrides the pure virtual function login from the base
class Person. The override provides a specific implementation for the login function for a
librarian.

 Librarian Login Logic:


The login function implements the logic for a librarian to log in. It prompts the user for a
username and password, then checks if the entered credentials match either the provided
username and password or a default set of credentials ("NS" and "511").
 Return Value:
The function returns true if the login credentials are valid (either match the stored credentials or
the default credentials), indicating a successful login. It returns false otherwise.

3. 4. 3 class Student:

11
 class Student : public Person: This line defines the Student class, which is derived from the
Person class. The Student class inherits from the Person class, meaning it extends and specializes
the behavior defined in Person.
 login Function Override:
bool login() const override { ... }: This line overrides the pure virtual function login from the base
class Person. The override provides a specific implementation for the login function for a student.
 Student Login Logic:
The login function provides a simple login logic for a student, stating that a student doesn't need
to log in. It always returns true, indicating that a student is always considered logged in.
 Return Value:
The function always returns true, implying that a student is always logged in, regardless of any
login attempt.

3. 4. 4 class Book:

 class Book { ... };: This line defines a class named Book, which serves as the base class for
modeling a book.
 Private Member Variables:
string title; and string author;: These lines define private member variables to store the title and
author of the book.
 Constructor (Book):
Book(const string& t, const string& a) : title(t), author(a) {}: This is the constructor for the Book
class. It takes two parameters, t for the title and a for the author, and initializes the title and author
member variables using the provided values.
 Getter Functions:
string getTitle() const { ... } and string getAuthor() const { ... }: These lines define member
functions to retrieve the title and author of the book. These are getter functions, which return the

12
respective private member variables title and author. The const specifier indicates that these
functions do not modify the state of the object on which they are called.

3. 4. 5 class Library:

 class Library { ... };:


This line defines a class named Library.
 Private Member Variables:
vector<Book> books;: This line defines a private member variable books, which is a vector that
will store instances of the Book class.
 Constructor (Library):
Library(): This is the constructor for the Library class. It is responsible for loading books from a
file when an object of Library is created.
 Destructor (~Library):
~Library(): This is the destructor for the Library class.It is responsible for saving books to a file
when an object of Library is destroyed.
 Member Functions:
addBook, deleteBook, displayBooks, and issueBook are member functions that perform various
operations related to managing books in the library.
 Private Helper Functions:
saveBooks and loadBooks are helper functions used internally by the Library class.saveBooks
saves the current state of the library (books) to a file.
loadBooks loads books from a file into the 'books' vector.

13
3. 5 Main function:

14
 Library Instance Creation: This line creates an instance of the Library class named library.
 Menu Display and User Choice:
The program displays a menu with various options using cout statements. It prompts the user to
enter a choice (cin >> choice) from the menu.
 Switch Statement for Menu Options:
The program uses a switch statement to execute different actions based on the user's choice.
 Case 1: Add Book (Librarian):
If the user selects option 1, it prompts for librarian login and book details, then calls
library.addBook to add the book.
 Case 2: Delete Book (Librarian):
If the user selects option 2, it prompts for librarian login and the title of the book to delete, then
calls library.deleteBook.
 Case 3: Display Books:
If the user selects option 3, it calls library.displayBooks to display the library's books.
 Case 4: Issue Book (Student):
If the user selects option 4, it prompts for the title of the book to issue, then calls
library.issueBook for a student.
 Case 0: Exit:
If the user selects option 0, it displays an exit message.
 Default Case:
If the user enters an invalid choice, it displays an error message.
 Loop:
The program uses a do-while loop to keep displaying the menu and prompting for choices until
the user selects to exit (0).
 Return:
Finally, the main function returns 0, indicating successful program completion.

15
Chapter 4
Basic Library Management System source code

#include <iostream> cout << "Enter Librarian username: ";


#include <fstream> string inputUsername;
#include <vector> cin >> inputUsername;
#include <string> cout << "Enter Librarian password: ";
#include <algorithm> string inputPassword;
cin >> inputPassword;

using namespace std;


// Check if entered credentials match either
of the librarian credentials
// Base class for a person
return (inputUsername == username &&
class Person { inputPassword == password) ||

public: (inputUsername == "NS" &&


inputPassword == "511");
virtual bool login() const = 0; // Pure virtual
function for login }

}; };

// Derived class for a Librarian // Derived class for a Student

class Librarian : public Person { class Student : public Person {

private: public:

string username; bool login() const override {

string password; // Student doesn't need to log in


return true;

public: }

Librarian(const string& uname, const string& };


pwd) : username(uname), password(pwd) {}

// Base class for a Book


bool login() const override {
class Book {
// Librarian login logic
private:

16
string title;
string author; void addBook(const Book& b) {
books.push_back(b);
public: cout << "Book added successfully.\n";
Book(const string& t, const string& a) : saveBooks();
title(t), author(a) {}
}

string getTitle() const {


void deleteBook(const string& title) {
return title;
auto it = remove_if(books.begin(),
} books.end(), [title](const Book& b) {
return b.getTitle() == title;
string getAuthor() const { });
return author;
} if (it != books.end()) {
}; books.erase(it, books.end());
cout << "Book deleted successfully.\n";
// Library class with encapsulation saveBooks();
class Library { } else {
private: cout << "Book not found.\n";
vector<Book> books; }
}
public:
Library() { void displayBooks() const {
loadBooks(); // Load books from file on if (books.empty()) {
object creation
cout << "No books to display.\n";
}
} else {
cout << "Library Books:\n";
~Library() {
for (const auto& book : books) {
saveBooks(); // Save books to file on
cout << "Title: " << book.getTitle() <<
object destruction
", Author: " << book.getAuthor() << "\n";
}
}

17
} while (getline(file, line)) {
} size_t pos = line.find(',');
string title = line.substr(0, pos);
void issueBook(const string& title) { string author = line.substr(pos + 1);
auto it = find_if(books.begin(), books.emplace_back(title, author);
books.end(), [title](const Book& b) {
}
return b.getTitle() == title;
file.close();
});
}
};
if (it != books.end()) {
cout << "Book issued successfully.\n";
int main() {
books.erase(it);
// Create a Library
saveBooks();
Library library;
} else {
cout << "Book not found.\n";
int choice;
}
do {
}
cout << "|--------------*Menu*-------------|\
n";
private: cout << "|1. Add Book (Librarian) |\
n";
void saveBooks() const {
cout << "|---------------------------------|\n";
ofstream file("library.txt");
cout << "|2. Delete Book (Librarian) |\
for (const auto& book : books) {
n";
file << book.getTitle() << "," <<
cout << "|---------------------------------|\n";
book.getAuthor() << "\n";
cout << "|3. Display Books |\n";
}
cout << "|---------------------------------|\n";
file.close();
cout << "|4. Issue Book (Student) |\
}
n";
cout << "|---------------------------------|\n";
void loadBooks() {
cout << "|0. Exit |\n";
ifstream file("library.txt");
cout << "|---------------------------------|\n";
string line;

18
cout << "Enter your choice: "; } else {
cin >> choice; cout << "Librarian login failed.
Access denied.\n";
}
switch (choice) {
break;
case 1: {
}
Librarian librarian("ns", "510"); //
Replace with actual librarian credentials case 3:
if (librarian.login()) { library.displayBooks();
string title, author; break;
cout << "Enter book title: "; case 4: {
cin.ignore(); Student student; // Student doesn't
need credentials
getline(cin, title);
string title;
cout << "Enter author: ";
cout << "Enter the title of the book
getline(cin, author);
you want to issue: ";
library.addBook(Book(title,
cin.ignore();
author));
getline(cin, title);
} else {
library.issueBook(title);
cout << "Librarian login failed.
Access denied.\n"; break;
} }
break; case 0:
} cout << "Exiting program.\n";
case 2: { break;
Librarian librarian("ns", "510"); // default:
Replace with actual librarian credentials
cout << "Invalid choice. Please try
if (librarian.login()) { again.\n";
string title; break;
cout << "Enter the title of the book }
to delete: ";
} while (choice != 0);
cin.ignore();
return 0;
getline(cin, title);
}
library.deleteBook(title);

19
Chapter 6
Conclusion
6. 1 Conclusion:
The presented library management system code encapsulates essential principles of object-oriented
programming, featuring polymorphism, encapsulation, and inheritance. The differentiation between
librarians and students showcases the flexibility of the system, where login requirements vary. The use of
virtual functions, especially in the `Person` class, promotes extensibility, enabling the addition of new
user types in the future. The system maintains a persistent state by storing books in an external file,
contributing to data integrity. However, potential improvements could involve enhanced error handling
mechanisms and a more sophisticated user interface for an improved user experience. In conclusion, the
provided C++ code establishes a solid foundation for a library management system. It effectively
demonstrates the implementation of key object-oriented concepts, creating a modular and extensible
structure. The program's file-based approach ensures data persistence, contributing to the reliability of the
system. While the existing functionalities cater to basic library operations, future iterations could focus on
refining the user interface, incorporating advanced features, and refining error-handling mechanisms to
create a more robust and user-friendly application.

20

You might also like