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

IN C++ WITH HEADER FILES, IMPLEMENTATION FILES AND ONE MAIN FILE Polymorphism, P

ID: 3889848 • Letter: I

Question

IN C++ WITH HEADER FILES, IMPLEMENTATION FILES AND ONE MAIN FILE

Polymorphism, Pointers, and Virtual Functions

Construct a base class called athlete. This class should have a member attribute called sport which contains the name of the sport the athlete plays. This class should have two virtual functions called scorePoints() and move().

Write two derived classes from the base athlete class. These classes should pertain to a particular sport. For example, basketballPlayer or baseballPlayer would be subclasses of the athlete class. The derived classes should put the name of the sport by default into the sport member attribute. Additionally, they should redefine the scorePoints() and move() functions with information related that particular sport. For example, a hockeyPlayer class could have “I score points by shooting the puck in the net” for its scorePoints() function and “I move by skating on ice skates” for its move() function.

Your driver file should include a processAthlete() function which accepts as a parameter an object of the base athlete class. In the function, you should call the accessor for the sport attribute, as well as the two virtual functions. The proper information related to the class should be displayed (the name of the sport, how points are scored, and how the athlete moves). This function should be called twice, once for each object of the two different derived classes.

You are required to write header files for the base athlete class as well as the derived athlete classes. Function bodies need to be defined in implementation files and the main function will be in main.cpp. Make sure your program is properly documented.

Explanation / Answer

// athlete.h : Declares athlete and its derived classes.
//
#include <string>

//Base class
class athlete
{
   //We have used protected access specifier which will inherited to child classes.
   protected:
       std::string m_strSport;
       std::string m_strScorePoint;
       std::string m_strMove;
public:
   athlete(const std::string &);
   athlete(void);
   ~athlete(void);
   virtual void scorePoints(std::string strScorePoint);
   virtual void move(std::string strMove);
};


//Derived class hockeyPlayer
class hockeyPlayer: public athlete
{
public:
   hockeyPlayer(const std::string&);
   ~hockeyPlayer(void);
   void scorePoints(std::string strScorePoint);
   void move(std::string strMove);
};


//Derived class baseballPlayer
class baseballPlayer: public athlete
{
public:
   baseballPlayer(const std::string&);
   ~baseballPlayer(void);
   void scorePoints(std::string strScorePoint);
   void move(std::string strMove);
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// athlete.cpp : Defines athlete and its derived classes.
//
#include "athlete.h"
using namespace std;
athlete::athlete(const string &strSports)
{
   m_strSport = strSports;
}

athlete::athlete(void)
{
   m_strSport = "";
}

athlete::~athlete(void)
{
}

void athlete::scorePoints(std::string strScorePoint)
{
   m_strScorePoint = strScorePoint;
}

void athlete::move(std::string strMove)
{
   m_strMove = strMove;
}


//Derived class hockeyPlayer implemtation.
hockeyPlayer::hockeyPlayer(const string &strSports)
{
   m_strSport = strSports;
}

void hockeyPlayer::scorePoints(std::string strScorePoint)
{
   m_strScorePoint = strScorePoint;
}

void hockeyPlayer::move(std::string strMove)
{
   m_strMove = strMove;
}


//Derived class baseballPlayer implemtation.
baseballPlayer::baseballPlayer(const string &strSports)
{
   m_strSport = strSports;
}

void baseballPlayer::scorePoints(std::string strScorePoint)
{
   m_strScorePoint = strScorePoint;
}

void baseballPlayer::move(std::string strMove)
{
   m_strMove = strMove;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// main.cpp : Defines the entry point and act as a driver file.
//

#include "athlete.h"
#include <string>
using namespace std;

void processAthlete(athlete *objathlete, string move, string score)
{
   objathlete->move(move);
   objathlete->scorePoints(score);
}

int _tmain(int argc, _TCHAR* argv[])
{
   //processAthlete for hockeyPlayer
   //processAthlete will call move() and scorePoinys() function of hockeyPlayer class
   processAthlete(
               new hockeyPlayer("Hockey"),
               "I move by skating on ice skates",
               "I score points by shooting the puck in the net"
               );

   //processAthlete for hockeyPlayer
   //processAthlete will call move() and scorePoinys() function of baseballPlayer class
   processAthlete(
               new baseballPlayer("BaseBall"),
               "I move by hand",
               "I score points by shooting the ball in the net"
               );
   return 0;
}