generate an exercise based on this C++ elements, and generate an code solution. use a the data type list, vector and focus on polymorphism. Pointers. References. | Strings. Data containers. | Structures. Unions and bit fields. Classes and objects. Encapsulation. | Constructors and destructors. Static. This pointer. | Friend functions. | Inheritance. Templates. | Operator overloading. | Polymorphism.
Assistant
Exercise: Library Management System
Design a simple Library Management System in C++ using the following concepts:
Structures, Classes, and Encapsulation
Vectors and Lists
Pointers and References
Constructors and Destructors
Static Members and This Pointer
Friend Functions
Inheritance and Polymorphism
Templates
Operator Overloading
Requirements:
Book: A structure containing book details like title, author, ISBN, and a flag for availability.
Person: A base class with common details like name and ID.
Librarian: Derived from Person with additional privileges.
Member: Derived from Person with methods to borrow and return books.
Library: Manages a collection of books using std::vector and std::list.
Friend Function: A function to access private data of classes.
Static Variables/Methods: Use to keep track of total books or members.
Operator Overloading: Overload the << operator to print details.
Exception Handling: Handle borrowing of unavailable books.
Solution:
cpp
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <memory>
// Book Structure
struct Book {
std::string title;
std::string author;
std::string ISBN;
bool isAvailable;
Book(std::string t, std::string a, std::string i)
: title(t), author(a), ISBN(i), isAvailable(true) {}
friend std::ostream& operator<<(std::ostream& os, const Book& book);
};
std::ostream& operator<<(std::ostream& os, const Book& book) {
os << "Title: " << book.title << ", Author: " << book.author
<< ", ISBN: " << book.ISBN << ", Available: " << (book.isAvailable ? "Yes" : "No");
return os;
}
// Person Class
class Person {
protected:
std::string name;
int id;
public:
Person(std::string n, int i) : name(n), id(i) {}
virtual void info() const = 0; // Pure virtual function
};
// Librarian Class
class Librarian : public Person {
public:
Librarian(std::string n, int i) : Person(n, i) {}
void info() const override {
std::cout << "Librarian Name: " << name << ", ID: " << id << std::endl;
}
void addBook(std::vector<Book>& books, const Book& book) {
books.push_back(book);
std::cout << "Book added successfully.\n";
}
};
// Member Class
class Member : public Person {
private:
std::list<Book> borrowedBooks;
public:
Member(std::string n, int i) : Person(n, i) {}
void info() const override {
std::cout << "Member Name: " << name << ", ID: " << id << std::endl;
std::cout << "Borrowed Books: ";
for(const auto& book : borrowedBooks)
std::cout << book.title << ", ";
std::cout << std::endl;
}
void borrowBook(Book& book) {
if (book.isAvailable) {
book.isAvailable = false;
borrowedBooks.push_back(book);
std::cout << "Book borrowed successfully.\n";
} else {
throw std::runtime_error("Book is not available");
}
}
void returnBook(Book& book) {
book.isAvailable = true;
borrowedBooks.remove(book);
std::cout << "Book returned successfully.\n";
}
};
// Library Class
class Library {
private:
std::vector<Book> books;
static int totalBooks;
public:
Library() {}
void addBook(const Book& book) {
books.push_back(book);
totalBooks++;
}
void showBooks() const {
for(const auto& book : books) {
std::cout << book << std::endl;
}
}
Book* findBookByISBN(const std::string& ISBN) {
for(auto& book : books) {
if(book.ISBN == ISBN)
return &book;
}
return nullptr;
}
static int getTotalBooks() {
return totalBooks;
}
};
int Library::totalBooks = 0;
int main() {
Library library;
// Adding books
library.addBook(Book("Book One", "Author A", "ISBN0001"));
library.addBook(Book("Book Two", "Author B", "ISBN0002"));
library.addBook(Book("Book Three", "Author C", "ISBN0003"));
Librarian librarian("Librarian Name", 1);
Member member("Member Name", 101);
// Display total number of books in the library
std::cout << "Total Books: " << Library::getTotalBooks() << std::endl;
// Show all books
library.showBooks();
// Borrow a book
Book* bookToBorrow = library.findBookByISBN("ISBN0001");
if(bookToBorrow) {
try {
member.borrowBook(*bookToBorrow);
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
}
// Show member info
member.info();
// Return the book
if(bookToBorrow) {
member.returnBook(*bookToBorrow);
}
// Show member info after returning the book
member.info();
// Show all books again
library.showBooks();
return 0;
}
Explanation:
Structure: Defined a Book structure with a friend function to overload the << operator.
Classes and Encapsulation: Defined Person, Librarian, and Member classes. Used encapsulation to manage member data.
Inheritance and Polymorphism: Librarian and Member classes inherit from Person class. Demonstrated polymorphism using the info() method.
Vectors and Lists: Used std::vector for storing books in the library and std::list for borrowed books in the member class.
Pointers and References: Used pointers and references for accessing book objects.
Static Members: totalBooks is a static member to track the total number of books.
Friend Functions: Used to print book details.
Operator Overloading: Overloaded the << operator for the Book structure.
Exception Handling: Handled exception when trying to borrow an unavailable book.