Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started