Question: For questions 5 - 9 , consider the following two classes, Player and BasketballPlayer: class Player { protected: string name; double weight; double height; public:

For questions 5-9, consider the following two classes, Player and BasketballPlayer:
class Player
{
protected:
string name;
double weight;
double height;
public:
Player(string n, double w, double h)
{ name = n; weight = w; height = h; }
string getName() const
{ return name; }
virtual void printStats() const =0;
};
class BasketballPlayer : public Player
{
private:
int fieldgoals;
int attempts;
public:
BasketballPlayer(string n, double w,
double h, int fg, int a) : Player(n, w, h)
{ fieldgoals = fg; attempts = a; }
void printStats() const
{ cout << name << endl;
cout << "Weight: "<< weight;
cout <<" Height: "<< height << endl;
cout <<"FG: "<< fieldgoals;
cout <<" attempts: "<< attempts;
cout <<" Pct: "<<(double) fieldgoals / attempts << endl; }
};
int main()
{
Player* ptr;
BasketballPlayer johnson("Magic Johnson", 100,200,300,400);
ptr = &johnson;
ptr->printStats();
return 0;
}
5. What does =0 after function printStats() do?
6. Would the following line in main() compile: Player p; -- why or why not?
7. Could I remove the printStats() member function from BasketballPlayer? Why or why not?
8. Would the following line in main() compile: Player *ptr; -- why or why not?
9. Write code which creates a Player pointer (e.g. Player *p), creates an object of class BasketballPlayer, and assigns the address of the object to the pointer. Why does this work?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!