Artwork.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. class Artwork {
  5. public:
  6. Artwork(const std::string& author, const std::string& title, int birthYear, const std::string& category)
  7. : author(author), title(title), birthYear(birthYear), category(category) {}
  8. virtual void displayInfo() const {
  9. std::cout << "Author: " << author << ", Title: " << title << ", Birth Year: " << birthYear << ", Category: " << category;
  10. }
  11. virtual ~Artwork() = default;
  12. private:
  13. std::string author;
  14. std::string title;
  15. int birthYear;
  16. std::string category;
  17. };
  18. class Painting : public Artwork {
  19. public:
  20. Painting(const std::string& author, const std::string& title, int birthYear, const std::string& category, const std::string& size)
  21. : Artwork(author, title, birthYear, category), size(size) {}
  22. void displayInfo() const override {
  23. Artwork::displayInfo();
  24. std::cout << ", Size: " << size;
  25. }
  26. private:
  27. std::string size;
  28. };
  29. class Music : public Artwork {
  30. public:
  31. Music(const std::string& author, const std::string& title, int birthYear, const std::string& category, const std::string& content)
  32. : Artwork(author, title, birthYear, category), content(content) {}
  33. void displayInfo() const override {
  34. Artwork::displayInfo();
  35. std::cout << ", Content: " << content;
  36. }
  37. private:
  38. std::string content;
  39. };
  40. class Chamber : public Music {
  41. public:
  42. Chamber(const std::string& author, const std::string& title, int birthYear, const std::string& content, int numPerformers)
  43. : Music(author, title, birthYear, "Chamber", content), numPerformers(numPerformers) {}
  44. void displayInfo() const override {
  45. Music::displayInfo();
  46. std::cout << ", Number of Performers: " << numPerformers;
  47. }
  48. private:
  49. int numPerformers;
  50. };
  51. int main() {
  52. std::vector<Artwork*> artworks;
  53. artworks.push_back(new Painting("Artist", "Painting", 2020, "Painting", "30x40"));
  54. artworks.push_back(new Music("Composer", "Music", 2021, "Music", "Symphony"));
  55. artworks.push_back(new Chamber("Composer", "Chamber", 2022, "Quartet", 4));
  56. for (const auto& artwork : artworks) {
  57. artwork->displayInfo();
  58. std::cout << std::endl;
  59. }
  60. for (const auto& artwork : artworks) {
  61. delete artwork;
  62. }
  63. return 0;
  64. }