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;
  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 ("C++ for Dummies", "Stephen R Davis", 1998, 29.99);
  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 ("C++ - The Complete Reference", "Herbert Schildt", 2003, 52.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. // use constructor again to create and then print information about book 4, your fourth favorite book ... remember to use b4
  90.  
  91. // use constructor again to create and then print information about book 5, your fifth favorite book ... remember to use b5
  92.  
  93. return (0);
  94. }
  95.  
Success #stdin #stdout 0s 4316KB
stdin
Standard input is empty
stdout
Here are some of my favorite books ...

C++ for Dummies authored by Stephen R Davis in the year 1998
The price of this book is:  $29.99


C++ - The Complete Reference authored by Herbert Schildt in the year 2003
The price of this book is:  $52.99