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

Project 2: Musician A Musician represents a player of some kind of instrument. T

ID: 3844274 • Letter: P

Question

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

Please include all .cpp and .h files

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

#include <iostream>
using namespace std;

class Musician {

public:

Musician :: Musician(String instrument){
myInstrument = instrument;
}


virtual void play() const {
cout <<"musician playing her " myInstrument << endl;
}
virtual string getInstrument( ) const {

cout << myInstrument << endl;

}

private :
string myInstrument;

};

class Pianist : public Musician
{

public :

Pianist :: Pianist();
virtual void play( ) const {
       cout <<"pianist playing the piano"<< endl;
   }

};

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;

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