Comp 220 lab 3
COMP 220 Lab 3
Inheritance: Baseball Player and Pitcher Classes
· Develop and apply an appropriate and useful inheritance hierarchy.
· Use multiple cpp and header files effectively to develop modular code.
Assume that all baseball players, regardless of their position, have these two important statistics:
· rbi – runs batted in, a whole number (so you must data type int)
· batting average – real number percentage of hits per at-bat (a real number, so you must use data type double)
Pitchers are baseball players with an additional statistic, era (earned run average), a real number for earned runs per nine innings pitched (so you must use data type double).
Developing the Class Hierarchy
A logical class hierarchy for these two types of objects would use aPlayer class as a parent (base) class for a derived (child) Pitcherclass. The Pitcher class would include all the members of the Player class and have an additional (private) era data member, and, of course, appropriate (public) accessor, mutator and constructor methods.
This relationship is sometimes called a tree, but is also called a hierarchy, more particularly in this case, a class hierarchy. Note that the arrow points from derived (child) class to parent (base) class, which indicates the search path for executing member methods; if the method is not found in the Pitcher class, the compiler looks back to the parent class, Player, to find it.
File Organization (Class Header and Implementation Files, etc.)
Each class (in this case Player and Pitcher) will have its own header (.h) and implementation (.cpp) file, and, of course, there needs to be an executable file that actually uses the class (an application .cpp file with a main function). As with the classes previously developed in the course, the application will be a“driver” program that executes each member function at least once with an arbitrary, but specifictest case data set.
Use the given Player class as a base class for Pitcher and add to the BaseballPlayerDriver file code to test the class with the data specified below.
Player class requirements:
1. Create with separate header (Player.h) and implementation (Player.cpp) files.
2. Two private data members, rbi and battingAverage.
3. A parameterized constructor that receives and sets the initial values for both rbi and battingAverage.
4. Two accessor (get) methods that return the values of the data members: getRbi and getBattingAverage.
5. Two mutator methods:
5.1. setRbi that receives a whole number and writes it to the rbi data member.
5.2. setBattingAverage that receives a real number and writes it to the battingAverage data member.
6. showAll accessor method that displays the values of both data members as shown here for an arbitrary test case for a player who has 10 rbi and batting average 0.1234:
rbi = 10
batting average = 123
Note that the battingAverage variable will be a floating point number (double), but it must be rounded and display as a digit integer.
Pitcher class requirements:
1. Pitcher class must be derived from Player class.
2. Create with separate header (Pitcher.h) and implementation (Pitcher.cpp) files.
3. One private data member, era.
4. A parameterized constructor that receives and sets the initial values for all three data members, rbi, battingAverage and era.
5. getEra accessor method that returns the value of era.
6. setEra mutator method that receives a real number and writes it to the era data member.
7. showAll function toover-ride the base class’s showAll function and do the following: call the base class’s showAll function to display the inherited data members’ values and, in addition, output the additional value, the pitcher’s era, as shown for an arbitrary test casefor a pitcher who has 10 rbi, batting average 0.1234 and era of 3.456:
rbi = 10
batting average = 123
era = 3.46
Note that the era variable will be a floating point number (double), but it must be rounded and display to decimal places.
BaseballPlayerDriver class requirements:
1. Tell the user you’re creating a Pitcher with 10 rbi, batting average 0.1234 and era 3.456.
2. Use the parameterized constructor to create to instantiate a Pitcher variable, p1 with initial values 10 rbi, batting average 0.1234 and era 3.456.
3. Tell the user you’re using the get methods to display the values of the data members.
4. Use cout and the three get methods to display the values.
5. Tell the user you’re using the showAll method to display the values of the data members.
6. Call the showAll method.
7. Tell the user you’re using setRbi to change it to 9.
8. Use setRbito change it to 9.
9. UseshowAllto display the data values.
10. Tell the user you’re using setBattingAverage to change it to 0.2345.
11. Use setBattingAverage to change it to 0.2345.
12. Use showAll to display the data values.
13. Tell the user you’re using setEra to change it to 5.678.
14. Use setEra to change it to 5.678.
15. Use showAll to display the data values.
Use the const modifier for member functions that do not write to a data member to guarantee the data members are protected.
Put all five files (Player.h, Player.cpp, Pitcher.h, Pitcher.cpp andBaseballPlayerDriver.cpp ) in a folder called FirstNameLastNameLab3Pitcher, zip it into a file named FirstNameLastNameLab3Pitcher.zip and submit it to the Week 3 Drop Box by the deadline.