fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. // class Book
  7. // with three private data fields: book title, author, copyright, and price
  8. // four public methods to retrieve fields (called "getters")
  9. // and one public non-default constructor
  10.  
  11. class Book {
  12.  
  13. public:
  14.  
  15. // member function prototypes
  16. void assign (string, string, int, float); // this is your constructor
  17. string getTitle();
  18. string getAuthor();
  19. int getCopyRightYear();
  20. float getPrice();
  21.  
  22.  
  23. private:
  24.  
  25. // data members
  26. string title;
  27. string author;
  28. int copyRightYear;
  29. float price;
  30. };
  31.  
  32.  
  33. // these are the actual member functions
  34.  
  35. // this member function is a "constructor" that will create a new book
  36. void Book::assign (string bookTitle, string bookAuthor, int bookDate, float bookPrice) {
  37. title = bookTitle;
  38. author = bookAuthor;
  39. copyRightYear = bookDate;
  40. price = bookPrice;
  41. }
  42.  
  43. // this member function is a "getter" that will retrieve that book title value
  44. string Book::getTitle() {
  45. return title;
  46. }
  47.  
  48. // this member function is a "getter" that will retrieve the primary book author value
  49. string Book::getAuthor() {
  50. return author;
  51. }
  52.  
  53. // this member function is a "getter" that will retrieve the year the book was copyrighted
  54. int Book::getCopyRightYear() {
  55. return copyRightYear;
  56. }
  57.  
  58. // this member function is a "getter" that will retrieve the list price of the book
  59. float Book::getPrice() {
  60. return price;
  61. }
  62.  
  63.  
  64.  
  65. int main()
  66. {
  67.  
  68. cout << "Here are some of my favorite books ...\n" << endl;
  69.  
  70. // Set up space to create 5 instances of the class Book to use with our constructor
  71. Book b1, b2, b3, b4, b5, b6, b7;
  72.  
  73. // Use our constructor to create the first book, replace my book below with info on your favorite book, use b1
  74. b1.assign ("Harry Potter and the Sorcerers Stone", "J.K. Rowling", 1997, 35.23);
  75.  
  76. cout << b1.getTitle() << " authored by " << b1.getAuthor() << " in the year " << b1.getCopyRightYear() << endl;
  77. cout << "The price of this book is: $" << b1.getPrice() << endl;
  78. cout << "\n" << endl;
  79.  
  80. // Use the constructor again to create another book, again, replacing my book below with one your favorite books, use b2
  81. b2.assign ("Harry Potter and the Cursed Child", "J.K. Rowling", 2016, 35.99);
  82.  
  83. cout << b2.getTitle() << " authored by " << b2.getAuthor() << " in the year " << b2.getCopyRightYear() << endl;
  84. cout << "The price of this book is: $" << b2.getPrice() << endl;
  85. cout << "\n" << endl;
  86.  
  87. // use constructor (its called assign) again to create and then print information about book 3, another favorite book of yours ... remember to use b3
  88.  
  89. b3.assign ("Harry Potter and the Chamber of Secrets", "J.K. Rowling", 1998, 37.23);
  90.  
  91. cout << b3.getTitle() << " authored by " << b3.getAuthor() << " in the year " << b3.getCopyRightYear() << endl;
  92. cout << "The price of this book is: $" << b3.getPrice() << endl;
  93.  
  94. // use constructor again to create and then print information about book 4, your fourth favorite book ... remember to use b4
  95.  
  96. b4.assign ("Lord of the Rings", "J.R.R Tolkien", 1954, 17.00);
  97.  
  98. cout << b4.getTitle() << " authored by " << b4.getAuthor() << " in the year " << b4.getCopyRightYear() << endl;
  99. cout << "The price of this book is: $" << b4.getPrice() << endl;
  100.  
  101. // use constructor again to create and then print information about book 5, your fifth favorite book ... remember to use b5
  102.  
  103. b5.assign ("Harry Potter and the Order of the Pheonix", "J.K. Rowling", 2003, 34.99);
  104.  
  105. cout << b5.getTitle() << " authored by " << b5.getAuthor() << " in the year " << b5.getCopyRightYear() << endl;
  106. cout << "The price of this book is: $" << b5.getPrice() << endl;
  107.  
  108. // Extra Book entires
  109.  
  110. b6.assign ("Harry Potter and the Goblet of Fire", "J.K. Rowling", 2000, 31.99);
  111.  
  112. cout << b6.getTitle() << " authored by " << b6.getAuthor() << " in the year " << b6.getCopyRightYear() << endl;
  113. cout << "The price of this book is: $" << b6.getPrice() << endl;
  114.  
  115.  
  116. return (0);
  117. }
  118.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Here are some of my favorite books ...

Harry Potter and the Sorcerers Stone authored by J.K. Rowling in the year 1997
The price of this book is:  $35.23


Harry Potter and the Cursed Child authored by J.K. Rowling in the year 2016
The price of this book is:  $35.99


Harry Potter and the Chamber of Secrets authored by J.K. Rowling in the year 1998
The price of this book is:  $37.23
Lord of the Rings authored by J.R.R Tolkien in the year 1954
The price of this book is:  $17
Harry Potter and the Order of the Pheonix authored by J.K. Rowling in the year 2003
The price of this book is:  $34.99
Harry Potter and the Goblet of Fire authored by J.K. Rowling in the year 2000
The price of this book is:  $31.99