Create a baseballbatter class that records the batting


C++ Study Examples - Data Abstraction

1. Explain the problem with the following code segments:

a. int *x;
    *x = 5;
b. const int * x;

2. Consider the following code:

 int nums[10] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
 int *pInt;
 pInt = &nums[2];
 pInt += 2;
 *pInt = 20; 

What will be the resulting array when this code is completed?  Explain.

3. Consider the following code:
 int i;
 int smallest;
 int nums[5] = {4, 20, 18, 7, 2};
 
 smallest = nums[0];
 for (i = 1; i < 5; i++) {
        if (nums[i] < smallest) smallest = nums[i];
}
cout << smallest;

a. Rewrite the code using pointers.  Specifically, instead of i and smallest, use
 
int* pInt;
int* pSmallest;

b. Which version of the code do you find easier to read?

4. Take the multi-purpose sorting program that uses function pointers.  Create a new auxiliary function such that the routine will sort numbers in ascending order, but all zeros will be placed at the end of the list. For example, an array with 3, 0, -5, 9, 22, 0, 14, 7 would be set to -5, 3, 7, 9, 22, 0, 0.

5.  Create a StudentGrade class that includes two data members: a student ID number and the student's score on a test. Both of these are integers.  Include "get" and "set" member functions for each of the data members, plus a displayRecord method which outputs the values in this form:

Student 12345 has a score of 98 For this program you do not have to do any data validation in the "set" methods. In addition to the class, write a short main function that tests your class.

6. Modify your StudentGrade class such that the "set" methods perform data validation.A student ID should be in the range of 10000-50000, and a grade should be in the range 0 - 100. Use a single "if" statement (using the && operator) in each "set" method.

7. Create a BaseballBatter class that records the batting statistics for an individual player.It should have "get" and "set" methods for the following values: at-bats, singles, doubles, triples, and home runs. The "set" methods should check that only non-negative values are set.In addition, the class should have the following methods:

a. A validData method that returns true if the number of at-bats is greater than the number of hits.

b. A battingAverage method that returns the batting average.

c. A sluggingPercentage method that returns the slugging percentage.

Create a simple main function to test your class.

 

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: Create a baseballbatter class that records the batting
Reference No:- TGS01246136

Now Priced at $20 (50% Discount)

Recommended (94%)

Rated (4.6/5)