Question

IN C++ format, Fill in the missing functions, using the concept of operator overloading. code: #include...

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

};

Homework Answers

Answer #1

#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 << "\n";
os << "$" << book.price << "\n";
os << "isbn #" << book.isbn << "\n";
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

};

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item = 0; 4. while (count < 4) { 5.      cout << "Enter item cost: "; 6.      cin >> item; 7.      item_total += item; 8.      ++count; 9. } 10. int average_cost = round(item_total / count); 11. cout << "Total cost: " << item_total << "\nAverage cost: " << average_cost; (Refer to Code Example 8-1.) If the user enters 5, 10, and 15 at the prompts, the output is: Total...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT