DATA STRUCTURE / C++ 7. a. Some of the characteristics of a book are the title,
ID: 3806762 • Letter: D
Question
DATA STRUCTURE / C++7. a. Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication. Design the class bookType that defines the book as an ADT. Each object of the class bookType can hold the following information about a book: title, up to four authors, publisher, ISBN, price, and number of copies in stock. To keep track of the number of authors, add another data member. Include the member functions to perform the various operations on the objects of bookType. For example, the typical operations that can be performed on the title are to show the title, set the title, and check whether a title is the same as the actual title ofthe book. Similarly, the typical operations that can be performed on the number ofcopies in stock are to show the number of copies in stock, set the number of copies in stock, update the number of copies in stock, and return the number of copies in stock. Add similar operations for the publisher, ISBN, book price, and authors. Add the appropriate constructors and a destructor (ifone is needed). DATA STRUCTURE / C++
7. a. Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication. Design the class bookType that defines the book as an ADT. Each object of the class bookType can hold the following information about a book: title, up to four authors, publisher, ISBN, price, and number of copies in stock. To keep track of the number of authors, add another data member. Include the member functions to perform the various operations on the objects of bookType. For example, the typical operations that can be performed on the title are to show the title, set the title, and check whether a title is the same as the actual title ofthe book. Similarly, the typical operations that can be performed on the number ofcopies in stock are to show the number of copies in stock, set the number of copies in stock, update the number of copies in stock, and return the number of copies in stock. Add similar operations for the publisher, ISBN, book price, and authors. Add the appropriate constructors and a destructor (ifone is needed).
Explanation / Answer
main
#include <iostream> #include <string> #include <iomanip> using namespace std; class bookType { private: string _title; string _authors[4]; string _publisher; string _ISBN; double _price; int _numOfcopies; int _numOfAuthor; string _year; public: bookType(); ~bookType(); void set_title(string title); string get_title(); int compare_titles(string title); void list_authors(); void set_numOfcopies(int num); int get_numOfcopies(); void updateNumOfcopies(int num); //void show_numOfcopies(); void set_publisher(string pub); string get_publisher(); // void show_publisher(); void set_ISBN(string isbn); string get_ISBN(); //void show_ISBN(); void set_price(double price); double get_price(); //void show_price(); void add_author(string author); string get_authors(int num); //void show_authors(); void set_year(string year); string get_year(); //void show_year(); void clear_authors(); void set_numOfAuthors(int num); int get_numOfAuthors(); }; bookType::bookType(){ _title = ""; _authors[0] = ""; _authors[1] = ""; _authors[2] = ""; _authors[3] = ""; _publisher = ""; _price = 0; _ISBN = ""; _numOfAuthor = 0; _numOfcopies = 0; _year = ""; } bookType::~bookType() { _title = ""; _authors[0] = ""; _authors[1] = ""; _authors[2] = ""; _authors[3] = ""; _publisher = ""; _price = 0; _ISBN = ""; _numOfAuthor = 0; _numOfcopies = 0; _year = ""; } void bookType::set_title(string title){ _title = title; } string bookType::get_title(){ return _title; } void bookType::clear_authors(){ _numOfAuthor = 0; } void bookType::set_publisher(string pub){ _publisher = pub; } string bookType::get_publisher(){ return _publisher; } void bookType::add_author(string author) { _authors[_numOfAuthor] = author; } void bookType::set_numOfAuthors(int num) { _numOfAuthor = num; } int bookType:: get_numOfAuthors() { return _numOfAuthor; } void bookType::list_authors() { int i = 0; cout << "Authors are" << endl; for (int i = 0; i < 4; i++) if (_authors[i] != "") { cout << _authors[i] << endl; } } void bookType::set_numOfcopies(int num) { _numOfcopies = num; } void bookType::updateNumOfcopies(int num) { _numOfcopies = _numOfcopies + num; } int bookType::get_numOfcopies() { return _numOfcopies; } void bookType::set_ISBN(string isbn) { _ISBN = isbn; } string bookType::get_ISBN() { return _ISBN; } void bookType::set_price(double price) { _price = price; } double bookType::get_price() { return _price; } void bookType::set_year(string year) { _year = year; } string bookType::get_year() { return _year; } int bookType::compare_titles(string title)//check this { int i = (title == _title); return i; } main
#include "bookType.h" #include <fstream> int main() { bookType books[5]; ifstream theFile("books.dat"); string line; string line2; double number; for (int i = 0; i < 5; i++) { getline(theFile, line); books[i].set_title(line); getline(theFile, line); books[i].set_ISBN(line); getline(theFile, line); books[i].set_publisher(line); getline(theFile, line); books[i].set_year(line); theFile >> number; theFile.ignore(numeric_limits<streamsize>::max(), ' '); books[i].set_price(number); theFile >> number; theFile.ignore(numeric_limits<streamsize>::max(), ' '); books[i].set_numOfcopies(number); theFile >> number; theFile.ignore(numeric_limits<streamsize>::max(), ' '); books[i].set_numOfAuthors(number); //theFile >> line; //books[i].add_author(line); } for (int i = 0; i < 5; i++) { cout << books[i].get_title(); cout << endl; cout << books[i].get_ISBN(); cout << endl; cout << books[i].get_publisher(); cout << endl; cout << books[i].get_year(); cout << endl; cout << books[i].get_price(); cout << endl; cout << books[i].get_numOfcopies(); cout << endl; cout << books[i].get_numOfAuthors(); cout << endl; //books[i].list_authors(); } //a[2].list_authors(); cout << endl; //a[0].get_authors(1); std::cin.get(); return 0; } Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.