fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. // Base class Item
  5. class Item {
  6. protected:
  7. string title;
  8. double price;
  9. public:
  10. Item(const string& t, double p) : title(t), price(p) {}
  11. virtual void display() const {
  12. cout << "Title: " << title << ", Price: $" << price << endl;
  13. }
  14. };
  15. // Subclass Book
  16. class Book : public Item {
  17. protected:
  18. string author;
  19. string genre;
  20. string ISBN;
  21.  
  22. public:
  23. Book(const string& t, double p, const string& a, const string& g, const string& i)
  24. : Item(t, p), author(a), genre(g), ISBN(i) {}
  25.  
  26. void display() const override {
  27. Item::display();
  28. cout << "Author: " << author << ", Genre: " << genre << ", ISBN: " << ISBN << endl;
  29. }
  30. };
  31.  
  32. // Subclass CD
  33. class CD : public Item {
  34. protected:
  35. string artist;
  36. string genre;
  37. double duration;
  38.  
  39. public:
  40. CD(const string& t, double p, const string& a, const string& g, double d)
  41. : Item(t, p), artist(a), genre(g), duration(d) {}
  42.  
  43. void display() const override {
  44. Item::display();
  45. cout << "Artist: " << artist << ", Genre: " << genre << ", Duration: " << duration << " minutes" << endl;
  46. }
  47. };
  48.  
  49. // Further subclass Fiction from Book
  50. class Fiction : public Book {
  51. string categorySpecificData;
  52.  
  53. public:
  54. Fiction(const string& t, double p, const string& a, const string& g, const string& i, const string& csd)
  55. : Book(t, p, a, g, i), categorySpecificData(csd) {}
  56.  
  57. void display() const override {
  58. Book::display();
  59. cout << "Category Specific Data: " << categorySpecificData << endl;
  60. }
  61. };
  62.  
  63. // Further subclass NonFiction from Book
  64. class NonFiction : public Book {
  65. string categorySpecificData;
  66.  
  67. public:
  68. NonFiction(const string& t, double p, const string& a, const string& g, const string& i, const string& csd)
  69. : Book(t, p, a, g, i), categorySpecificData(csd) {}
  70.  
  71. void display() const override {
  72. Book::display();
  73. cout << "Category Specific Data: " << categorySpecificData << endl;
  74. }
  75. };
  76.  
  77. // Further subclass Rock from CD
  78. class Rock : public CD {
  79. string categorySpecificData;
  80.  
  81. public:
  82. Rock(const string& t, double p, const string& a, const string& g, double d, const string& csd)
  83. : CD(t, p, a, g, d), categorySpecificData(csd) {}
  84.  
  85. void display() const override {
  86. CD::display();
  87. cout << "Category Specific Data: " << categorySpecificData << endl;
  88. }
  89. };
  90.  
  91. // Further subclass Pop from CD
  92. class Pop : public CD {
  93. string categorySpecificData;
  94.  
  95. public:
  96. Pop(const string& t, double p, const string& a, const string& g, double d, const string& csd)
  97. : CD(t, p, a, g, d), categorySpecificData(csd) {}
  98.  
  99. void display() const override {
  100. CD::display();
  101. cout << "Category Specific Data: " << categorySpecificData << endl;
  102. }
  103. };
  104.  
  105. int main() {
  106. // Create sample instances of various books and CDs
  107. Fiction fictionBook("The Great Gatsby", 15.99, "F. Scott Fitzgerald", "Fiction", "978-0743273565", "Classic American Literature");
  108. NonFiction nonFictionBook("Sapiens", 19.99, "Yuval Noah Harari", "Non-Fiction", "978-0062316110", "History of Humankind");
  109.  
  110. Rock rockCD("Led Zeppelin IV", 12.99, "Led Zeppelin", "Rock", 42.38, "Classic Rock Album");
  111. Pop popCD("Thriller", 10.99, "Michael Jackson", "Pop", 42.19, "Best-selling Pop Album");
  112.  
  113. // Display details
  114. fictionBook.display();
  115. cout << endl;
  116. nonFictionBook.display();
  117. cout << endl;
  118. rockCD.display();
  119. cout << endl;
  120. popCD.display();
  121.  
  122. return 0;
  123. }
  124.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Title: The Great Gatsby, Price: $15.99
Author: F. Scott Fitzgerald, Genre: Fiction, ISBN: 978-0743273565
Category Specific Data: Classic American Literature

Title: Sapiens, Price: $19.99
Author: Yuval Noah Harari, Genre: Non-Fiction, ISBN: 978-0062316110
Category Specific Data: History of Humankind

Title: Led Zeppelin IV, Price: $12.99
Artist: Led Zeppelin, Genre: Rock, Duration: 42.38 minutes
Category Specific Data: Classic Rock Album

Title: Thriller, Price: $10.99
Artist: Michael Jackson, Genre: Pop, Duration: 42.19 minutes
Category Specific Data: Best-selling Pop Album