fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Book {
  7. public:
  8. void assign(string, string, int, float); // constructor
  9. string getTitle();
  10. string getAuthor();
  11. int getCopyRightYear();
  12. float getPrice();
  13.  
  14. private:
  15. string title;
  16. string author;
  17. int copyRightYear;
  18. float price;
  19. };
  20.  
  21. // constructor function
  22. void Book::assign(string bookTitle, string bookAuthor, int bookDate, float bookPrice) {
  23. title = bookTitle;
  24. author = bookAuthor;
  25. copyRightYear = bookDate;
  26. price = bookPrice;
  27. }
  28.  
  29. // getter methods
  30. string Book::getTitle() {
  31. return title;
  32. }
  33.  
  34. string Book::getAuthor() {
  35. return author;
  36. }
  37.  
  38. int Book::getCopyRightYear() {
  39. return copyRightYear;
  40. }
  41.  
  42. float Book::getPrice() {
  43. return price;
  44. }
  45.  
  46. int main() {
  47. cout << "Here are some of my favorite books ...\n" << endl;
  48.  
  49. Book b1, b2, b3, b4, b5;
  50.  
  51. // Book 1
  52. b1.assign("Atomic Habits", "James Clear", 2018, 11.98);
  53. cout << b1.getTitle() << " authored by " << b1.getAuthor() << " in the year " << b1.getCopyRightYear() << endl;
  54. cout << "The price of this book is: $" << b1.getPrice() << "\n" << endl;
  55.  
  56. // Book 2
  57. b2.assign("Dune", "Frank Herbert", 1965, 9.99);
  58. cout << b2.getTitle() << " authored by " << b2.getAuthor() << " in the year " << b2.getCopyRightYear() << endl;
  59. cout << "The price of this book is: $" << b2.getPrice() << "\n" << endl;
  60.  
  61. // Book 3
  62. b3.assign("Born a Crime", "Trevor Noah", 2016, 14.99);
  63. cout << b3.getTitle() << " authored by " << b3.getAuthor() << " in the year " << b3.getCopyRightYear() << endl;
  64. cout << "The price of this book is: $" << b3.getPrice() << "\n" << endl;
  65.  
  66. // Book 4
  67. b4.assign("The Pragmatic Programmer", "Andrew Hunt & David Thomas", 1999, 42.35);
  68. cout << b4.getTitle() << " authored by " << b4.getAuthor() << " in the year " << b4.getCopyRightYear() << endl;
  69. cout << "The price of this book is: $" << b4.getPrice() << "\n" << endl;
  70.  
  71. // Book 5
  72. b5.assign("The Martian", "Andy Weir", 2011, 12.99);
  73. cout << b5.getTitle() << " authored by " << b5.getAuthor() << " in the year " << b5.getCopyRightYear() << endl;
  74. cout << "The price of this book is: $" << b5.getPrice() << "\n" << endl;
  75.  
  76. return 0;
  77. }
  78.  
  79.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Here are some of my favorite books ...

Atomic Habits authored by James Clear in the year 2018
The price of this book is:  $11.98

Dune authored by Frank Herbert in the year 1965
The price of this book is:  $9.99

Born a Crime authored by Trevor Noah in the year 2016
The price of this book is:  $14.99

The Pragmatic Programmer authored by Andrew Hunt & David Thomas in the year 1999
The price of this book is:  $42.35

The Martian authored by Andy Weir in the year 2011
The price of this book is:  $12.99