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. // Updated assign function to accept 7 arguments
  17. void assign (string, string, int, float, string, string, string); // this is your constructor
  18. string getTitle();
  19. string getAuthor();
  20. int getCopyRightYear();
  21. float getPrice();
  22. string getISBN();
  23. string getPublisher();
  24. string getGenre();
  25.  
  26.  
  27. private:
  28.  
  29. // core data members
  30. string title;
  31. string author;
  32. int copyRightYear;
  33. float price;
  34.  
  35. // additional data members
  36. string isbn;
  37. string publisher;
  38. string genre;
  39. };
  40.  
  41.  
  42. // these are the actual member functions
  43.  
  44. // this member function is a "constructor" that will create a new book
  45. void Book::assign (string bookTitle, string bookAuthor, int bookDate, float bookPrice,
  46. string bookISBN, string bookPublisher, string bookGenre) {
  47. title = bookTitle;
  48. author = bookAuthor;
  49. copyRightYear = bookDate;
  50. price = bookPrice;
  51. isbn = bookISBN;
  52. publisher = bookPublisher;
  53. genre = bookGenre;
  54. }
  55.  
  56. // this member function is a "getter" that will retrieve that book title value
  57. string Book::getTitle() {
  58. return title;
  59. }
  60.  
  61. // this member function is a "getter" that will retrieve the primary book author value
  62. string Book::getAuthor() {
  63. return author;
  64. }
  65.  
  66. // this member function is a "getter" that will retrieve the year the book was copyrighted
  67. int Book::getCopyRightYear() {
  68. return copyRightYear;
  69. }
  70.  
  71. // this member function is a "getter" that will retrieve the list price of the book
  72. float Book::getPrice() {
  73. return price;
  74. }
  75.  
  76. // this member function is a "getter" that will retrieve the ISBN of the book
  77. string Book::getISBN() {
  78. return isbn;
  79. }
  80.  
  81. // this member function is a "getter" that will retrieve the publisher of the book
  82. string Book::getPublisher() {
  83. return publisher;
  84. }
  85.  
  86. // this member function is a "getter" that will retrieve the genre of the book
  87. string Book::getGenre() {
  88. return genre;
  89. }
  90.  
  91. int main()
  92. {
  93.  
  94. cout << "Here are some of my favorite books ...\n" << endl;
  95.  
  96. // Set up space to create 5 instances of the class Book to use with our constructor
  97. Book b1, b2, b3, b4, b5;
  98.  
  99. // Use our constructor to create the first book, replace my book below with info on your favorite book, use b1
  100. b1.assign("Atomic Habits", "James Clear", 2018, 21.99, "9780735211292", "Avery", "Self-help");
  101. b2.assign("Deep Work", "Cal Newport", 2016, 18.50, "9781455586691", "Grand Central", "Productivity");
  102. b3.assign("The Alchemist", "Paulo Coelho", 1988, 15.00, "9780061122415", "HarperOne", "Fiction");
  103. b4.assign("Educated", "Tara Westover", 2018, 16.00, "9780399590504", "Random House", "Memoir");
  104. b5.assign("The Pragmatic Programmer", "Andy Hunt", 1999, 42.00, "9780201616224", "Addison-Wesley", "Technology");
  105.  
  106. // Function to print all book details
  107. Book books[] = {b1, b2, b3, b4, b5};
  108. for (int i = 0; i < 5; i++) {
  109. cout << books[i].getTitle() << " authored by " << books[i].getAuthor()
  110. << " in the year " << books[i].getCopyRightYear() << endl;
  111. cout << "Price: $" << books[i].getPrice() << endl;
  112. cout << "ISBN: " << books[i].getISBN() << endl;
  113. cout << "Publisher: " << books[i].getPublisher() << endl;
  114. cout << "Genre: " << books[i].getGenre() << endl;
  115. cout << "\n";
  116. }
  117.  
  118.  
  119. return (0);
  120. }
  121.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Here are some of my favorite books ...

Atomic Habits authored by James Clear in the year 2018
Price: $21.99
ISBN: 9780735211292
Publisher: Avery
Genre: Self-help

Deep Work authored by Cal Newport in the year 2016
Price: $18.5
ISBN: 9781455586691
Publisher: Grand Central
Genre: Productivity

The Alchemist authored by Paulo Coelho in the year 1988
Price: $15
ISBN: 9780061122415
Publisher: HarperOne
Genre: Fiction

Educated authored by Tara Westover in the year 2018
Price: $16
ISBN: 9780399590504
Publisher: Random House
Genre: Memoir

The Pragmatic Programmer authored by Andy Hunt in the year 1999
Price: $42
ISBN: 9780201616224
Publisher: Addison-Wesley
Genre: Technology