fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. class Book
  6. {
  7. protected:
  8. std::string title_;
  9. std::string author_;
  10.  
  11. public:
  12. Book(std::string title, std::string author)
  13. : title_(title), author_(author) {}
  14.  
  15. virtual void printInfo() {
  16. std::cout << "Book: " << title_ << " by " << author_ << std::endl;
  17. }
  18.  
  19. virtual ~Book() {}
  20. };
  21.  
  22. class EBook : public Book
  23. {
  24. private:
  25. double fileSizeMB_;
  26.  
  27. public:
  28. EBook(std::string title, std::string author, double fileSize)
  29. : Book(title, author), fileSizeMB_(fileSize) {}
  30.  
  31. void printInfo() {
  32. std::cout << "EBook: " << title_ << " by " << author_ << " (" << fileSizeMB_ << " MB)" << std::endl;
  33. }
  34. };
  35.  
  36. int main()
  37. {
  38. std::vector<Book*> library;
  39. library.push_back(new EBook("1984", "George Orwell", 1.2));
  40. library.push_back(new Book("To Kill a Mockingbird", "Harper Lee"));
  41.  
  42. for (Book* b : library) {
  43. b->printInfo();
  44. }
  45.  
  46. for (Book* b : library) {
  47. delete b;
  48. }
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
EBook: 1984 by George Orwell (1.2 MB)
Book: To Kill a Mockingbird by Harper Lee