Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include class Book { private: std::string title; std::string author; int id; bool isAvailable; public: Book ( std::string t , std::string a ,

#include
#include
#include
#include
class Book {
private:
std::string title;
std::string author;
int id;
bool isAvailable;
public:
Book(std::string t, std::string a, int i) : title(t), author(a), id(i), isAvailable(true){}
void borrowBook(){
if(isAvailable){
isAvailable = false;
} else {
std::cout << "Book already borrowed." << std::endl;
}
}
void returnBook(){
isAvailable = true;
}
void displayInfo(){
std::cout << "Book: "<< title <<", Author: "<< author <<", ID: "<< id
<<", Available: "<<(isAvailable ? "Yes" : "No")<< std::endl;
}
};
class Library {
private:
std::vector books;
public:
void addBook(Book book){
books.push_back(book);
}
void displayAllBooks(){
for(int i =0; i < books.size(); i++){
books[i].displayInfo();
}
}
Book* findBookById(int id){
for(int i =0; i < books.size(); i++){
if(books[i].getId()== id){
return &books[i];
}
}
return NULL;
}
};
void saveLibraryData(Library &lib){
std::ofstream file("library.txt");
lib.displayAllBooks();
file.close();
}
int main(){
Library myLibrary;
Book book1("1984", "George Orwell", 101);
Book book2("To Kill a Mockingbird", "Harper Lee", 102);
Book book3("The Great Gatsby", "F. Scott Fitzgerald", 103);
myLibrary.addBook(book1);
myLibrary.addBook(book2);
myLibrary.addBook(book3);
myLibrary.displayAllBooks();
Book* book = myLibrary.findBookById(102);
if(book != NULL){
book->borrowBook();
}
myLibrary.displayAllBooks();
saveLibraryData(myLibrary);
return 0;
}
This program aims to represent a simple library management system but contains several intentional errors, bad practices, and areas for improvement. It's a great exercise to review this code, identify the errors, and think about how you would correct them.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions