fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. // class Book
  7. // with four 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(); // these are getter functions
  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. int main()
  65. {
  66. cout << "Here are some of my favorite books ...\n" << endl;
  67.  
  68. // Set up space to create 5 instances of the class Book
  69. Book b1, b2, b3, b4, b5;
  70.  
  71. // Step 1) assign and print book 1
  72. b1.assign("The Pragmatic Programmer", "Andrew Hunt and David Thomas", 1999, 42.35);
  73. cout << b1.getTitle() << " authored by " << b1.getAuthor() << " in the year " << b1.getCopyRightYear() << endl;
  74. cout << "The price of this book is: $" << b1.getPrice() << endl << endl;
  75.  
  76. // Step 2) assign and print book 2
  77. b2.assign("Clean Code", "Robert C. Martin", 2008, 37.50);
  78. cout << b2.getTitle() << " authored by " << b2.getAuthor() << " in the year " << b2.getCopyRightYear() << endl;
  79. cout << "The price of this book is: $" << b2.getPrice() << endl << endl;
  80.  
  81. // Step 3) assign and print book 3
  82. b3.assign("Introduction to Algorithms", "Thomas H. Cormen", 2009, 89.99);
  83. cout << b3.getTitle() << " authored by " << b3.getAuthor() << " in the year " << b3.getCopyRightYear() << endl;
  84. cout << "The price of this book is: $" << b3.getPrice() << endl << endl;
  85.  
  86. // Step 4) assign and print book 4
  87. b4.assign("Effective Modern C++", "Scott Meyers", 2014, 45.00);
  88. cout << b4.getTitle() << " authored by " << b4.getAuthor() << " in the year " << b4.getCopyRightYear() << endl;
  89. cout << "The price of this book is: $" << b4.getPrice() << endl << endl;
  90.  
  91. // Step 5) assign and print book 5
  92. b5.assign("Design Patterns: Elements of Reusable Object-Oriented Software", "Erich Gamma et al.", 1994, 54.95);
  93. cout << b5.getTitle() << " authored by " << b5.getAuthor() << " in the year " << b5.getCopyRightYear() << endl;
  94. cout << "The price of this book is: $" << b5.getPrice() << endl << endl;
  95.  
  96. return 0;
  97. }
  98.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Here are some of my favorite books ...

The Pragmatic Programmer authored by Andrew Hunt and David Thomas in the year 1999
The price of this book is:  $42.35

Clean Code authored by Robert C. Martin in the year 2008
The price of this book is:  $37.5

Introduction to Algorithms authored by Thomas H. Cormen in the year 2009
The price of this book is:  $89.99

Effective Modern C++ authored by Scott Meyers in the year 2014
The price of this book is:  $45

Design Patterns: Elements of Reusable Object-Oriented Software authored by Erich Gamma et al. in the year 1994
The price of this book is:  $54.95