fork download
  1. // Attached: Lab_10_Part2
  2. // ===========================================================
  3. // File: Lab_10_Part2
  4. // ===========================================================
  5. // Programmer: Elaine Torrez
  6. // Class: CMPR 121
  7. // ===========================================================
  8.  
  9. #include <iostream>
  10. #include <string>
  11. using namespace std;
  12.  
  13. class Book
  14. {
  15. private:
  16. string isbn;
  17. int year;
  18. double price;
  19. static int bookCount;
  20.  
  21. public:
  22. Book()
  23. {
  24. isbn = "";
  25. year = 0;
  26. price = 0.0;
  27. bookCount++;
  28. }
  29.  
  30. Book(string bookIsbn, int bookYear, double bookPrice)
  31. {
  32. isbn = bookIsbn;
  33. year = bookYear;
  34. price = bookPrice;
  35. bookCount++;
  36. }
  37.  
  38. ~Book()
  39. {
  40. }
  41.  
  42. void displayBook() const
  43. {
  44. cout << "ISBN: " << isbn << endl;
  45. cout << "Year: " << year << endl;
  46. cout << "Price: " << price << endl;
  47. }
  48.  
  49. int getCount()
  50. {
  51. return bookCount;
  52. }
  53.  
  54. bool operator>(Book book)
  55. {
  56. return price > book.price;
  57. }
  58.  
  59. bool operator==(Book book)
  60. {
  61. return price == book.price;
  62. }
  63.  
  64. bool operator>(double amount)
  65. {
  66. return price > amount;
  67. }
  68.  
  69. double operator+(Book book)
  70. {
  71. return price + book.price;
  72. }
  73.  
  74. bool operator<(int amount)
  75. {
  76. return year < amount;
  77. }
  78.  
  79. friend ostream& operator<<(ostream& stream, Book& book);
  80. friend istream& operator>>(istream& stream, Book& book);
  81. };
  82.  
  83. int Book::bookCount = 0;
  84.  
  85. ostream& operator<<(ostream& stream, Book& book)
  86. {
  87. stream << "ISBN: " << book.isbn << endl;
  88. stream << "Year: " << book.year << endl;
  89. stream << "Price: " << book.price << endl;
  90.  
  91. return stream;
  92. }
  93.  
  94. istream& operator>>(istream& stream, Book& book)
  95. {
  96. cout << "ISBN: ";
  97. stream >> book.isbn;
  98.  
  99. cout << "Year: ";
  100. stream >> book.year;
  101.  
  102. cout << "Price: ";
  103. stream >> book.price;
  104.  
  105. return stream;
  106. }
  107.  
  108. int main()
  109. {
  110. Book b1("0-12345-9", 1990, 12.50);
  111. Book b2("0-54321-9", 2001, 7.75);
  112. Book b3;
  113. double avg;
  114.  
  115. cout << "Here is book #1:\n";
  116. b1.displayBook();
  117.  
  118. cout << "\nHere is book #2:\n";
  119. b2.displayBook();
  120.  
  121. cout << "\nThere are " << b1.getCount() << " books.\n\n";
  122.  
  123. if (b1 > b2)
  124. cout << "Book #1 has a higher price.\n\n";
  125. else
  126. cout << "Book #1 does not have a higher price.\n\n";
  127.  
  128. if (b1 == b2)
  129. cout << "Same price.\n\n";
  130. else
  131. cout << "Not the same price.\n\n";
  132.  
  133. if (b2 > 10.00)
  134. cout << "The price is more than $10.00.\n\n";
  135. else
  136. cout << "The price is not more than $10.00.\n\n";
  137.  
  138. avg = (b1 + b2) / 2.0;
  139.  
  140. cout << "The average book price is " << avg << ".\n\n";
  141.  
  142. if (b1 < 2000)
  143. cout << "The book was published before 2000.\n\n";
  144. else
  145. cout << "The book was not published before 2000.\n\n";
  146.  
  147. cout << b1;
  148.  
  149. cout << "\nEnter Book #3 information.\n";
  150. cin >> b1;
  151.  
  152. cout << "\nHere is what you entered for Book #3:\n";
  153. cout << b1;
  154.  
  155. return 0;
  156. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Here is book #1:
ISBN:  0-12345-9
Year:  1990
Price: 12.5

Here is book #2:
ISBN:  0-54321-9
Year:  2001
Price: 7.75

There are 3 books.

Book #1 has a higher price.

Not the same price.

The price is not more than $10.00.

The average book price is 10.125.

The book was published before 2000.

ISBN:  0-12345-9
Year:  1990
Price: 12.5

Enter Book #3 information.
ISBN:  Year:  Price: 
Here is what you entered for Book #3:
ISBN:  0-12345-9
Year:  1990
Price: 12.5