How to implement istream& operator
Mar 27, 2019 at 8:29am UTC
From this code, I am pretty sure I am supposed to create an array of books with the istream& operator method. However, I am not sure how to do this.
I just need help on the istream operator >> method, not the whole program.
I attached my Book.cpp program under the Warehouse.h header file just in case you might need it (Book.cpp is also part of the project)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include "Warehouse.h"
#include "Book.h"
#include <iostream>
#include<string>
using namespace std;
static const int MAX_BOOKS = 35;
clss Warehouse {
/**
* @param is the input stream
* @param warehouse the warehouse object reference
* @return the input stream
*/
friend istream& operator >> (istream& is, Warehouse& warehouse);
/**
* @param os the output stream
* @param warehouse the warehouse object reference
* @return the output stream
*/
friend ostream& operator << (ostream& os, const Warehouse& warehouse);
public :
static const int MAX_BOOKS = 35;
Warehouse();
/**
* @param isbn the ISBN number to search for
* @param book reference to the matched book object, if found
* @return true if found.
*/
bool find (string isbn, Book& book) const ;
/**
* Prints the inventory of the Warehouse (i.e. list all the books)
*/
void list () const ;
private : /* extra credit */
void sort_();
private :
Book books[Warehouse::MAX_BOOKS];
int bookCount;
};
#endif /* WAREHOUSE_H */
Book.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#include "Book.h"
#include <iostream>
#include <string>
using namespace std;
static const int MAX_BOOKS = 35;
static const int MAX_AUTHORS = 20;
string title_;
string authors_[Book::MAX_AUTHORS];
int authorCount_;
string publisher_;
short yearPublish_;
bool hardcover_;
float price_;
string isbn_;
long copies_;
istream& operator >> (istream& is, Book& book){
is >> book.title_;
is.ignore();
is >> book.authorCount_;
for (int j=0;j<book.authorCount_;j++){
is >> book.authors_[j];
is.ignore();
}
is >> book.publisher_;
is.ignore();
is >> book.yearPublish_;
is.ignore();
is >> book.hardcover_;
is.ignore();
is >> book.price_;
is.ignore();
is >> isbn_;
is.ignore();
is >> copies_;
is.ignore();
return is;
}
ostream& operator << (ostream& os, const Book& book){
cout << "Title: " ;
cout << book.title_ << endl;
for (int j=0; j<book.authorCount_; j++){
cout << "Author: " ;
cout << book.authors_[j] << endl;
}
cout << "Publisher: " ;
cout << book.publisher_ << endl;
cout << "Year Published: " ;
cout << book.yearPublish_;
if (book.hardcover_) cout << "Cover: Hardcover" << endl;
if (!book.hardcover_) cout << "Cover: Paperback" << endl;
cout << "Price: " ;
cout << book.price_ << endl;
cout << "Isbn: " ;
cout << book.isbn_ << endl;
cout << "Copies: " ;
cout << book.copies_ << endl;
return os;
}
Book :: Book() {};
Book :: Book (string title, string authors[], int authorCount, string publisher, short yearPublish, bool hardcover, float price, string isbn, long copies){
title_ = title;
authorCount_ = authorCount;
for (int i=0;i<authorCount_;i++){
authors_[i] = authors[i];
}
publisher_ = publisher;
yearPublish_ = yearPublish;
hardcover_ = hardcover;
price_ = price;
isbn_ = isbn;
copies_= copies;
}
void Book::setTitle(string title){
title_=title;
}
string Book::getTitle()const {
return title_;
}
void Book:: setAuthorCount(short authorCount){
authorCount_ = authorCount;
}
void Book::setAuthors(string authors[]){
authors_[MAX_AUTHORS]=authors[MAX_AUTHORS];
}
string Book::getAuthors() const {
return authors_[MAX_AUTHORS-1];
}
void Book::setPublisher(string publisher){
publisher_ = publisher;
}
string Book::getPublisher() const {
return publisher_;
}
void Book::setYearPublish(short yearPublish){
yearPublish_ = yearPublish;
}
short Book:: getYearPublish() const {
return yearPublish_;
}
void Book::setHardcover(bool hardcover){
hardcover_ = hardcover;
}
bool Book::getHardcover() const {
return hardcover_;
}
void Book:: setIsbn(string isbn){
isbn_ = isbn;
}
string Book::getIsbn() const {
return isbn_;
}
void Book:: setPrice(float price){
price_ = price;
}
float Book:: getPrice() const {
return price_;
}
void Book:: setCopies(long copies){
copies_ = copies;
}
long Book::getCopies()const {
return copies_;
}
Last edited on Mar 27, 2019 at 8:31am UTC
Mar 27, 2019 at 1:38pm UTC
Hello daveed,
This is similar to
http://www.cplusplus.com/forum/beginner/251553/
Posting the same basic code and asking a similar question does not get any quicker help.
Here you post "Warehoude.h" and "Book.cpp". Since neither file goes with the other I am not sure what you expect.
Post the "Warehouse.cpp" and "Book.h" files so that everything makes sence and is put in proper context. The file with "main" would also be helpful.
Lastly pick one subject or the other. Do not split everyone's time between the two.
Hope that helps,
Andy
Topic archived. No new replies allowed.