
MAlik 5.
asked 10/13/20Create two c++ classes Library and Books
Implement a C++ program to develop a simple Library. Library contains pile of books and each book comprises of ISBN, Name, Author Name, Publisher Name, Issue Date, Return Date.
Create initializer/constructor functions for book. Declare and implement functions like insert book, retrieve book, update book information book using ISBN number in Library. Declare another function named Search to find a specific book from catalogue.
NOTE: It’s your choice you want to implement this program using Structures or Classes.
1 Expert Answer

Patrick B. answered 10/13/20
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define ISBN_LENGTH (50)
#define TITLE_LENGTH (255)
#define PUBLISHER_LENGTH (50)
#define AUTHOR_LENGTH (100)
#define MAX_NUM_BOOKS (100)
typedef
class Book
{
protected:
char ISBN[ISBN_LENGTH];
char title[TITLE_LENGTH];
char publisher[PUBLISHER_LENGTH];
char author[AUTHOR_LENGTH];
char issueDate[3]; //month,day,year. Ex. 10 15 20 is october 15 2020
char returnDate[3];
public:
Book() { }
Book(char * isbn, char * title, char * author)
{
memset(this->ISBN,0,ISBN_LENGTH);
strcpy(this->ISBN,isbn);
memset(this->title,0,TITLE_LENGTH);
strcpy(this->title, title);
memset(this->author,0,AUTHOR_LENGTH);
strcpy(this->author,author);
memset(this->publisher,0,PUBLISHER_LENGTH);
memset(this->issueDate,0,3);
memset(this->returnDate,0,3);
}
~Book()
{
memset(this->ISBN,0,ISBN_LENGTH);
memset(this->title,0,TITLE_LENGTH);
memset(this->author,0,AUTHOR_LENGTH);
memset(this->publisher,0,PUBLISHER_LENGTH);
memset(this->issueDate,0,3);
memset(this->returnDate,0,3);
}
Book (Book & book)
{
book.GetISBN(this->ISBN);
book.GetAuthor(this->author);
book.GetPublisher(this->publisher);
book.GetTitle(this->title);
book.GetIssueDate(this->issueDate);
book.GetReturnDate(this->returnDate);
}
void GetISBN( char * isbnBuff) { strncpy(isbnBuff,this->ISBN,ISBN_LENGTH); }
void GetTitle( char * titleBuff) { strncpy(titleBuff,this->title,TITLE_LENGTH); }
void GetAuthor( char *authorBuff) { strncpy(authorBuff,this->author,AUTHOR_LENGTH); }
void GetPublisher(char * publisherBuff) { strncpy(publisherBuff,this->publisher,PUBLISHER_LENGTH); }
void GetIssueDate( char * issueDateBuff) { strncpy(issueDateBuff,this->issueDate,3); }
void GetReturnDate( char * returnDateBuff) { strncpy(returnDateBuff,this->returnDate,3); }
void SetISBN( char * isbnBuff) { strncpy(this->ISBN,isbnBuff,ISBN_LENGTH); }
void SetTitle( char * titleBuff) { strncpy(this->title,titleBuff,TITLE_LENGTH); }
void SetAuthor( char *authorBuff) { strncpy(this->author,authorBuff,AUTHOR_LENGTH); }
void SetPublisher(char * publisherBuff) { strncpy(this->publisher,publisherBuff,PUBLISHER_LENGTH); }
void SetIssueDate( char * issueDateBuff) { strncpy(this->issueDate,issueDateBuff,3); }
void SetReturnDate( char * returnDateBuff) { strncpy(this->returnDate,returnDateBuff,3); }
void Output(char * strMsg=NULL)
{
if (strMsg!=NULL)
{
cout << "*************************************" << endl;
cout << strMsg << endl;
}
cout << "*************************************" << endl;
cout << " ISBN = >" << this->ISBN << "<" << endl;
cout << " title = >" << this->title << "<" << endl;
cout << " author = >" << this->author << "<" << endl;
cout << " publisher =>" << this->publisher << "<" << endl;
cout << "issue date:" << endl;
for (int iLoop=0; iLoop<3; iLoop++)
{
cout << ">" << (int )this->issueDate[iLoop] << "<" << endl;
}
cout << "return date:" << endl;
for (int iLoop=0; iLoop<3; iLoop++)
{
cout << " >" << (int) this->returnDate[iLoop] << "<" << endl;
}
}
} * TBook;
#define BOOK_SIZE ( sizeof(Book))
typedef
class LibraryCatalog
{
private:
Book bookArray[MAX_NUM_BOOKS];
int numBooks;
public:
LibraryCatalog()
{
this->numBooks=0;
memset(this->bookArray,0,MAX_NUM_BOOKS*BOOK_SIZE);
}
int GetNumBooks() { return(numBooks); }
~LibraryCatalog()
{
if (bookArray!=NULL)
{
memset(this->bookArray,0,MAX_NUM_BOOKS*BOOK_SIZE);
this->numBooks=0;
}
}
LibraryCatalog( LibraryCatalog& x)
{
int N = this->numBooks = x.numBooks;
memcpy(this->bookArray,x.bookArray,BOOK_SIZE*N);
}
int InsertAddNew(Book b)
{
int iReturn=-1;
if (this->numBooks<MAX_NUM_BOOKS)
{
this->bookArray[this->numBooks++] = b;
}
else
{
iReturn=-1;
}
return(iReturn);
}
TBook IndexerGetIndexAt( int iIndexPos)
{
TBook bookReturn=NULL;
if ((iIndexPos>-1) && (iIndexPos<numBooks))
{
bookReturn = new Book(bookArray[iIndexPos]);
}
return(bookReturn);
}
int LinearSearch( TBook b)
{
char targetISBN[ISBN_LENGTH];
char curISBN[ISBN_LENGTH];
int iReturn=-1;
b->GetISBN(targetISBN);
for (int iLoop=0; iLoop<this->numBooks; iLoop++)
{
bookArray[iLoop].GetISBN(curISBN);
if (strncmp(curISBN,targetISBN,ISBN_LENGTH)==0)
{
iReturn=iLoop;
memcpy(b,&bookArray[iLoop],BOOK_SIZE);
break;
}
}
return(iReturn);
}
} *TLibraryCatalog;
Go()
{
LibraryCatalog myBooks;
char date[3];
TBook book = new Book("9780316540032","The Catcher in the Rye","J.D. Salinger");
book->SetPublisher("Little/Brown");
myBooks.InsertAddNew(*book);
delete(book);
book = new Book("9780345320223","The Empire Strike Back","George Lucas/Donald F. Glut");
book->SetPublisher("Ballantine Books");
date[0]=10; date[1]=13; date[2]=20; book->SetIssueDate(date);
myBooks.InsertAddNew(*book);
delete(book);
book = new Book("9781451606393","The Liberty Amendments","Mark R. Levin");
book->SetPublisher("Threshold Editions");
date[0]=10; date[1]=13; date[2]=20; book->SetIssueDate(date);
date[0]=11; date[1]=4; date[2]=20; book->SetReturnDate(date);
myBooks.InsertAddNew(*book);
delete(book);
book = new Book("9780321573513","Algorithms","Kevin Wayne/Robert Sedgewick");
book->SetPublisher("Addison-Wesley");
date[0]=10; date[1]=15; date[2]=20; book->SetIssueDate(date);
date[0]=11; date[1]=6; date[2]=20; book->SetReturnDate(date);
myBooks.InsertAddNew(*book);
delete(book);
book = new Book("9780538735643,","Numerical Analysis","J. Douglas Faires/Richard Burden");
book->SetPublisher("Brooks & Cole/Cengage Learning");
date[0]=10; date[1]=17; date[2]=20; book->SetIssueDate(date);
date[0]=1; date[16]=6; date[2]=21; book->SetReturnDate(date);
myBooks.InsertAddNew(*book);
delete(book);
book = new Book("9781619300668,","Seven Wonders of the World","Farah Tizvi/Carmella Van Vleet");
book->SetPublisher("Nomad Press Inc.");
date[0]=10; date[1]=16; date[2]=20; book->SetIssueDate(date);
date[0]=11; date[3]=6; date[2]=20; book->SetReturnDate(date);
myBooks.InsertAddNew(*book);
delete(book);
int N=myBooks.GetNumBooks();
for (int iLoop=0; iLoop<N; iLoop++)
{
TBook curBookPtr = myBooks.IndexerGetIndexAt(iLoop);
char msgStr[25];
sprintf(msgStr," Book Rec # %d of %d \n",(iLoop+1),N);
curBookPtr->Output(msgStr);
}
book = new Book("9781451606393","The Liberty Amendments","Mark R. Levin");
int iIndexPos = myBooks.LinearSearch(book);
if (iIndexPos>0)
{
book->Output("FOUND");
}
else
{
cout << "BOOK NOT FOUND" << endl;
}
delete(book);
book = new Book("9786543210ABC","This book is not in the list","Nobody");
iIndexPos = myBooks.LinearSearch(book);
if (iIndexPos>0)
{
book->Output("FOUND");
}
else
{
cout << "BOOK NOT FOUND" << endl;
}
delete(book);
}
int main()
{
Go();
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
I have uploaded this source code for you in the RESOURCES section under this link10/13/20