Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I write a function printSalary that is not a class function, but will be a polymorphic function that takes in a base class

How can I write a function printSalary that is not a class function, but will be a polymorphic function that takes in a base class argument.

#include

using namespace std;

class Athlete { string name; double salary; public: Athlete(string n, double s) { name = n; salary = s; } virtual double getSalary() = 0; string getName() { return name; } void setName(string s) { name = s; } void setSalary(double s) { salary = s; } double salaryPerGame() { return salary; } };

class BaseBallPlayer: public Athlete { public: BaseBallPlayer(string n, double s):Athlete(n, s) {} double getSalary() { return 162 / Athlete::salaryPerGame(); } }; class BasketBallPlayer: public Athlete { public: BasketBallPlayer(string n, double s):Athlete(n, s) {} double getSalary() { return 82 / Athlete::salaryPerGame(); } }; class FootBallPlayer: public Athlete { public: FootBallPlayer(string n, double s):Athlete(n, s) {} double getSalary() { return 16 / Athlete::salaryPerGame(); } }; class SoccerPlayer: public Athlete { public: SoccerPlayer(string n, double s):Athlete(n, s) {} double getSalary() { return 38 / Athlete::salaryPerGame(); } };

int main() { Athlete** athletes = new Athlete*[4]; athletes[0] = new BaseBallPlayer("MyBaseBall Player", 200); athletes[1] = new BasketBallPlayer("MyBasketBall Player", 130); athletes[2] = new FootBallPlayer("MyFootBall Player", 340); athletes[3] = new SoccerPlayer("MySoccer Player", 140); for(int i=0; i<4; i++) { cout << athletes[i]->getName() << ", Salary: " << athletes[i]->getSalary() << endl; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Describe Aristotle's model of communication.

Answered: 1 week ago

Question

Discuss the challenge faced by people analytics.

Answered: 1 week ago

Question

Discuss the various types of leasing.

Answered: 1 week ago

Question

Define the term "Leasing"

Answered: 1 week ago

Question

What do you mean by Dividend ?

Answered: 1 week ago

Question

What is database?

Answered: 1 week ago