Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with C++ ! Please answer this question as soon as possible! Objectives: Working with pointers and dynamic memory allocation. Determining the length of

Need help with C++ ! Please answer this question as soon as possible!

Objectives:

  • Working with pointers and dynamic memory allocation.
  • Determining the length of an array at run-time.
  • Implementing dynamically allocated arrays of class objects.

Instructions:

Download from Canvas Lab09.cpp (the given code) and the two input files music.txt and writer.txt. Read the given code. There is a base class called Person which stores a persons firstName and lastName. There is a Musician derived class and a Writer derived class (both derived from Person). A musician is a person. A writer is a person. The Musician derived class holds an additional member variable for the musicians instrument. The Writer derived class holds an additional member variable for the writers genre. These classes are implemented for you.

1)Please implement the Musician derived class and a Writer derived class.

2)Please implement the main() function to accomplish the following tasks:

  • Scan the data from the input files and load that data in an array of Musician objects and an array of Writer objects.
  • The two arrays should be allocated dynamically, so they are the exact length that is required according to their respective input file.
  • Print the content of each array before releasing the memory and exiting the program.
  • The variables numberOfMusicians and numberOfWriters (shown below) are intended to store the length of the corresponding arrays. The string variables str1, str2, and str3 are intended to temporarily hold firstName, lastName, and third variable (instrument or genre) as each line is read from its input file and then set into its object.

Below is the given code:

#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

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

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

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; }

music.txt

6 Miles Davis Trumpet John Coltrane Saxophone Keith Jarrett Piano Keith Richards Guitar Ella Fitzgerald Voice Paul Motian Percussion

Writer.txt

7 Tobias Wolff Memoir August Wilson Playwright Te-Ping Chen Journalist Stanislaw Lem SciFi Gertrude Stein Novelist Hunter Thompson Gonzo Khaled Hosseini Novelist

image text in transcribed

image text in transcribed

Sample Output: Name: Miles Davis Instrument: Trumpet Name: John Coltrane Instrument: Saxophone Name: Keith Jarrett Instrument: Piano Name: Keith Richards Instrument: Guitar Name: Ella Fitzgerald Instrument: Voice Name: Paul Motian Instrument: Percussion Name: Tobias Wolff Genre: Memoir Name: August Wilson Genre: Playwright Name: Te-Ping Chen Genre: Journalist Name: Stanislaw Lem Genre: SciFi Name: Gertrude Stein Genre: Novelist Name: Hunter Thompson Genre: Gonzo Name: Khaled Hosseini Genre: Novelist Sample Output: Name: Miles Davis Instrument: Trumpet Name: John Coltrane Instrument: Saxophone Name: Keith Jarrett Instrument: Piano Name: Keith Richards Instrument: Guitar Name: Ella Fitzgerald Instrument: Voice Name: Paul Motian Instrument: Percussion Name: Tobias Wolff Genre: Memoir Name: August Wilson Genre: Playwright Name: Te-Ping Chen Genre: Journalist Name: Stanislaw Lem Genre: SciFi Name: Gertrude Stein Genre: Novelist Name: Hunter Thompson Genre: Gonzo Name: Khaled Hosseini Genre: Novelist

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

Students also viewed these Databases questions