Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me with this poject Project 2: Musician A Musician represents a play

ID: 3844434 • Letter: P

Question

Please help me with this poject

Project 2: Musician

A Musician represents a player of some kind of instrument. The subclass Pianist represents a piano player whose instrument is a piano. Define these two classes so that they have the behavior shown in the sample driver and the sample output below.

Implementation Details

Sample Driver

Musician

Musician( std::string instrument );
std::string getInstrument( ) const;
virtual void play( );

std::string myInstrument;

Pianist : public Musician

Pianist( );
virtual void play( );

Pianist * ptrP = new Pianist( );
cout << "ptrP->getInstrument()" << endl;
cout << ptrP->getInstrument( ) << endl; // piano
cout << "ptrP->play()" << endl;
ptrP->play();


Musician m;
Pianist p;
m = p;      /// legal
/// p = m; /// illegal!

Musician * temp;
cout << "temp pointing at a musician..." << endl;
temp = ptrM;
cout << "temp->play()" << endl;
temp->play( );
cout << "temp pointing at a pianist..." << endl;
temp = ptrP;
cout << "temp->play()" << endl;
temp->play( );

Sample Output

Implementation Details

Sample Driver

Musician

Musician( std::string instrument );
std::string getInstrument( ) const;
virtual void play( );

std::string myInstrument;

Pianist : public Musician

Pianist( );
virtual void play( );

Musician * ptrM = new Musician( "guitar" );
cout << "ptrM->getInstrument()" << endl;
cout << ptrM->getInstrument( ) << endl; // guitar
cout << "ptrM->play()" << endl;
ptrM->play();

Pianist * ptrP = new Pianist( );
cout << "ptrP->getInstrument()" << endl;
cout << ptrP->getInstrument( ) << endl; // piano
cout << "ptrP->play()" << endl;
ptrP->play();


Musician m;
Pianist p;
m = p;      /// legal
/// p = m; /// illegal!

Musician * temp;
cout << "temp pointing at a musician..." << endl;
temp = ptrM;
cout << "temp->play()" << endl;
temp->play( );
cout << "temp pointing at a pianist..." << endl;
temp = ptrP;
cout << "temp->play()" << endl;
temp->play( );

Sample Output

ptryM->getInstrument( )
guitar
ptrM->play()
musician playing her guitar
ptrP->getInstrument( )
piano
ptrP->play()
pianist playing the piano
temp pointing at a musician...
temp->play()
musician playing her guitar
temp pointing at a pianist...
temp->play()
pianist playing the piano

Explanation / Answer

Edit : Edited to include source code as per request .

First line of the code says,
Musician * ptrM = new Musician( "guitar" );

It is trying to make a pointer, ptrM, to an object of the class 'Musician'.
Thus, our first step towards implementation of the class Musician should be making it's constructor. As we can see, the constructor takes a string parameter, in this case "guitar". The constructor should assign the value of this parameter to it's data member myInstrument.
Musician(std::string instrument)
{
myInstrument = instrument;

}

Next, the code says,
cout << "ptrM->getInstrument()" << endl;
cout << ptrM->getInstrument( ) << endl;


And the output is guitar. This indicates that the utility function getInstrument() which takes no parameter, should return an std::string object which should be the value of the data member myInstrument.
std::string getInstrument() const
{
return myInstrument;
}


There is a virtual function play(), which means that the superclass should contain a basic implementation of it, and the derived classes should override the function. Looking at

cout << "ptrM->play()" << endl;
ptrM->play();

suggests that the play() function should print out a string, saying the musician is playing the instrument that she has. Hence, the code should be, in Musician class,
virtual void play()
{
   cout<<"musician playing her "<<myInstrument<<" ";
}


The class Pianist should always have myInstrument set to "piano", as we can see that the snippet
Pianist * ptrP = new Pianist( );
cout << "ptrP->getInstrument()" << endl;
cout << ptrP->getInstrument( ) << endl;

prints out piano. Also, the default constructor should only initialize the myInstrument field to "piano".
Here's the more interesting thing about the derived class - it must override the virtual function play() to meet its own demands, in this case, printing out different information.
It's implementation in derived class is as follows-
virtual void play()
{
   cout<<"pianist playing the piano ";
}

As the derived class would use a default constructor, we must implement a default constructor of the parent class as well. We don't need to do anything in this constructor - yet it is important that it stays, as any object created of the derived class will call the default constructor of the parent class implicitly.

So, the complete classes look like this -

#include <iostream>

using namespace std;

class Musician
{
public:
std::string myInstrument;
Musician()
{
}
Musician(string instrument)
{
myInstrument = instrument;
}
string getInstrument() const
{
return myInstrument;
}
virtual void play()
{
cout<<"musician playing her "<<myInstrument<<" ";
}
};

class Pianist: public Musician
{
public:
Pianist()
{
myInstrument = "piano";
}
virtual void play()
{
cout<<"pianist playing the piano ";
}
};


int main()
{
Musician * ptrM = new Musician( "guitar" );
cout << "ptrM->getInstrument()" << endl;
cout << ptrM->getInstrument( ) << endl; // guitar
cout << "ptrM->play()" << endl;
ptrM->play();
Pianist * ptrP = new Pianist( );
cout << "ptrP->getInstrument()" << endl;
cout << ptrP->getInstrument( ) << endl; // piano
cout << "ptrP->play()" << endl;
ptrP->play();


Musician m;
Pianist p;
m = p; /// legal
/// p = m; /// illegal!

Musician * temp;
cout << "temp pointing at a musician..." << endl;
temp = ptrM;
cout << "temp->play()" << endl;
temp->play( );
cout << "temp pointing at a pianist..." << endl;
temp = ptrP;
cout << "temp->play()" << endl;
temp->play( );
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote