Advertisement
Guest User

Sample Program with Classes

a guest
Nov 20th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cinttypes>
  4.  
  5. class Book {
  6. private:
  7. std::string name, author, publisher;
  8. uint32_t publishYear, pageCount;
  9.  
  10. public:
  11. Book(const std::string name, const std::string author, const std::string publisher, uint32_t publishYear,
  12. uint32_t pageCount) {
  13. this->name = name;
  14. this->author = author;
  15. this->publisher = publisher;
  16. this->publishYear = publishYear;
  17. this->pageCount = pageCount;
  18. }
  19.  
  20. void printBook(bool isList = false) {
  21. if(isList) std::cout << " - ";
  22. std::cout << "Name: " << this->name
  23. << " | Author: " << this->author
  24. << " | Publisher: " << this->publisher
  25. << " | Publish Year: " << this->publishYear
  26. << " | Page Count: " << this->pageCount
  27. << std::endl;
  28. }
  29.  
  30. uint32_t getYear() { return this->publishYear; }
  31. };
  32.  
  33. class Library {
  34. private:
  35. std::string name;
  36. uint32_t bookCount;
  37. Book** books;
  38.  
  39. public:
  40. Library(const std::string name) {
  41. this->name = name;
  42. this->bookCount = 0;
  43. this->books = new Book*[bookCount];
  44. }
  45.  
  46. void insertBooks() {
  47. bool insertMoreBooks = true;
  48. char userDecision;
  49. while(insertMoreBooks) {
  50. std::cout << "Do you want to insert book? (Y/N) ";
  51. std::cin >> userDecision;
  52. if(userDecision == 'N' || userDecision == 'n') {
  53. insertMoreBooks = false;
  54. break;
  55. }
  56.  
  57. std::string name, author, publisher;
  58. uint32_t publishYear, pagesCount;
  59.  
  60. std::cin.ignore();
  61. std::cout << "Name: ";
  62. std::getline(std::cin, name);
  63. std::cout << "Author: ";
  64. std::getline(std::cin, author);
  65. std::cout << "Publisher: ";
  66. std::getline(std::cin, publisher);
  67. std::cout << "Publish Year: ";
  68. std::cin >> publishYear;
  69. std::cout << "Page Count: ";
  70. std::cin >> pagesCount;
  71.  
  72. Book* insertedBook = this->addBook(name, author, publisher, publishYear, pagesCount);
  73. std::cout << "Inserted book: ";
  74. insertedBook->printBook();
  75. }
  76. }
  77.  
  78. Book* addBook(const std::string name, const std::string author, const std::string publisher, uint32_t publishYear,
  79. uint32_t pageCount) {
  80. this->bookCount++;
  81.  
  82. Book** oldBooks = this->books;
  83. this->books = new Book*[bookCount];
  84. for(int i = 0; i < bookCount - 1; i++) {
  85. this->books[i] = oldBooks[i];
  86. }
  87.  
  88. this->books[bookCount-1] = new Book(name, author, publisher, publishYear, pageCount);
  89.  
  90. return this->books[bookCount-1];
  91. }
  92.  
  93. void printBooks() {
  94. for(int i = 0; i < bookCount; i++) {
  95. this->books[i]->printBook(true);
  96. }
  97. }
  98.  
  99. Book* getOldestBook() {
  100. Book* oldestBook = this->books[0];
  101. for(int i = 0; i < bookCount; i++) {
  102. if(this->books[i]->getYear() < oldestBook->getYear()) oldestBook = this->books[i];
  103. }
  104. return oldestBook;
  105. }
  106.  
  107. uint32_t getAvgBookYear() {
  108. uint32_t yearSum = 0;
  109. for(int i = 0; i < bookCount; i++) {
  110. yearSum += this->books[i]->getYear();
  111. }
  112. return static_cast<uint32_t>(yearSum / bookCount);
  113. }
  114. };
  115.  
  116. int main() {
  117. // Create library
  118. Library myLib("My Library");
  119. std::cout << "Librarian, welcome to your new workplace! Be prepared for everything..."
  120. << std::endl
  121. << "--------------------------------------------------"
  122. << std::endl;
  123.  
  124. // Add books to library
  125. //myLib.addBook("Don Quixote", "Miguel de Cervantes", "Ancient Stuff", 1615, 928);
  126. //myLib.addBook("1984", "George Orwell", "Harvill Secker", 1949, 336);
  127. std::cout << "Well, I guess we should start somewhere..."
  128. << std::endl;
  129. myLib.insertBooks();
  130. std::cout << "--------------------------------------------------"
  131. << std::endl;
  132.  
  133. // Print all books
  134. std::cout << std::endl
  135. << "Phew... After all these keystrokes I guess you want to see your work... Look below :D "
  136. << std::endl;
  137. myLib.printBooks();
  138. std::cout << "--------------------------------------------------"
  139. << std::endl;
  140.  
  141. // Find oldest book
  142. std::cout << std::endl
  143. << "But... you must keep an eye on the oldest one because some people would want to steal it."
  144. << std::endl
  145. << "The oldest book is: "
  146. << std::endl;
  147. Book* oldestBook = myLib.getOldestBook();
  148. oldestBook->printBook();
  149. std::cout << "--------------------------------------------------"
  150. << std::endl;
  151.  
  152. // Get avg publish year
  153. std::cout << std::endl
  154. << "The legend says that the last Librarian was born at the average book year in the library when the new one step into the library for the first time..."
  155. << std::endl
  156. << "The last librarian's birth year was "
  157. << myLib.getAvgBookYear()
  158. << std::endl;
  159. std::cout << "--------------------------------------------------"
  160. << std::endl;
  161.  
  162. return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement