I'm really really stuck. What i'm supposed to do is, first, create a class called Book having the private attributes: title(string), publisher(string), and number of pages(int). For the Book class there are supposed to be two constructors: a default (setting title and publisher to some default value, and number of pages to zero), next is an overloaded constructor that takes 3 parameters and sets title, publisher, and pages accordingly.

I'm unsure whether i wrote the set method mutators and the get method accessors correctly... but what i'm REALLY confused about it where i have to overload the "<<" operator so that a Book object gets output like this ( cout << b; if b is an object of type Book):

TITLE: title
PUBLISHER: publisher
NUMBER OF PAGES: number of pages

code:

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <fstream>

using namespace std;

//Class for type "book"
class Book
{
	public:
	
		Book();
		Book(string new_title, string new_publisher, int new_pages);
		
		string get_new_title();
		//function that gets private "title" 
		string get_new_publisher();
		//function that gets private "publisher"
		int get_new_pages();
		//function that gets private "pages"
		void set_t(string new_title);
		//function that sets title
		void set_p(string new_publisher);
		//function that sets publisher
		void set_np(int new_pages);
		//function that sets pages
		friend ostream& operator <<(ostream& outs, const Book& b);		

	private:
		string title;

		string publisher;

		int pages;

			
};

class Library
{
	public:
	vector<Book> books;


	bool contains(string title);
	//checks to make sure the book doesn't already exist
	void addBook(Book b);
	//adds books to the library
	void readFile(string filname);
	//reads file containing information

};

int main()
{
	Book b;
	ifstream in_stream;
	in_stream.open("Library.txt");
	
	
	
	int choice;
	string filename;
	do
	{

		cout << "1. Read information about books from file." << endl;
		cout << "2. Find book." << endl;
		cout << "3. Add book to library." << endl;
		cout << "4. Print out the library's book collection." << endl;
		cout << "5. Quit program." << endl;
		cin >> choice;
		
		switch(choice)
		{
			case 1:
				
				readFile(filename);
								
				break;
				
			case 2:
			
				break;
				
			case 3:
	
				break;
				
			case 4:
				
				break;
				
			case 5:
				cout << "GOODBYE!!!!" << endl;			
				break;
			
		}
		
		
		
	}while(choice !=5);
	
	
	
	
	
	
	
	in_stream.close();
	
	return 0;
}

Book::Book() : title(), publisher(), pages(0) 
{
	//intentionally left blank
}

Book::Book(string new_title, string new_publisher, int new_pages)
{
	set_t(new_title);
	set_p(new_publisher);
	set_np(new_pages);
	
}

string Book::get_new_title()//function that accesses, "gets", the private data from class book
{
	return title;
}

string Book::get_new_publisher()//gets private publisher
{
	return publisher;
}

int Book::get_new_pages()//gets private pages
{
	return pages;
}

void Book::set_t(string new_title)
{
	new_title = title;
}

void Book::set_p(string new_publisher)
{
	new_publisher = publisher;
}

void Book::set_np(int new_pages)
{
	new_pages = pages;
}	
	
ostream& operator <<(ostream& outs, const Book& b)
{
	


}	

bool contains(string title)
{
	if(!contains(Book& books[], Book& string title))
	{
		cout << "Book not already stored in library" << endl;
	}
	else
		cout << "Book already exists in library" << endl;

}	

void Library::addBook(Book b)
{

	string title;
	string publisher;
	int pages;
	
	cout << "Enter the title of the book: " << endl;
	cin.ignore(100, '\n');
	getline(cin, title);
	books.push_back(title);
	cout << "Enter the publisher: " << endl;
	getline(cin, publisher);
	books.push_back(publisher);
	cout << "Enter the number of pages: " << endl;
	cin >> pages;
	books.push_back(pages);
}	

void Library::readFile(string filename)
{
	cout << "HI!" << endl;
}

Recommended Answers

All 4 Replies

The definition can be in any file, since its not a class member. your code seems fine to me. inside the defintion, you can decide what all you need to output when you do cout << book.

ostream& operator <<(ostream& outs, const Book& b)
{
	
     outs << b.publisher << " " << b.title << " " << b.price << endl;
     return outs;
}

Since title, price and publisher are declared with private access within the class the << operator will need to use public accessor methods to do it's job unless it is declared as a friend function within the class.

that's exactly what he's done i guess ...

My bad. Guess I need another cup of coffee so I can open my eyes better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.