C++ Program - Need this done in Xcode. A Musician represents a player of some ki
ID: 3577358 • Letter: C
Question
C++ Program - Need this done in Xcode.
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( );
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>
#include<string>
using namespace std;
class Musician
{
string instrument;
public:
//defualt constructor
Musician()
{
instrument = "";
}
//overloaded constructor
Musician(string ins)
{
instrument = ins;
}
//get method
string getInstrument() const
{
return instrument;
}
//depnding on the object play will be called either musiccian or pianist
virtual void play()
{
cout << "musician playing her " << instrument<< endl;
}
};
class Pianist:public Musician
{
public:
Pianist() :Musician("Piano")
{
}
virtual void play()
{
cout << "Pianist playing her " << getInstrument() << 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; /// 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();
}
----------------------------------------------------------------
output:
ptrM->getInstrument()
guitar
ptrM->play()
musician playing her guitar
ptrP->getInstrument()
Piano
ptrP->play()
Pianist playing her Piano
temp pointing at a musician...
temp->play()
musician playing her guitar
temp pointing at a pianist...
temp->play()
Pianist playing her Piano
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.