Help implement this Class Please

[/#ifndef BOOK_H
#define BOOK_H
#include <iostream>
#include <string>
using namespace std;
class Book {
/**
* @param is the input stream
* @param book the book object to be filled
* @return the input stream
*/
friend istream& operator >> (istream& is, Book& book);
/**
* @param os the output stream
* @param book the book object reference * @return the output stream
*/
friend ostream& operator << (ostream& os, const Book& book);
public:
static const int MAX_AUTHORS = 35;
Book();
Book(string title_, string authors_[], int authorCount_, string publisher_,
short yearPublish_, bool hardcover_, float price_,
string isbn_, long copies_);
void setTitle(string title_); string getTitle();
void setAuthors (string authors_[]); string getAuthors();
void setAuthorCount(int authorCount_); int getAuthorCount();
void setPublisher(string publisher_); string getPublisher();
void setIsbn(string isbn_); string getIsbn();
void setHardcover(boolean hardcover_); boolean getHardcover();
void setPrice(float price_); float getPrice();
void setYearPublish(short yearPublish_); short getYearPublish();
void setCopies(long copies_); long getCopies();

private:
string title;
string authors[Book::MAX_AUTHORS];
int authorCount;
string publisher;
short yearPublish;
bool hardcover;
float price;
string isbn;
long copies;

};
#endif /* BOOK_H */


// @file Warehouse.h
#ifndef WAREHOUSE_H
#define WAREHOUSE_H
#include <iostream>
#include <string>
#include "Book.h"
using namespace std;
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 */]
Last edited on
OK, you've managed to dump your assignment on us, what do you actually need help with?

We're not here to do your work for you.

I just felt stuck because I did give it a try. Following is the implementation I tried for Book.cpp.
However, I wanted to know how to make this work without using an array if there are more than 1 book that is going to be read from the file.
[/#include "Book.h"
#include <string>

using namespace std;

static const int MAX_BOOKS = 35;
static const int MAX_AUTHORS = 20;

istream& operator >> (istream& is, Book& book){
Book book[MAX_BOOKS];
while (is.eof()){
for (int i=0; i<MAX_BOOKS;i++){
is >> book[i].title;
is.ignored ();
is >> book[i].authorCount;
is.ignored ();
for (int j=0; j< book[i].authorCount; j++){
is >> book[i].author[j];
is.ignored ();
}
is >> book[i].publisher;
is.ignored ();
is >> book[i].yearPublish;
is.ignored ();
is >> book[i].hardcover;
is.ignored ();
is >> book[i].price;
is.ignored ();
is >> book[i].isbn;
is.ignored ();
is >> book[i].copies;
is.ignored ();
}
}
return is;
}
ostream& operator << (ostream& os, const Book& book){
for (int i=0; i<MAX_BOOKS;i++){
cout << "Title: ";
cout << book[i].title<<endl;
for (int j=0; j<book[i].authorCount;j++){
cout << "Author: ";
cout << book[i].authors[j]<<endl;
}
cout << "Publisher: ";
cout << book[i].publisher << endl;
cout << "Year Published: ";
cout << book[i].yearPublish <<endl;
if (book[i].hardcover){
cout << "Cover: ";
cout << "Hardcover" <<endl;
}
if(!book[i].hardcover){
cout << "Cover: ";
cout << "Paperback" << endl;
cout << "Price: ";
cout << book[i].price << endl;
cout << "Isbn: ";
cout << book[i].price <<endl;
cout << "Copies: ";
cout << book[i].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(){
return title;
}
void Book::setAuthorCount(int authorCount_){
authorCount = authorCount_;
}
void Book::setAuthors(string authors_[]){
for (int i=0;i<authorCount;i++){
authors[i] = authors_[i];
}
}
string Book::getAuthors(){
return authors[];
}
void Book::setPublisher(string publisher_){
publisher = publisher_;
}
string Book::getPublisher(){
return publisher;
}
void Book::setHardcover(bool hardcover_){
hardcover = hardcover_;
}
bool Book::getHardcover(){
return hardcover;
}
void Book::setIsbn(string isbn_){
isbn = isbn_;
}
string Book::getIsbn(){
return isbn;
}
void Book::setPrice(float price_){
price = price_;
}
float Book::getPrice(){
return price;
}
void Book::setCopies(long copies_){
copies = copies_;
}
long Book::getCopies(){
return copies;
}]
Last edited on
Please edit your post(s) to put [code][/code] tags around the code.

Your << and >> operators should only deal with ONE book (the one passed as a parameter), not an array of books.
The thing is, it would read a file and store the books.

If the >> operator only read the first book from a file of books, how would I be able to read the next book?

C++ Network Programming – Volume 1
2
Douglas C. Schmidt
Stephen D. Huston
Addison-Wesley
2002
0
35.99
0-201-60464-7
236
Programming Ruby
2
David Thomas
Andrew Hunt
Addison-Wesley
2001
0
42.95
0-201-71089-7
123
Problem Solving with C++ - The Object of Programming
1
Walter Savitch
Addison Wesley Longman, Inc.
2001
0
74.00
0-201-70390-4
325

This is my book.dat

Thank you so much for the help, I'm just really confused
Edit your posts to put in code tags as I mentioned, then people will read your code.
Topic archived. No new replies allowed.