我查看了许多关于SO的问题,但找不到真正能回答我问题的东西。
我想知道如何实现istream& operator >>
方法来创建一系列的图书。
我只需要istream方法的帮助,而不是整个程序的帮助。
#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文件:
#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){
os<< book.title_ << endl;
for (int j=0; j<book.authorCount_; j++){
os<< book.authors_[j] << endl;
}
os<< book.publisher_ << endl;
os<< book.yearPublish_;
os<<book.hardcover_;
os<< book.price_ << endl;
os<< book.isbn_ << endl;
os<< 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_;
}