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:...
Using c++ You may #include only: <iostream> <cmath> “functions.h” Write the function void readForceValuesFromStdIn(double* leftTeam, double*...
Using c++ You may #include only: <iostream> <cmath> “functions.h” Write the function void readForceValuesFromStdIn(double* leftTeam, double* rightTeam, unsigned const int noParticipants) that will read the list of forces exerted by each wizard alternating by the team into the correct array of doubles (see example in problem description) Define the function bool validForces(const double* forces, unsigned const int noParticipants) using the provided code. This function will be used to ensure that the forces exerted by each team’s wizard are non-negative The...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with a Program class and write the following two methods (headers provided) as described below: - A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number...
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...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
In the following C code, Which variable if NOT of primitive data type? A. a B....
In the following C code, Which variable if NOT of primitive data type? A. a B. b C. c D. d int a = 10; double b = 20.0; float c = false; char d[5] = "Hello"; // here we define a string In programming language C, the implementation of a string data type is limited dynamic length, which means the length of a string variable is fixed once it has been defined. A. True B. False In C# programming...
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