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, int, string); // this is your constructor
  17. string getTitle(); // these are getter functions
  18. string getAuthor();
  19. int getCopyRightYear();
  20. float getPrice();
  21. int getPages();
  22. string getISBN();
  23.  
  24.  
  25. private:
  26.  
  27. // data members
  28. string title;
  29. string author;
  30. int copyRightYear;
  31. float price;
  32. int pages;
  33. string isbn;
  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, int bookDate, float bookPrice,
  41. int bookPages, string bookISBN) {
  42. title = bookTitle;
  43. author = bookAuthor;
  44. copyRightYear = bookDate;
  45. price = bookPrice;
  46. pages = bookPages;
  47. isbn = bookISBN;
  48. }
  49.  
  50. // this member function is a "getter" that will retrieve that book title value
  51. string Book::getTitle() {
  52. return title;
  53. }
  54.  
  55. // this member function is a "getter" that will retrieve the primary book author value
  56. string Book::getAuthor() {
  57. return author;
  58. }
  59.  
  60. // this member function is a "getter" that will retrieve the year the book was copyrighted
  61. int Book::getCopyRightYear() {
  62. return copyRightYear;
  63. }
  64.  
  65. // this member function is a "getter" that will retrieve the list price of the book
  66. float Book::getPrice() {
  67. return price;
  68. }
  69.  
  70. // this member function is a "getter" that will retrieve the number of pages
  71. int Book::getPages() {
  72. return pages;
  73. }
  74.  
  75. // this member function is a "getter" that will retrieve the ISBN
  76. string Book::getISBN() {
  77. return isbn;
  78. }
  79.  
  80.  
  81.  
  82. int main()
  83. {
  84.  
  85. cout << "Here are some of my favorite books ...\n" << endl;
  86.  
  87. // Set up space to create 5 instances of the class Book to use with our constructor
  88. Book b1, b2, b3, b4, b5;
  89.  
  90. // Use our constructor to create the first book, replace my book below with info on your favorite book, use b1
  91. b1.assign ("The Hobbit", "J.R.R. Tolkien", 1937, 14.99, 310, "9780547928227");
  92.  
  93. cout << b1.getTitle() << " authored by " << b1.getAuthor() << " in the year " << b1.getCopyRightYear() << endl;
  94. cout << "The price of this book is: $" << b1.getPrice() << endl;
  95. cout << "Number of pages: " << b1.getPages() << endl;
  96. cout << "ISBN: " << b1.getISBN() << endl;
  97. cout << "\n" << endl;
  98.  
  99. // Use the constructor again to create another book, again, replacing my book below with one your favorite books, use b2
  100. b2.assign ("Harry Potter and the Sorcerer's Stone", "J.K. Rowling", 1997, 10.99, 309, "9780590353427");
  101.  
  102. cout << b2.getTitle() << " authored by " << b2.getAuthor() << " in the year " << b2.getCopyRightYear() << endl;
  103. cout << "The price of this book is: $" << b2.getPrice() << endl;
  104. cout << "Number of pages: " << b2.getPages() << endl;
  105. cout << "ISBN: " << b2.getISBN() << endl;
  106. cout << "\n" << endl;
  107.  
  108. // use constructor (its called assign) again to create and then print information about book 3, another favorite book of yours ... remember to use b3
  109. b3.assign ("Dune", "Frank Herbert", 1965, 18.99, 896, "9780441013593");
  110.  
  111. cout << b3.getTitle() << " authored by " << b3.getAuthor() << " in the year " << b3.getCopyRightYear() << endl;
  112. cout << "The price of this book is: $" << b3.getPrice() << endl;
  113. cout << "Number of pages: " << b3.getPages() << endl;
  114. cout << "ISBN: " << b3.getISBN() << endl;
  115. cout << "\n" << endl;
  116.  
  117. // use constructor again to create and then print information about book 4, your fourth favorite book ... remember to use b4
  118. b4.assign ("The Martian", "Andy Weir", 2011, 15.99, 384, "9780553418026");
  119.  
  120. cout << b4.getTitle() << " authored by " << b4.getAuthor() << " in the year " << b4.getCopyRightYear() << endl;
  121. cout << "The price of this book is: $" << b4.getPrice() << endl;
  122. cout << "Number of pages: " << b4.getPages() << endl;
  123. cout << "ISBN: " << b4.getISBN() << endl;
  124. cout << "\n" << endl;
  125.  
  126. // use constructor again to create and then print information about book 5, your fifth favorite book ... remember to use b5
  127. b5.assign ("Pride and Prejudice", "Jane Austen", 1813, 9.99, 279, "9781503290563");
  128.  
  129. cout << b5.getTitle() << " authored by " << b5.getAuthor() << " in the year " << b5.getCopyRightYear() << endl;
  130. cout << "The price of this book is: $" << b5.getPrice() << endl;
  131. cout << "Number of pages: " << b5.getPages() << endl;
  132. cout << "ISBN: " << b5.getISBN() << endl;
  133. cout << "\n" << endl;
  134.  
  135. return (0);
  136. }
  137.  
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
Here are some of my favorite books ...

The Hobbit authored by J.R.R. Tolkien in the year 1937
The price of this book is:  $14.99
Number of pages: 310
ISBN: 9780547928227


Harry Potter and the Sorcerer's Stone authored by J.K. Rowling in the year 1997
The price of this book is:  $10.99
Number of pages: 309
ISBN: 9780590353427


Dune authored by Frank Herbert in the year 1965
The price of this book is:  $18.99
Number of pages: 896
ISBN: 9780441013593


The Martian authored by Andy Weir in the year 2011
The price of this book is:  $15.99
Number of pages: 384
ISBN: 9780553418026


Pride and Prejudice authored by Jane Austen in the year 1813
The price of this book is:  $9.99
Number of pages: 279
ISBN: 9781503290563