You are to create a database of books that are stored using a vector. Keep track of the author, title and publication date of each book. Your program should have a main menu that allows the user to select from the following: Add a book’s author, title, and date Print an alphabetical list of the books sorted by author Quit You must use a class to hold the data for each book. This class must hold three string fields, one to hold the author’s name, one for the publication date, and another to hold the book’s title. Store the entire database of books in a vector where each vector element is a book class object. To sort the data, use the generic sort function from the library. Note that this requires you to define the using namespace std; class Book { public: Book(); Book(string new_author, string new_title, string new_date); void setData(string new_author, string new_title, string new_date); string getAuthor() const; string getTitle() const; string getDate() const; friend bool operator< (const Book &book1, const Book &book2); private: string author, title, date; }; #endif Book.cpp Add the definitions of data members of class Book. The definition of friend function should be (in Book.cpp): bool operator< (const Book &book1, const Book &book2) { return (book1.author < book2.author); } TestProgram.cpp Don’t forget to add header files, vector and algorithm along with other required header files. using namespace std; void AddNewBook(vector *bookdata); void PrintBooks(vector &bookdata); void SortBooks(vector &bookdata); void PrintMenu(); int main() { vector bookdata; //Add your logic to test your class, using functions mentioned above return 0; }
You are to create a
- Add a book’s author, title, and date
- Print an alphabetical list of the books sorted by author
- Quit
You must use a class to hold the data for each book. This class must hold three string fields, one to hold the author’s name, one for the publication date, and another to hold the book’s title. Store the entire database of books in a vector where each vector element is a book class object.
To sort the data, use the generic sort function from the <
Note: you are required to create three files, Book.h, Book.cpp and TestProgram.cpp.
Book.h
#ifndef BOOK_H
#define BOOK_H
#include <string>
using namespace std;
class Book
{
public:
Book();
Book(string new_author, string new_title, string new_date);
void setData(string new_author, string new_title, string new_date);
string getAuthor() const;
string getTitle() const;
string getDate() const;
friend bool operator< (const Book &book1, const Book &book2);
private:
string author, title, date;
};
#endif
Book.cpp
Add the definitions of data members of class Book. The definition of friend function should be (in Book.cpp):
bool operator< (const Book &book1, const Book &book2)
{
return (book1.author < book2.author);
}
TestProgram.cpp
Don’t forget to add header files, vector and algorithm along with other required header files.
using namespace std;
void AddNewBook(vector<Book> *bookdata);
void PrintBooks(vector<Book> &bookdata);
void SortBooks(vector<Book> &bookdata);
void PrintMenu();
int main()
{
vector<Book> bookdata;
//Add your logic to test your class, using functions mentioned above
return 0;
}
Step by step
Solved in 2 steps