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();
  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, int numPages, string ISBNcode) {
  41. title = bookTitle;
  42. author = bookAuthor;
  43. copyRightYear = bookDate;
  44. price = bookPrice;
  45. pages = numPages;
  46. ISBN = ISBNcode;
  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 year the book was copyrighted
  60. int Book::getCopyRightYear() {
  61. return copyRightYear;
  62. }
  63.  
  64. // this member function is a "getter" that will retrieve the list price of the book
  65. float Book::getPrice() {
  66. return price;
  67. }
  68.  
  69.  
  70. // this member function is a "getter" that will retrieve the number of pages of the book
  71. int Book::getPages() {
  72. return pages;
  73. }
  74.  
  75. // this member function is a "getter" that will retrieve the ISBN number of the book
  76. string Book::getISBN() {
  77. return ISBN;
  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 ("Harry Potter and the Sorcerer's Stone", "J.K. Rowling",1998, 15.08, 309, "0590353403");
  91.  
  92. cout << b1.getTitle() << " authored by " << b1.getAuthor() << " in the year " << b1.getCopyRightYear() << endl;
  93. cout << "The price of this book is: $" << b1.getPrice() << endl;
  94. cout <<"The number of pages is: " << b1.getPages() << endl;
  95. cout <<"The ISBN is: " << b1.getISBN() << endl;
  96. cout << "\n" << endl;
  97.  
  98. // Use the constructor again to create another book, again, replacing my book below with one your favorite books, use b2
  99. b2.assign ("Harry Potter and the Chamber of Secrets", "J.K. Rowling", 1999, 20.34, 360, "1408865408");
  100.  
  101. cout << b2.getTitle() << " authored by " << b2.getAuthor() << " in the year " << b2.getCopyRightYear() << endl;
  102. cout << "The price of this book is: $" << b2.getPrice() << endl;
  103. cout <<"The number of pages is: " << b2.getPages() << endl;
  104. cout <<"The ISBN is: " << b2.getISBN() << endl;
  105. cout << "\n" << endl;
  106.  
  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.  
  110. b3.assign ("Harry Potter and the Prisoner of Azkaban", "J.K. Rowling", 1999, 14.58, 435, "0439136350");
  111.  
  112. cout << b3.getTitle() << " authored by " << b3.getAuthor() << " in the year " << b3.getCopyRightYear() << endl;
  113. cout << "The price of this book is: $" << b3.getPrice() << endl;
  114. cout <<"The number of pages is: " << b3.getPages() << endl;
  115. cout <<"The ISBN is: " << b3.getISBN() << endl;
  116. cout << "\n" << endl;
  117.  
  118. // use constructor again to create and then print information about book 4, your fourth favorite book ... remember to use b4
  119.  
  120. b4.assign ("Harry Potter and the Goblet of Fire", "J.K. Rowling", 2000, 18.49, 752, "0439139597");
  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 number of pages is: " << b4.getPages() << endl;
  125. cout <<"The ISBN is: " << b4.getISBN() << endl;
  126. cout << "\n" << endl;
  127.  
  128. // use constructor again to create and then print information about book 5, your fifth favorite book ... remember to use b5
  129.  
  130. b5.assign ("Harry Potter and the Order of the Phoenix", "J.K. Rowling", 2003, 17.29, 896, "9780439358064");
  131.  
  132. cout << b5.getTitle() << " authored by " << b5.getAuthor() << " in the year " << b5.getCopyRightYear() << endl;
  133. cout << "The price of this book is: $" << b5.getPrice() << endl;
  134. cout <<"The number of pages is: " << b5.getPages() << endl;
  135. cout <<"The ISBN is: " << b5.getISBN() << endl;
  136. cout << "\n" << endl;
  137.  
  138. return (0);
  139. }
  140.  
Success #stdin #stdout 0.01s 5440KB
stdin
Standard input is empty
stdout
Here are some of my favorite books ...

Harry Potter and the Sorcerer's Stone authored by J.K. Rowling in the year 1998
The price of this book is:  $15.08
The number of pages is: 309
The ISBN is: 0590353403


Harry Potter and the Chamber of Secrets authored by J.K. Rowling in the year 1999
The price of this book is:  $20.34
The number of pages is: 360
The ISBN is: 1408865408


Harry Potter and the Prisoner of Azkaban authored by J.K. Rowling in the year 1999
The price of this book is:  $14.58
The number of pages is: 435
The ISBN is: 0439136350


Harry Potter and the Goblet of Fire authored by J.K. Rowling in the year 2000
The price of this book is:  $18.49
The number of pages is: 752
The ISBN is: 0439139597


Harry Potter and the Order of the Phoenix authored by J.K. Rowling in the year 2003
The price of this book is:  $17.29
The number of pages is: 896
The ISBN is: 9780439358064