Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Answer this in c++ #include #include #include using namespace std; class Person { public: Person() { setData(unknown-first, unknown-last); } Person(string first, string last) { setData(first,
Answer this in c++
#include
#include
#include
using namespace std;
class Person
{
public:
Person() {
setData("unknown-first", "unknown-last");
}
Person(string first, string last) {
setData(first, last);
}
void setData(string first, string last) {
firstName = first;
lastName = last;
}
void printData() const {
cout << " Name: " << firstName << " " << lastName << endl;
}
private:
string firstName;
string lastName;
};
class Musician : public Person
{
public:
Musician() {
// TODO: set this derived class member
// variable instrument to "unknown-instrument". The base class content will be
// initialized according to the base class's default constructor.
}
Musician(string first, string last, string inst)
// TODO: Before the opening brace.
//construct the base class using the base class parameterized
// constructor. Pass the base class content (first, last)
// up to the base class.
{
// After the opening brace, set the one
// derived class member variable instrument according to the corresponding
// parameter.
}
void setData(string first, string last, string inst) {
// TODO: The base class and derived classes both have setData functions.
// The derived class setData function overrides the base class one. However,
// we can invoke the base class version of the function here if we scope to it.
// Since the member variables first, last are stored
// in the base class, use the base class's setData function to set those
// values according to the corresponding parameters.
// Then set the remaining derived class variable.
}
void printData() const {
Person::printData();
cout << "Instrument: " << instrument << endl;
}
private:
string instrument;
};
class Writer : public Person
{
public:
Writer() {
// TODO: set this derived class member
// variable genre to "unknown-genre". The base class content will be
// initialized according to the base class's default constructor.
}
Writer(string first, string last, string gen)
// TODO: Before the opening brace.
//construct the base class using the base class parameterized
// constructor. Pass the base class content (first, last)
// up to the base class.
{
// After the opening brace, set the one
// derived class member variable genre according to the corresponding
// parameter.
}
void setData(string first, string last, string gen) {
// TODO: The base class and derived classes both have setData functions.
// The derived class setData function overrides the base class one. However,
// we can invoke the base class version of the function here if we scope to it.
// Since the member variables first, last are stored
// in the base class, use the base class's setData function to set those
// values according to the corresponding parameters.
// Then set the remaining derived class variable.
}
void printData() const {
Person::printData();
cout << "Genre: " << genre << endl;
}
private:
string genre;
};
int main() {
int numberOfMusicians = 0;
int numberOfWriters = 0;
string str1, str2, str3;
ifstream fin;
// TODO: Open the file music.txt. Print an error message and exit
// the program if the file cannot be opened. 5 pts.
// TODO: Read the number at the beginning of the text file. That number
// indicates the number of musicians in the list (which is also the length
// of the musicians' array). 5 pts.
//
// Dynamically allocate an array of musician objects. The array should be
// the exact length needed to read the file. 10 pts.
//
// Use a for-loop to load the array with the content from the text file. 15 pts.
//
// Close the text file when you are done with it. 5 pts.
// TODO: Open the file writer.txt. Print an error message and exit
// the program if the file cannot be opened. 5 pts.
// TODO: Read the number at the beginning of the text file. That number
// indicates the number of writers in the list (which is also the length
// of the writers' array). 5 pts.
//
// Dynamically allocate an array of writer objects. The array should be
// the exact length needed to read the file. 10 pts.
//
// Use a for-loop to load the array with the content from the text file. 15 pts.
//
// Close the text file when you are done with it. 5 pts.
// TODO: Loop through the musicians' array and print all of its data.
// Then loop through the writers' array and print all of its data. 15 pts.
// Remember to release all dynamically allocated memory before exiting. 5 pts.
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started