Answer to Question #215528 in C++ for Gel

Question #215528
Create a class named MusicalComposition that contains fields for title, composer, and year written. Include a constructor that requires all three values and an appropriate display function. The child class NationalAnthem contains an additional field that holds the name of the anthem’s nation. The child class constructor requires a value for this additional field. The child class also contains a display function. Write a main()function that instantiates objects of each class and demonstrates that the functions work correctly. Save the file as Compositions.cpp.
1
Expert's answer
2021-07-09T03:35:20-0400
#include <iostream>
#include <vector>

using namespace std;

struct Composer {
  string name;
};

class MusicalComposition {
  string title;
  Composer composer;
  int year;

  MusicalComposition(string title, string composerName, int year) {
    this->title = title;
    this->composer->name = composerName;
    this->year = year;
  }

  void display() {
    cout << "MusicanComposer = {" 
         << "title = " << this->title << ", "
         << "composerName = " << this->composer->name << ", "
         << "year = " << this->year << "}" << endl;
  }
};

class NationalAnthem : public MusicalComposition {
  vector<string> anthemNames;

  NationalAnthem(string title, string composerName, int year, vector<string> anthemNames) : MusicalComposition(title, composerName, year) {
    this->anthemNames = anthemNames;
  }

  void display() {
    cout << "NationalAnthem = {" 
         << "title = " << this->title << ", "
         << "composerName = " << this->composer->name << ", "
         << "year = " << this->year << ", "
         << "anthemNames" << this->anthemNames << "}" << endl;
  }
}

int main() {
  MusicalComposition m("Some title", "John", 2021);
  m.display();
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS