IN C format Fill in the missing functions using the concept
IN C++ format, Fill in the missing functions, using the concept of operator overloading.
code:
#include <string>
#include <ostream>
using namespace std;
class Book {
private:
string title;
string author;
unsigned isbn;
double price;
public:
/**Constructors*/
Book();
Book(string t, string a, unsigned i, double p);
/**Access Functions*/
string get_title();
string get_author();
unsigned get_isbn();
double get_price();
/**Manipulation Procedures*/
void set_title(string t);
void set_author(string a);
void set_isbn(unsigned i);
void set_price(double p);
/**Additional Functions*/
friend ostream& operator<<(ostream& os, const Book& book);
//prints out a book to the designated stream in the following format
//<title> by <author>
//$<price>
//isbn #<isbn>
//note that the << is required to be a friend function, not a member function
//note2: do not print out the <> as part of the output
bool operator==(const Book& book);
//compares two books to determine if they are the same book
bool operator<(const Book& book);
//compares two books to determine if one comes before the other
//alphabetically by title and secondarily by author if the two
//books contain the same title
//returns false if the two books are the same
bool operator>(const Book& book);
//compares two books to determine if one comes after the other
//alphabetically by title and secondarily by author if the two
//books contain the same title
//returns false if the two books are the same
};
Solution
#include <string>
#include <ostream>
using namespace std;
class Book {
private:
string title;
string author;
unsigned isbn;
double price;
public:
/**Constructors*/
Book(){
}
Book(string t, string a, unsigned i, double p){
title = t;
author = a;
isbn = i;
price = p;
}
/**Access Functions*/
string get_title(){
return title;
}
string get_author(){
return author;
}
unsigned get_isbn(){
return isbn;
}
double get_price(){
return price;
}
/**Manipulation Procedures*/
void set_title(string t){
title = t;
}
void set_author(string a){
author = a;
}
void set_isbn(unsigned i){
isbn = i;
}
void set_price(double p){
price = p;
}
/**Additional Functions*/
friend ostream& operator<<(ostream& os, const Book& book){
os << book.title << \" by \" << book.author << \"\ \";
os << \"$\" << book.price << \"\ \";
os << \"isbn #\" << book.isbn << \"\ \";
return os;
}
//prints out a book to the designated stream in the following format
//<title> by <author>
//$<price>
//isbn #<isbn>
//note that the << is required to be a friend function, not a member function
//note2: do not print out the <> as part of the output
bool operator==(const Book& book){
if(this->title.compare(book.title) == 0 && this->author.compare(book.author) == 0 && this->price == book.price && this->isbn == book.isbn)
return true;
else
return false;
}
//compares two books to determine if they are the same book
bool operator<(const Book& book){
if(this->title.compare(book.title) == 0 && this->author.compare(book.author) == 0 && this->price == book.price && this->isbn == book.isbn)
return false;
else{
if(this->title.compare(book.title) < 0) return true;
else if(this->title.compare(book.title) > 0) return false;
else{
if(this->author.compare(book.author) < 0) return true;
else return false;
}
}
}
//compares two books to determine if one comes before the other
//alphabetically by title and secondarily by author if the two
//books contain the same title
//returns false if the two books are the same
bool operator>(const Book& book){
if(this->title.compare(book.title) == 0 && this->author.compare(book.author) == 0 && this->price == book.price && this->isbn == book.isbn)
return false;
else{
if(this->title.compare(book.title) > 0) return true;
else if(this->title.compare(book.title) < 0) return false;
else{
if(this->author.compare(book.author) > 0) return true;
else return false;
}
}
}
//compares two books to determine if one comes after the other
//alphabetically by title and secondarily by author if the two
//books contain the same title
//returns false if the two books are the same
};


