Question

In: Computer Science

Define and implement a class named Coach, which represents a special kind of person. It is...

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(){

}

Solutions

Expert Solution

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


Related Solutions

in java Design and implement a class named Person and its two subclasses named Student and...
in java Design and implement a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name,address, phone number, and email address. A student has a class status (year 1,year 2,year 3,or year 4). An employee has an office, salary, and date hired. Use the Date class from JavaAPI 8 to create an object for date hired. A faculty member has office hours and a rank. A staff...
Implement a class named Complex that represents immutable complex numbers. Your class needs two private final...
Implement a class named Complex that represents immutable complex numbers. Your class needs two private final fields of type double to store the real and imaginary parts. Try to use constructor chaining to implement 2 of the 3 required constructors. If you cannot complete one or more of the methods, at least make sure that it returns some value of the correct type; this will allow the tester to run, and it will make it much easier to evaluate your...
Based on a Node class; Use Java to implement the OrderedList class which represents an ordered...
Based on a Node class; Use Java to implement the OrderedList class which represents an ordered singly linked list that cannot contain any duplicates. Note that items in the OrderedList are always kept in descending order. Complete the class with the following methods. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the list at the proper location in the list. If the insertion is successful, the function returns true; otherwise,...
Person class You will implement the Person Class in Visual Studio. A person object may be...
Person class You will implement the Person Class in Visual Studio. A person object may be associated with multiple accounts. A person initiates activities (deposits or withdrawal) against an account that is captured in a transaction object. A short description of each class member is given below: Person Class Fields -   password : string Properties +   «C# property, setter private» IsAuthenticated : bool +   «C# property, setter absent» SIN : string +   «C# property, setter absent» Name : string Methods...
Implement a class named Parade using an ArrayList, which will manage instances of class Clown. Each...
Implement a class named Parade using an ArrayList, which will manage instances of class Clown. Each Clown only needs to be identified by a String for her/his name. Always join a new Clown to the end of the Parade. Only the Clown at the head of the Parade (the first one) can leave the Parade. Create a test application to demonstrate building a parade of 3 or 4 clowns (include your own name), then removing 1 or 2, then adding...
For this lab you will implement the class Ball which represents a toy ball. Ball should...
For this lab you will implement the class Ball which represents a toy ball. Ball should have a method bounce() whose return type is void, and should have an integer member bounces that counts the number of times the ball has been bounced. Ball should also have a static method named getTotalBounces which counts the total number of times that any Ball object has been bounced. To do this, you will want to include a static integer member totalBounces which...
in Python, Define the class called Line which represents a line with equation ?=??+? with input...
in Python, Define the class called Line which represents a line with equation ?=??+? with input slope ? and intercept ? to initialize the instances. It should include: attributes named ? and ? to represent slope and intercept. method named intersect to return the list, containing coordinates of the intersection point of two lines. support for + operator to compute the addition of two equations. The sum of two Line objects ?=?1?+?1 and ?=?2?+?2 is defined as the line ?=(?1+?2)?+?1+?2...
Implement a class named stack pair that provides a pair of stacks. Make the class a...
Implement a class named stack pair that provides a pair of stacks. Make the class a template class. So, you will have two files: stack pair.h and stack pair.template, following the style of the text. The basic idea is that two stacks can share a single static array. This may be advantageous if only one of the stacks will be in heavy use at any one time. • The class should have various methods to manipulate the stack: T pop...
1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named PriorityQueue The...
1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named PriorityQueue The priority queue MUST be implemented using a linked list. 2 test program checks that a newly constructed priority queue is empty o checks that a queue with one item in it is not empty o checks that items are correctly entered that would go at the front of the queue o checks that items are correctly entered that would go at the end of...
PYTHON Define a function named variousChanges(...) which receives one string (origst) (with letters, digits or special...
PYTHON Define a function named variousChanges(...) which receives one string (origst) (with letters, digits or special characters), possibly empty, and returns a new string containing the following. a) in those positions where the original string has an even digit, the corresponding character in the new (returned) string should have the string digit '0' b) in those positions where the original string has a vowel letter (upper or lower case), the new (returned) string should have the letter 'V'. Note that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT