fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. // Bill Davern
  7. // June 13, 2025
  8. // Assignment 4, with 5 book objects
  9.  
  10.  
  11. // class Book
  12. // with three private data fields: book title, author, copyright, and price
  13. // four public methods to retrieve fields (called "getters")
  14. // and one public non-default constructor
  15.  
  16. class Book {
  17.  
  18. public:
  19.  
  20. // member function prototypes
  21. void assign (string, string, int, float, string, int); // this is your constructor
  22. string getTitle();
  23. string getAuthor();
  24. int getCopyRightYear();
  25. float getPrice();
  26. string getType();
  27. int getPages();
  28.  
  29.  
  30. private:
  31.  
  32. // data members
  33. string title;
  34. string author;
  35. int copyRightYear;
  36. float price;
  37. string type;
  38. int pages;
  39. };
  40.  
  41.  
  42. // these are the actual member functions
  43.  
  44. // this member function is a "constructor" that will create a new book
  45. void Book::assign (string bookTitle, string bookAuthor, int bookDate, float bookPrice, string bookType, int bookPages) {
  46. title = bookTitle;
  47. author = bookAuthor;
  48. copyRightYear = bookDate;
  49. price = bookPrice;
  50. type = bookType;
  51. pages = bookPages;
  52. }
  53.  
  54. // this member function is a "getter" that will retrieve that book title value
  55. string Book::getTitle() {
  56. return title;
  57. }
  58.  
  59. // this member function is a "getter" that will retrieve the primary book author value
  60. string Book::getAuthor() {
  61. return author;
  62. }
  63.  
  64. // this member function is a "getter" that will retrieve the year the book was copyrighted
  65. int Book::getCopyRightYear() {
  66. return copyRightYear;
  67. }
  68.  
  69. // this member function is a "getter" that will retrieve the list price of the book
  70. float Book::getPrice() {
  71. return price;
  72. }
  73.  
  74. // this member function is a "getter" that will retrieve the type of book, hardcover or soft
  75. string Book::getType() {
  76. return type;
  77. }
  78.  
  79. // this member function is a "getter" that will retrieve the number of pages in the book
  80. int Book::getPages() {
  81. return pages;
  82. }
  83.  
  84. int main()
  85. {
  86.  
  87. cout << "Here are some of my favorite books ...\n" << endl;
  88.  
  89. // Set up space to create 5 instances of the class Book to use with our constructor
  90. Book b1, b2, b3, b4, b5;
  91.  
  92. // Use our constructor to create the first book, replace my book below with info on your favorite book, use b1
  93. b1.assign ("The Art of War", "Sun Tzu", 1998, 29.99, "Hardcover", 232);
  94.  
  95. cout << b1.getTitle() << " authored by " << b1.getAuthor() << " in the year " << b1.getCopyRightYear() << endl;
  96. cout << "The price of this book is: $" << b1.getPrice() << endl;
  97. cout << "The type of book is a " << b1.getType() << " book." << endl;
  98. cout << "The book has " << b1.getPages() << " pages." << endl;
  99. cout << "\n" << endl;
  100.  
  101. // Use the constructor again to create another book, again, replacing my book below with one your favorite books, use b2
  102. b2.assign ("Basic Economics", "Thomas Sowell", 2014, 25.99, "Softcover", 704);
  103.  
  104. cout << b2.getTitle() << " authored by " << b2.getAuthor() << " in the year " << b2.getCopyRightYear() << endl;
  105. cout << "The price of this book is: $" << b2.getPrice() << endl;
  106. cout << "The type of book is a " << b2.getType() << " book." << endl;
  107. cout << "The book has " << b2.getPages() << " pages." << endl;
  108. cout << "\n" << endl;
  109.  
  110. // use constructor (its called assign) again to create and then print information about book 3, another favorite book of yours ... remember to use b3
  111. b3.assign ("The Outsiders", "S. E. Hinton", 1967, 12.98, "Softcover", 192);
  112.  
  113. cout << b3.getTitle() << " authored by " << b3.getAuthor() << " in the year " << b3.getCopyRightYear() << endl;
  114. cout << "The price of this book is: $" << b3.getPrice() << " book." << endl;
  115. cout << "The type of book is a " << b3.getType() << " book." << endl;
  116. cout << "The book has " << b3.getPages() << " pages." << endl;
  117. cout << "\n" << endl;
  118.  
  119. // use constructor again to create and then print information about book 4, your fourth favorite book ... remember to use b4
  120. b4.assign ("VMware vSAN 8.0 U2", "Duncon Epping", 2024, 45.00, "Audio", 632);
  121.  
  122. cout << b4.getTitle() << " authored by " << b4.getAuthor() << " in the year " << b4.getCopyRightYear() << endl;
  123. cout << "The price of this book is: $" << b4.getPrice() << endl;
  124. cout << "The type of book is a " << b4.getType() << " book" << endl;
  125. cout << "The book has " << b4.getPages() << " pages." << endl;
  126. cout << "\n" << endl;
  127. // use constructor again to create and then print information about book 5, your fifth favorite book ... remember to use b5
  128. b5.assign ("Mastering AWS CloudFormation", "Karen Tovmasyan", 2023, 43.13, "Hardcover", 310);
  129.  
  130. cout << b5.getTitle() << " authored by " << b5.getAuthor() << " in the year " << b5.getCopyRightYear() << endl;
  131. cout << "The price of this book is: $" << b5.getPrice() << endl;
  132. cout << "The type of book is a " << b5.getType() << " book." << endl;
  133. cout << "The book has " << b5.getPages() << " pages." << endl;
  134. cout << "\n" << endl;
  135.  
  136. return (0);
  137. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Here are some of my favorite books ...

The Art of War authored by Sun Tzu in the year 1998
The price of this book is:  $29.99
The type of book is a Hardcover book.
The book has 232 pages.


Basic Economics authored by Thomas Sowell in the year 2014
The price of this book is:  $25.99
The type of book is a Softcover book.
The book has 704 pages.


The Outsiders authored by S. E. Hinton in the year 1967
The price of this book is:  $12.98 book.
The type of book is a Softcover book.
The book has 192 pages.


VMware vSAN 8.0 U2 authored by Duncon Epping in the year 2024
The price of this book is:  $45
The type of book is a Audio book
The book has 632 pages.


Mastering AWS CloudFormation authored by Karen Tovmasyan in the year 2023
The price of this book is:  $43.13
The type of book is a Hardcover book.
The book has 310 pages.