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, string, int, float, int); // this is your constructor
  17. string getTitle();
  18. string getAuthor();
  19. string getArtist();
  20. int getCopyRightYear();
  21. float getPrice();
  22. int getPageNumber();
  23.  
  24.  
  25. private:
  26.  
  27. // data members
  28. string title;
  29. string author;
  30. string artist;
  31. int copyRightYear;
  32. float price;
  33. int pageNumber;
  34. };
  35.  
  36.  
  37. // these are the actual member functions
  38.  
  39. // this member function is a "constructor" that will create a new book
  40. void Book::assign (string bookTitle, string bookAuthor, string bookArtist, int bookDate, float bookPrice, int bookPageNumber) {
  41. title = bookTitle;
  42. author = bookAuthor;
  43. artist = bookArtist;
  44. copyRightYear = bookDate;
  45. price = bookPrice;
  46. pageNumber = bookPageNumber;
  47. }
  48.  
  49. // this member function is a "getter" that will retrieve that book title value
  50. string Book::getTitle() {
  51. return title;
  52. }
  53.  
  54. // this member function is a "getter" that will retrieve the primary book author value
  55. string Book::getAuthor() {
  56. return author;
  57. }
  58.  
  59. // this member function is a "getter" that will retrieve the cover artist of the book
  60. string Book::getArtist() {
  61. return artist;
  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 number of pages in the book
  75. int Book::getPageNumber() {
  76. return pageNumber;
  77. }
  78.  
  79.  
  80.  
  81. int main()
  82. {
  83.  
  84. cout << "Here are some of my favorite books ...\n" << endl;
  85.  
  86. // Set up space to create 5 instances of the class Book to use with our constructor
  87. Book b1, b2, b3, b4, b5;
  88.  
  89. // Use our constructor to create the first book, replace my book below with info on your favorite book, use b1
  90. b1.assign ("The Lightning Thief", "Rick Riordan", "John Rocco", 2005, 7.99, 377);
  91.  
  92. cout << b1.getTitle() << " authored by " << b1.getAuthor() << " in the year " << b1.getCopyRightYear() << " with the cover drawn by " << b1.getArtist() << endl;
  93. cout << "The price of this book is: $" << b1.getPrice() << endl;
  94. cout << "This book has " << b1.getPageNumber() << " pages." << endl;
  95. cout << "\n" << endl;
  96.  
  97. // Use the constructor again to create another book, again, replacing my book below with one your favorite books, use b2
  98. b2.assign ("The Lord of the Flies", "William Golding", "Anthony Gross", 1954, 5.99, 224);
  99.  
  100. cout << b2.getTitle() << " authored by " << b2.getAuthor() << " in the year " << b2.getCopyRightYear() << " with the cover drawn by " << b2.getArtist() << endl;
  101. cout << "The price of this book is: $" << b2.getPrice() << endl;
  102. cout << "This book has " << b2.getPageNumber() << " pages." << endl;
  103. cout << "\n" << endl;
  104.  
  105. // use constructor (its called assign) again to create and then print information about book 3, another favorite book of yours ... remember to use b3
  106. b3.assign ("The Hunger Games", "Suzanne Collins", "Tim O'Brien", 2008, 8.89, 374);
  107.  
  108. cout << b3.getTitle() << " authored by " << b3.getAuthor() << " in the year " << b3.getCopyRightYear() << " with the cover drawn by " << b3.getArtist() << endl;
  109. cout << "The price of this book is: $" << b3.getPrice() << endl;
  110. cout << "This book has " << b3.getPageNumber() << " pages." << endl;
  111. cout << "\n" << endl;
  112.  
  113. // use constructor again to create and then print information about book 4, your fourth favorite book ... remember to use b4
  114. b4.assign ("The Fault in Our Stars", "John Green", "Rodrigo Corral", 2012, 9.99, 313);
  115.  
  116. cout << b4.getTitle() << " authored by " << b4.getAuthor() << " in the year " << b4.getCopyRightYear() << " with the cover drawn by " << b4.getArtist() << endl;
  117. cout << "The price of this book is: $" << b4.getPrice() << endl;
  118. cout << "This book has " << b4.getPageNumber() << " pages." << endl;
  119. cout << "\n" << endl;
  120.  
  121. // use constructor again to create and then print information about book 5, your fifth favorite book ... remember to use b5
  122. b5.assign ("Adventures of Huckleberry Finn", "Mark Twain", "E.W. Kemble", 1884, 10.99, 400);
  123.  
  124. cout << b5.getTitle() << " authored by " << b5.getAuthor() << " in the year " << b5.getCopyRightYear() << " with the cover drawn by " << b5.getArtist() << endl;
  125. cout << "The price of this book is: $" << b5.getPrice() << endl;
  126. cout << "This book has " << b5.getPageNumber() << " pages." << endl;
  127. cout << "\n" << endl;
  128.  
  129. return (0);
  130. }
  131.  
Success #stdin #stdout 0.01s 5408KB
stdin
Standard input is empty
stdout
Here are some of my favorite books ...

The Lightning Thief authored by Rick Riordan in the year 2005 with the cover drawn by John Rocco
The price of this book is:  $7.99
This book has 377 pages.


The Lord of the Flies authored by William Golding in the year 1954 with the cover drawn by Anthony Gross
The price of this book is:  $5.99
This book has 224 pages.


The Hunger Games authored by Suzanne Collins in the year 2008 with the cover drawn by Tim O'Brien
The price of this book is:  $8.89
This book has 374 pages.


The Fault in Our Stars authored by John Green in the year 2012 with the cover drawn by Rodrigo Corral
The price of this book is:  $9.99
This book has 313 pages.


Adventures of Huckleberry Finn authored by Mark Twain in the year 1884 with the cover drawn by E.W. Kemble
The price of this book is:  $10.99
This book has 400 pages.