In: Computer Science
Define and implement a class named Coach, which represents a special kind of person. It is to be defined by inheriting from the Person class. The Coach class has the following constructor:
Coach(string n, int sl) // creates a person with name n, whose occupation
// is “coach” and service length is sl
The class must have a private static attribute
static int nextID ;
which is the unique personID of the next Coach object to be created. This is incremented by 1 each time a new Coach object is created. This must be initialized to the value of 0 in the Coach.cpp file, the first coach has personID equal to 0.
Redefine the get_salary function inherited by Coach from Person so that it returns either the value of salary if the service length is less than 15 or three times the value of salary otherwise.
So we can check that your code compiles implement a program with a main method that declares a Coach object in a file called main-2-1.cpp.
2-2. Define and implement a class named Player, which represents a special kind of person. It is to be defined by inheriting from the Person class. The Player class has the following constructor and behaviour:
Player(string n, int sl, int *list, int m)
/* creates a person with name n, whose occupation is
“player” and service length is sl;
list records the time that a player spent in the
field in each game, the integers are distinct and
sorted in ascending order; m is the number of games
*/
int searchGame(int x)
/* searches for a particular time x in the array of times;
returns either the index of x in the array if x exists or -1 otherwise;
you may use your favourite sorting algorithm
*/
The class must have a private static attribute
static int nextID ;
which is the unique personID of the next Player object to be created. This is incremented by 1 each time a new Player object is created. This must be initialized to the value of 1000 in the Player.cpp file, the first player has personID equal to 1000.
Redefine get_salary function inherited by Player from Person so that it returns either the value of salary if service length is less than 20 or three times the value of salary otherwise.
Function searchGame should look for a particular time x in the array of times. If x exists, it returns the array index of x in the array or it returns -1 otherwise.
So we can check that your code compiles implement a program with a main method that declares a Player object in a file called main-2-2.cpp.
code:
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
using namespace std;
class Person{
protected:
string name; // the name of a person
string occupation; // the occupation of a person
int salary; // the salary of a person; takes only non-negative values
int serviceLength; // the service length of a person
int personID; // the unique ID of a person
public:
Person(string n, string o, int sl);
Person();
// for name
void set_name(string input);
string get_name();
// for occupation
void set_occupation(string input);
string get_occupation();
// for salary
void set_salary(int input);
virtual int get_salary();
// for serviceLength
int get_serviceLength();
// for personID
int get_personID();
~Person();
};
#endif
#include "Person.h"
#include <iostream>
using namespace std;
Person::Person(){
}
Person::Person(string n, string o, int sl){
name = n;
occupation = o;
serviceLength = sl;
salary = 1;
}
void Person::set_name(string input){
name = input;
}
string Person::get_name(){
return name;
}
void Person::set_occupation(string input){
occupation = input;
}
string Person::get_occupation(){
return occupation;
}
void Person::set_salary(int input){
if (input >= 0){
salary = input;
}
}
int Person::get_salary()
{
return salary;
}
int Person::get_serviceLength(){
return serviceLength;
}
int Person::get_personID(){
return personID;
}
Person::~Person(){
}
Please find the programs below, refer the program screenshot below for indentation, line number, correctness etc:
Program Person.h
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
using namespace std;
class Person {
protected:
string name;
// the name of a person
string occupation; //
the occupation of a person
int salary; // the
salary of a person; takes only non-negative values
int serviceLength; //
the service length of a person
int personID;
// the unique ID of a person
public:
Person(string n, string o, int sl);
Person();
// for name
void set_name(string input);
string get_name();
// for occupation
void set_occupation(string input);
string get_occupation();
// for salary
void set_salary(int input);
virtual int get_salary();
// for serviceLength
int get_serviceLength();
// for personID
int get_personID();
~Person();
};
#endif
Program Person.cpp
#include "Person.h"
#include <iostream>
using namespace std;
Person::Person() {
}
Person::Person(string n, string o, int sl) {
name = n;
occupation = o;
serviceLength = sl;
salary = 1;
}
void Person::set_name(string input) {
name = input;
}
string Person::get_name() {
return name;
}
void Person::set_occupation(string input) {
occupation = input;
}
string Person::get_occupation() {
return occupation;
}
void Person::set_salary(int input) {
if (input >= 0) {
salary = input;
}
}
int Person::get_salary() {
return salary;
}
int Person::get_serviceLength() {
return serviceLength;
}
int Person::get_personID() {
return personID;
}
Person::~Person() {
}
Program Coach.h
#include "Person.h"
#include <iostream>
using namespace std;
class Coach: public Person {
public:
Coach(string n, int sl);
int get_salary();
};
Program Coach.cpp
#include "Person.h"
#include "Coach.h"
#include <iostream>
using namespace std;
static int nextID = 0;
Coach::Coach(string n, int sl) :
Person::Person(n, "coach", sl)
{
personID = nextID;
nextID = nextID + 1;
}
int Coach::get_salary() {
if (serviceLength >= 15) {
return (3 * salary);
} else {
return salary;
}
}
Program Player.h
#include "Person.h"
#include <iostream>
using namespace std;
class Player: public Person {
public:
int no_of_game;
int *time_record;
Player(string n, int sl, int *list, int m);
int searchGame(int x);
int get_salary();
};
Program Player.cpp:
#include "Person.h"
#include "Player.h"
#include <iostream>
using namespace std;
static int nextID = 1000;
Player::Player(string n, int sl, int *list, int m) :
Person::Person(n, "player", sl)
{
no_of_game = m;
time_record = list;
personID = nextID;
nextID = nextID + 1;
}
int Player::searchGame(int x) {
for (int i = 0; i < no_of_game; i++) {
if (x == time_record[i]) {
return i;
}
}
return -1;
}
int Player::get_salary() {
if (serviceLength >= 30) {
return (3 * salary);
} else {
return salary;
}
}
Driver program to run the coach: CoachDriver.cpp, you need to run this program to see the output for coach
#include <iostream>
#include "Coach.h"
using namespace std;
int main() {
cout << "Coaches:" << endl;
Coach c("Andy", 15);
c.set_salary(10000);
cout << "Name : " << c.get_name() <<
endl;
cout << "ID : " << c.get_personID()
<< endl;
cout << "Occupation : " <<
c.get_occupation() << endl;
cout << "Service Length : " <<
c.get_serviceLength() << endl;
cout << "Salary : " << c.get_salary()
<< endl;
Coach c1("Elizabeth", 15);
c1.set_salary(20000);
cout << "Name : " << c1.get_name()
<< endl;
cout << "ID : " << c1.get_personID()
<< endl;
cout << "Occupation : " <<
c1.get_occupation() << endl;
cout << "Service Length : " <<
c1.get_serviceLength() << endl;
cout << "Salary : " << c1.get_salary()
<< endl;
return 0;
}
Driver program to run the players, PlayerDriver.cpp, you need to run this program to see the output for player:
#include <iostream>
#include "Player.h"
using namespace std;
int main() {
cout << endl << "Players:" <<
endl;
int time_record[] = { 10, 20, 30, 40 };
Player p("Andy", 15, time_record, 4);
p.set_salary(5000);
cout << "Name : " << p.get_name() <<
endl;
cout << "ID : " << p.get_personID()
<< endl;
cout << "Occupation : " <<
p.get_occupation() << endl;
cout << "Service Length : " <<
p.get_serviceLength() << endl;
cout << "Salary : " << p.get_salary()
<< endl;
cout << "Game : " << p.searchGame(30)
<< endl;
int time_record1[] = { 10, 30, 50, 60 };
Player p1("Elizabeth", 15, time_record1, 4);
p1.set_salary(5005);
cout << "Name : " << p1.get_name()
<< endl;
cout << "ID : " << p1.get_personID()
<< endl;
cout << "Occupation : " <<
p1.get_occupation() << endl;
cout << "Service Length : " <<
p1.get_serviceLength() << endl;
cout << "Salary : " << p1.get_salary()
<< endl;
cout << "Game : " << p1.searchGame(30)
<< endl;
return 0;
}
Program screen shots with the output:
Person.h
Person.cpp
Coach.h
Coach.cpp
Player.h
Player.cpp
Driver program for Coach:
Output for coach:
Driver program for player:
Output for player driver:
Please provide a feedback by clicking on the feedback button