Note: the program should be written only in C++ Problem 2: submit the implementa
ID: 3596048 • Letter: N
Question
Note: the program should be written only in C++
Problem 2: submit the implementation file here mplement a C++ project that consists of separate files to manage a list of books. Specifically, implement the following three functions declared in the header file, which you have submitted earlier: void addBook( vector &bookList;, Book &newBook;) This function will add newBook to bookList if it is not already in bookList int backupBookList vector &bookList;, Bookx &backupList;) This function copies all the books stored in bookList to a dynamic array managed by the backupList pointer and returns the size of this dynamic array void destructBackupList (Book* &backupList;); This function returns the space occupied by the pointee of backupList.Explanation / Answer
Solution:
Book.h
#include<iostream>
#include<string>
#include<vector>
#ifndef BOOK
#define BOOK
using namespace std;
// Define Class
class Book
{
string title;
string author;
int id;
// Class functions
public:
// Constructor
Book(){};
Book(string tit,string auth,int ident);
// Accessor methods
string getTitle();
string getAuthor();
int getID();
};
// Other functions
void addBook(vector<Book> &bookList, Book &newBook);
int backupBookList(vector<Book> &bookList, Book* &backupList);
void destructBackupList(Book* &backupList);
void printBooks(vector<Book> &bookList);
#endif
Book.cpp
#include "Book.h"
#include<iostream>
#include<string>
#include<vector>
using namespace std;
// Constructor
Book::Book(string tit,string auth,int ident)
{
title=tit;
author=auth;
id=ident;
}
// Accessor functions
string Book:: getTitle()
{
return title;
}
string Book::getAuthor()
{
return author;
}
int Book:: getID()
{
return id;
}
// Function to add book
void addBook(vector<Book> &bookList,Book &newBook)
{
bookList.push_back(newBook);
}
// Function for back up
int backupBookList(vector<Book> &bookList, Book* &backupList)
{
backupList=new Book[bookList.size()];
int i=0;
for(i=0;i<bookList.size();i++)
{
backupList[i]=bookList.at(i);
}
cout<<"Books in back up"<<endl;
for(int k=0;k<i;k++)
{
cout<<"ID: "<<backupList[k].getID();
cout<<" Title: "<<backupList[k].getTitle();
cout<<" Author: "<<backupList[k].getAuthor()<<endl;
}
cout<<endl;
return i;
}
// Function to free memory
void destructBackupList(Book* &backupList)
{
free(backupList);
}
// Function to rint t he books
void printBooks(vector<Book> &bookList)
{
for(int i=0;i<bookList.size();i++)
{
cout<<"ID: "<<bookList.at(i).getID();
cout<<" Title: "<<bookList.at(i).getTitle();
cout<<" Author: "<<bookList.at(i).getAuthor()<<endl;
}
}
main.cpp
#include<iostream>
#include<string>
#include<vector>
#include"Book.h"
int main()
{
// Vector of books
vector<Book> bk;
Book* backup=NULL;
Book b1("AAA","BBB",1);
Book b2("CCC","DDD",2);
Book b3("EEE","FFF",3);
Book b4("GGG","HHH",4);
// Add book to vector
addBook(bk, b1);
addBook(bk, b2);
addBook(bk, b3);
addBook(bk, b4);
// Back up
backupBookList(bk,backup);
// rint Books
printBooks(bk);
system("pause");
return 0;
}
Output:
Books in back up
ID: 1 Title: AAA Author: BBB
ID: 2 Title: CCC Author: DDD
ID: 3 Title: EEE Author: FFF
ID: 4 Title: GGG Author: HHH
ID: 1 Title: AAA Author: BBB
ID: 2 Title: CCC Author: DDD
ID: 3 Title: EEE Author: FFF
ID: 4 Title: GGG Author: HHH
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.