Wrap-Up

Answers to Self Review Exercises

9.1

a) dot (.), arrow (->). b) private. c) public. d) Default memberwise assignment (performed by the assignment operator).

9.2
  1. Error: Destructors are not allowed to return values (or even specify a return type) or take arguments.

    Correction: Remove the return type void and the parameter int from the declaration.

  2. Error: Members cannot be explicitly initialized in the class definition.

    Correction: Remove the explicit initialization from the class definition and initialize the data members in a constructor.

  3. Error: Constructors are not allowed to return values.

    Correction: Remove the return type int from the declaration.

Exercises

9.3

What is the purpose of the scope resolution operator?

9.4

(Enhancing Class Time) Provide a constructor that is capable of using the current time from the time() functiondeclared in the C++ Standard Library header to initialize an object of the Time class.

9.5

(Complex Class) Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class.

Complex numbers have the form

realPart + imaginaryPart * i  

where i is

 

Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks:

  1. Adding two Complex numbers: The real parts are added together and the imaginary parts are added together.
  2. Subtracting two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.
  3. Printing Complex numbers in the form (a, b), where a is the real part and b is the imaginary part.

9.6

(Rational Class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class.

Use integer variables to represent the private data of the classthe numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction

 

would be stored in the object as 1 in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks:


  1. Adding two Rational numbers. The result should be stored in reduced form.
  2. Subtracting two Rational numbers. The result should be stored in reduced form.
  3. Multiplying two Rational numbers. The result should be stored in reduced form.
  4. Dividing two Rational numbers. The result should be stored in reduced form.
  5. Printing Rational numbers in the form a/b, where a is the numerator and b is the denominator.
  6. Printing Rational numbers in floating-point format.

9.7

(Enhancing Class Time) Modify the Time class of Figs. 9.89.9 to include a tick member function that increments the time stored in a Time object by one second. The Time object should always remain in a consistent state. Write a program that tests the tick member function in a loop that prints the time in standard format during each iteration of the loop to illustrate that the tick member function works correctly. Be sure to test the following cases:

  1. Incrementing into the next minute.
  2. Incrementing into the next hour.
  3. Incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).

9.8

(Enhancing Class Date) Modify the Date class of Figs. 9.179.18 to perform error checking on the initializer values for data members month, day and year. Also, provide a member function nexTDay to increment the day by one. The Date object should always remain in a consistent state. Write a program that tests function nexTDay in a loop that prints the date during each iteration to illustrate that nexTDay works correctly. Be sure to test the following cases:

  1. Incrementing into the next month.
  2. Incrementing into the next year.

9.9

(Combining Class Time and Class Date) Combine the modified Time class of Exercise 9.7 and the modified Date class of Exercise 9.8 into one class called DateAndTime. (In Chapter 12, we will discuss inheritance, which will enable us to accomplish this task quickly without modifying the existing class definitions.) Modify the tick function to call the nextday function if the time increments into the next day. Modify functions printStandard and printUniversal to output the date and time. Write a program to test the new class DateAndTime. Specifically, test incrementing the time into the next day.

9.10

(Returning Error Indicators from Class Time's set Functions) Modify the set functions in the Time class of Figs. 9.89.9 to return appropriate error values if an attempt is made to set a data member of an object of class Time to an invalid value. Write a program that tests your new version of class Time. Display error messages when set functions return error values.

9.11

(Rectangle Class) Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide member functions that calculate the perimeter and the area of the rectangle. Also, provide set and get functions for the length and width attributes. The set functions should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0.

9.12

(Enhancing Class Rectangle) Create a more sophisticated Rectangle class than the one you created in Exercise 9.11. This class stores only the Cartesian coordinates of the four corners of the rectangle. The constructor calls a set function that accepts four sets of coordinates and verifies that each of these is in the first quadrant with no single x- or y-coordinate larger than 20.0. The set function also verifies that the supplied coordinates do, in fact, specify a rectangle. Provide member functions that calculate the length, width, perimeter and area. The length is the larger of the two dimensions. Include a predicate function square that determines whether the rectangle is a square.

9.13

(Enhancing Class Rectangle) Modify class Rectangle from Exercise 9.12 to include a draw function that displays the rectangle inside a 25-by-25 box enclosing the portion of the first quadrant in which the rectangle resides. Include a setFillCharacter function to specify the character out of which the body of the rectangle will be drawn. Include a setPerimeterCharacter function to specify the character that will be used to draw the border of the rectangle. If you feel ambitious, you might include functions to scale the size of the rectangle, rotate it, and move it around within the designated portion of the first quadrant.


9.14

(HugeInteger Class) Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. Provide member functions input, output, add and substract. For comparing HugeInteger objects, provide functions isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isGreaterThanOrEqualTo and isLessThanOrEqualToeach of these is a "predicate" function that simply returns TRue if the relationship holds between the two HugeIntegers and returns false if the relationship does not hold. Also, provide a predicate function isZero. If you feel ambitious, provide member functions multiply, divide and modulus.

9.15

(TicTacToe Class) Create a class TicTacToe that will enable you to write a complete program to play the game of tic-tac-toe. The class contains as private data a 3-by-3 two-dimensional array of integers. The constructor should initialize the empty board to all zeros. Allow two human players. Wherever the first player moves, place a 1 in the specified square. Place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has been won or is a draw. If you feel ambitious, modify your program so that the computer makes the moves for one of the players. Also, allow the player to specify whether he or she wants to go first or second. If you feel exceptionally ambitious, develop a program that will play three-dimensional tic-tac-toe on a 4-by-4-by-4 board. [Caution: This is an extremely challenging project that could take many weeks of effort!]

Категории