The class Athlete represents a sport fanatic with the data fields name (a string
ID: 3861318 • Letter: T
Question
The class Athlete represents a sport fanatic with the data fields name (a string such as "Howard") and a sport (such as "Rowing"). Consider the following:
#include <iostream>
class Athlete
{
public:
Athlete( std::string name, std::string sport);
virtual void play( );
private:
std::string mName, mSport;
};
Athlete::Athlete( std::string name, std::string sport ) : mName( name ), mSport( sport )
{ // empty... }
void Athlete::play( )
{ std::cout << mName << " is playing " << mSport << std::endl; }
Based on this class Athlete, create the class Rower. An Rower is a special kind of Athlete. Driver code should be able to create a Rower by just passing a name to the constructor, as in: Rower r( "Howard" ); . Be sure to Rower calls the parent class constructor, passing "Rowing" as the sport. Define the operation Rower::play( ) so that Rower's print "Ready All Row!" whenever they play.
Explanation / Answer
#include <iostream>
using namespace std;
class Athlete
{
public:
Athlete( std::string name, std::string sport);
virtual void play( );
private:
std::string mName, mSport;
};
Athlete::Athlete( std::string name, std::string sport ) : mName( name ), mSport( sport )
{
// empty...
}
void Athlete::play( )
{
std::cout << mName << " is playing " << mSport << std::endl;
}
class Rower: public Athlete //Inheriting Athlete class
{
string mName;
public:
Rower(string name); // Parameterized constructor
void play();
};
Rower::Rower(string name): Athlete ( name, "Rowing"), mName(name)
{
}
void Rower::play()
{
cout<<"Ready All Row";
}
int main ()
{
Athlete *athlete = new Rower("Philip");
athlete->play();
delete athlete;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.