fork(1) 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, string, int); // this is your constructor
  17. string getTitle();
  18. string getAuthor();
  19. int getCopyRightYear();
  20. float getPrice();
  21. string getType();
  22. int getPages();
  23.  
  24.  
  25. private:
  26.  
  27. // data members
  28. string title;
  29. string author;
  30. int copyRightYear;
  31. float price;
  32. string type;
  33. int pages;
  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, string bookType, int bookPages) {
  41. title = bookTitle;
  42. author = bookAuthor;
  43. copyRightYear = bookDate;
  44. price = bookPrice;
  45. type = bookType;
  46. pages = bookPages;
  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. // this member function is a "getter" that will retrieve the type of book, hardcover or soft
  70. string Book::getType() {
  71. return type;
  72. }
  73.  
  74. // this member function is a "getter" that will retrieve the number of pages in the book
  75. int Book::getPages() {
  76. return price;
  77. }
  78.  
  79. int main()
  80. {
  81.  
  82. cout << "Here are some of my favorite books ...\n" << endl;
  83.  
  84. // Set up space to create 5 instances of the class Book to use with our constructor
  85. Book b1, b2, b3, b4, b5;
  86.  
  87. // Use our constructor to create the first book, replace my book below with info on your favorite book, use b1
  88. b1.assign ("The Art of War", "Sun Tzu", 1998, 29.99, "Hardcover", 232);
  89.  
  90. cout << b1.getTitle() << " authored by " << b1.getAuthor() << " in the year " << b1.getCopyRightYear() << endl;
  91. cout << "The price of this book is: $" << b1.getPrice() << endl;
  92. cout << "The type of book is " << b1.getType() << endl;
  93. cout << "The book has " << b1.getPages() << " pages." << endl;
  94. cout << "\n" << endl;
  95.  
  96. // Use the constructor again to create another book, again, replacing my book below with one your favorite books, use b2
  97. b2.assign ("C++ - The Complete Reference", "Herbert Schildt", 2003, 52.99, "Hardcover", 222);
  98.  
  99. cout << b2.getTitle() << " authored by " << b2.getAuthor() << " in the year " << b2.getCopyRightYear() << endl;
  100. cout << "The price of this book is: $" << b2.getPrice() << endl;
  101. cout << "The type of book is " << b2.getType() << endl;
  102. cout << "The book has " << b2.getPages() << " 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.  
  107. // use constructor again to create and then print information about book 4, your fourth favorite book ... remember to use b4
  108.  
  109. // use constructor again to create and then print information about book 5, your fifth favorite book ... remember to use b5
  110.  
  111. return (0);
  112. }
  113.  
Success #stdin #stdout 0s 5332KB
stdin
Standard input is empty
stdout
Here are some of my favorite books ...

The Art of War authored by Sun Tzu in the year 1998
The price of this book is:  $29.99
The type of book is Hardcover
The book has 29 pages.


C++ - The Complete Reference authored by Herbert Schildt in the year 2003
The price of this book is:  $52.99
The type of book is Hardcover
The book has 52 pages.