You are on page 1of 50

9

Classes:
A Deeper Look,
Part 1
OBJ E CTI VE S
In this chapter you will learn:
How to use a preprocessor wrapper to prevent multiple
definition errors caused by including more than one copy
of a header file in a source-code file.
To understand class scope and accessing class members
via the name of an object, a reference to an object or a
pointer to an object.
To define constructors with default arguments.
How destructors are used to perform termination
housekeeping on an object before it is destroyed.
When constructors and destructors are called.
The logic errors that may occur when a public member
function of a class returns a reference to private data.
To assign the data members of one object to those of
another object by default memberwise assignment.
My object all sublime
I shall achieve in time.
W. S. Gilbert
Is it a world to hide virtues
in?
William Shakespeare
Dont be consistent, but be
simply true.
Oliver Wendell Holmes, Jr.
This above all: to thine own
self be true.
William Shakespeare
cpphtp5_09_IM.fm Page 480 Thursday, December 23, 2004 4:14 PM
2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected under all copyright laws as they currently exist.
No portion of this material may be reproduced, in any form or by any means, without permission in writing from the publisher.

For the exclusive use of adopters of the book C++ How to Program, 5th Edition,
by Deitel and Deitel. ISBN 0-13-185757-6.
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 481
Self-Review Exercises
9.1 Fill in the blanks in each of the following:
a) Class members are accessed via the operator in conjunction with the name
of an object (or reference to an object) of the class or via the operator in
conjunction with a pointer to an object of the class.
ANS: dot (.), arrow (->).
b) Class members specified as are accessible only to member functions of the
class and friends of the class.
ANS: private.
c) Class members specified as are accessible anywhere an object of the class is
in scope.
ANS: public.
d) can be used to assign an object of a class to another object of the same class.
ANS: Default memberwise assignment (performed by the assignment operator).
9.2 Find the error(s) in each of the following and explain how to correct it (them):
a) Assume the following prototype is declared in class Time:
void ~Time( int );
ANS: 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 declara-
tion.
b) The following is a partial definition of class Time:
class Time
{
public:
// function prototypes
private:
int hour = 0;
int minute = 0;
int second = 0;
}; // end class Time
ANS: 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.
c) Assume the following prototype is declared in class Employee:
int Employee( const char *, const char * );
ANS: Error: Constructors are not allowed to return values.
Correction: Remove the return type int from the declaration.
Solutions
9.3 What is the purpose of the scope resolution operator?
ANS: The scope resolution operator is used to specify the class to which a function belongs.
It also resolves the ambiguity caused by multiple classes having member functions of
the same name. It also associates a member function in a .cpp file with a class defini-
tion in a .h file.
cpphtp5_09_IM.fm Page 481 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
482 Chapter 9 Classes: A Deeper Look, Part 1
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 <ctime>to initialize an object
of the Time class.
ANS: [Note: We provide two solutions. The first one only uses function time. The second
one uses several other data member and functions in <ctime> header.]
1 // Exercise 9.4 Solution: Time.h
2 #ifndef TIME_H
3 #define TIME_H
4
5 class Time
6 {
7 public:
8 Time(); // constructor
9 void setTime( int, int, int ); // set hour, minute and second
10 void printUniversal(); // print time in universal-time format
11 void printStandard(); // print time in standard-time format
12 private:
13 int hour; // 0 - 23 (24-hour clock format)
14 int minute; // 0 - 59
15 int second; // 0 - 59
16 bool isLeapYear( int ); // check if input is a leap year
17 }; // end class Time
18
19 #endif
1 // Exercise 9.4 Solution: Time.cpp
2 // Member-function definitions for class Time.
3 #include <iostream>
4 using std::cout;
5
6 #include <iomanip>
7 using std::setfill;
8 using std::setw;
9
10 #include <ctime>
11 using std::time;
12
13 #include "Time.h" // include definition of class Time from Time.h
14
15 Time::Time()
16 {
17 const int CURRENT_YEAR = 2004;
18 const int START_YEAR = 1970;
19 const int HOURS_IN_A_DAY = 24;
20 const int MINUTES_IN_AN_HOUR = 60;
21 const int SECONDS_IN_A_MINUTE = 60;
22 const int DAYS_IN_A_YEAR = 365;
23 const int DAYS_IN_A_LEAPYEAR = 366;
24 const int TIMEZONE_DIFFERENCE = 5;
25 int leapYear = 0;
26 int days;
27
cpphtp5_09_IM.fm Page 482 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 483
28 // calculate leap year
29 for ( int y = START_YEAR; y <= CURRENT_YEAR; y++ )
30 {
31 if ( isLeapYear( y ) )
32 leapYear++;
33 } // end for
34
35 int dayTimeInSeconds = time( 0 ) - HOURS_IN_A_DAY *
36 MINUTES_IN_AN_HOUR * SECONDS_IN_A_MINUTE * (
37 DAYS_IN_A_YEAR * ( CURRENT_YEAR - START_YEAR ) + leapYear );
38
39 // calculate current second, minute and hour
40 for ( int s = 0; s < SECONDS_IN_A_MINUTE; s++ )
41 {
42 for ( int m = 0; m < MINUTES_IN_AN_HOUR; m++ )
43 {
44 for ( int h = 0; h <= HOURS_IN_A_DAY; h++ )
45 {
46 if ( isLeapYear( CURRENT_YEAR ) )
47 days = DAYS_IN_A_LEAPYEAR;
48 else
49 days = DAYS_IN_A_YEAR;
50
51 for ( int d = 0; d <= days; d++ )
52 {
53 if ( s + m * SECONDS_IN_A_MINUTE +
54 h * MINUTES_IN_AN_HOUR * SECONDS_IN_A_MINUTE +
55 d * HOURS_IN_A_DAY * MINUTES_IN_AN_HOUR *
56 SECONDS_IN_A_MINUTE == dayTimeInSeconds )
57 {
58 setTime( h-TIMEZONE_DIFFERENCE, m, s );
59 } // end if
60 } // end for
61 } // end for
62 } // end for
63 } // end for
64 } // end Time constructor
65
66 // set new Time value using universal time; ensure that
67 // the data remains consistent by setting invalid values to zero
68 void Time::setTime( int h, int m, int s )
69 {
70 hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour
71 minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute
72 second = ( s >= 0 && s < 60 ) ? s : 0; // validate second
73 } // end function setTime
74
75 // print Time in universal-time format (HH:MM:SS)
76 void Time::printUniversal()
77 {
78 cout << setfill( '0' ) << setw( 2 ) << hour << ":"
79 << setw( 2 ) << minute << ":" << setw( 2 ) << second;
80 } // end function printUniversal
81
82 // print Time in standard-time format (HH:MM:SS AM or PM)
cpphtp5_09_IM.fm Page 483 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
484 Chapter 9 Classes: A Deeper Look, Part 1

83 void Time::printStandard()
84 {
85 cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":"
86 << setfill( '0' ) << setw( 2 ) << minute << ":" << setw( 2 )
87 << second << ( hour < 12 ? " AM" : " PM" );
88 } // end function printStandard
89
90 // check if a year is a leap year
91 bool Time::isLeapYear( int y )
92 {
93 if ( ( y % 400 == 0 ) || ( ( y % 4 == 0 ) && ( y % 100 != 0 ) ) )
94 return true;
95 else
96 return false;
97 } // end function isLeapYear
1 // Exercise 9.4 Solution: Ex09_04.cpp
2 #include <iostream>
3 using std::cout;
4 using std::endl;
5
6 #include "Time.h"
7
8 int main()
9 {
10 Time t; // create Time object
11
12 // display current time
13 cout << "The universal time is ";
14 t.printUniversal();
15 cout << "\nThe standard time is ";
16 t.printStandard();
17 cout << endl;
18 return 0;
19 } // end main

The universal time is 14:54:06
The standard time is 2:54:06 PM
1 // Exercise 9.4 Solution: Time.h
2 #ifndef TIME_H
3 #define TIME_H
4
5 class Time
6 {
7 public:
8 Time(); // constructor
9 void setTime( int, int, int ); // set hour, minute and second
10 void printUniversal(); // print time in universal-time format
cpphtp5_09_IM.fm Page 484 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 485
11 void printStandard(); // print time in standard-time format
12 private:
13 int hour; // 0 - 23 (24-hour clock format)
14 int minute; // 0 - 59
15 int second; // 0 - 59
16 }; // end class Time
17
18 #endif
1 // Exercise 9.4 Solution: Time.cpp
2 // Member-function definitions for class Time.
3 #include <iostream>
4 using std::cout;
5
6 #include <iomanip>
7 using std::setfill;
8 using std::setw;
9
10 #include <ctime>
11 using std::localtime;
12 using std::time;
13 using std::time_t;
14
15 #include "Time.h" // include definition of class Time from Time.h
16
17 Time::Time()
18 {
19 const time_t currentTime = time( 0 );
20 const tm *localTime = localtime( &currentTime );
21 setTime( localTime->tm_hour, localTime->tm_min, localTime->tm_sec );
22 } // end Time constructor
23
24 // set new Time value using universal time; ensure that
25 // the data remains consistent by setting invalid values to zero
26 void Time::setTime( int h, int m, int s )
27 {
28 hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour
29 minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute
30 second = ( s >= 0 && s < 60 ) ? s : 0; // validate second
31 } // end function setTime
32
33 // print Time in universal-time format (HH:MM:SS)
34 void Time::printUniversal()
35 {
36 cout << setfill( '0' ) << setw( 2 ) << hour << ":"
37 << setw( 2 ) << minute << ":" << setw( 2 ) << second;
38 } // end function printUniversal
39
40 // print Time in standard-time format (HH:MM:SS AM or PM)
41 void Time::printStandard()
42 {
43 cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":"
44 << setfill( '0' ) << setw( 2 ) << minute << ":" << setw( 2 )
45 << second << ( hour < 12 ? " AM" : " PM" );
cpphtp5_09_IM.fm Page 485 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
486 Chapter 9 Classes: A Deeper Look, Part 1
9.5 (Complex Class) Create a class called Complex for performing arithmetic with complex num-
bers. 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 fol-
lowing tasks:
a) Adding two Complex numbers: The real parts are added together and the imaginary
parts are added together.
b) 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 sub-
tracted from the imaginary part of the left operand.
c) Printing Complex numbers in the form (a, b), where a is the real part and b is the imag-
inary part.
ANS:
46 } // end function printStandard
1 // Exercise 9.5 Solution: Complex.h
2 #ifndef COMPLEX_H
3 #define COMPLEX_H
4
5 class Complex
6 {
7 public:
8 Complex( double = 0.0, double = 0.0 ); // default constructor
9 Complex add( const Complex & ); // function add
10 Complex subtract( const Complex & ); // function subtract
11 void printComplex(); // print complex number format
12 void setComplexNumber( double, double ); // set complex number
13 private:
14 double realPart;
15 double imaginaryPart;
16 }; // end class Complex
17
18 #endif
1 // Exercise 9.5 Solution: Complex.cpp
2 // Member-function definitions for class Complex.
3 #include <iostream>
4 using std::cout;
5
6 #include "Complex.h"
7
8 Complex::Complex( double real, double imaginary )
9 {
1
cpphtp5_09_IM.fm Page 486 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 487
10 setComplexNumber( real, imaginary );
11 } // end Complex constructor
12
13 Complex Complex::add( const Complex &right )
14 {
15 return Complex(
16 realPart + right.realPart, imaginaryPart + right.imaginaryPart );
17 } // end function add
18
19 Complex Complex::subtract( const Complex &right )
20 {
21 return Complex(
22 realPart - right.realPart, imaginaryPart - right.imaginaryPart );
23 } // end function subtract
24
25 void Complex::printComplex()
26 {
27 cout << '(' << realPart << ", " << imaginaryPart << ')';
28 } // end function printComplex
29
30 void Complex::setComplexNumber( double rp, double ip )
31 {
32 realPart = rp;
33 imaginaryPart = ip;
34 } // end function setComplexNumber
1 // Exercise 9.5 Solution: Ex09_05.cpp
2 #include <iostream>
3 using std::cout;
4 using std::endl;
5
6 #include "Complex.h"
7
8 int main()
9 {
10 Complex a( 1, 7 ), b( 9, 2 ), c; // create three Complex objects
11
12 a.printComplex(); // output object a
13 cout << " + ";
14 b.printComplex(); // output object b
15 cout << " = ";
16 c = a.add( b ); // invoke add function and assign to object c
17 c.printComplex(); // output object c
18
19 cout << '\n';
20 a.setComplexNumber( 10, 1 ); // reset realPart and
21 b.setComplexNumber( 11, 5 ); // and imaginaryPart
22 a.printComplex(); // output object a
23 cout << " - ";
24 b.printComplex(); // output object b
25 cout << " = ";
26 c = a.subtract( b ); // invoke add function and assign to object c
27 c.printComplex(); // output object c
28 cout << endl;
cpphtp5_09_IM.fm Page 487 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
488 Chapter 9 Classes: A Deeper Look, Part 1
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 denom-
inator. 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:
a) Adding two Rational numbers. The result should be stored in reduced form.
b) Subtracting two Rational numbers. The result should be stored in reduced form.
c) Multiplying two Rational numbers. The result should be stored in reduced form.
d) Dividing two Rational numbers. The result should be stored in reduced form.
e) Printing Rational numbers in the form a/b, where a is the numerator and b is the de-
nominator.
f) Printing Rational numbers in floating-point format.
ANS:
29 return 0;
30 } // end main

(1, 7) + (9, 2) = (10, 9)
(10, 1) - (11, 5) = (-1, -4)
1 // Exercise 9.6 Solution: Rational.h
2 #ifndef RATIONAL_H
3 #define RATIONAL_H
4
5 class Rational
6 {
7 public:
8 Rational( int = 0, int = 1 ); // default constructor
9 Rational addition( const Rational & ); // function addition
10 Rational subtraction( const Rational & ); // function subtraction
11 Rational multiplication( const Rational & ); // function multi.
12 Rational division( const Rational & ); // function division
13 void printRational (); // print rational format
14 void printRationalAsDouble(); // print rational as double format
15 private:
16 int numerator; // integer numerator
17 int denominator; // integer denominator
18 void reduction(); // utility function
19 }; // end class Rational
20
21 #endif
1 // Exercise 9.6 Solution: Rational.cpp
2 // Member-function definitions for class Rational.
3 #include <iostream>
2
4
---
cpphtp5_09_IM.fm Page 488 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 489
4 using std::cout;
5
6 #include "Rational.h" // include definition of class Rational
7
8 Rational::Rational( int n, int d )
9 {
10 numerator = n; // sets numerator
11 denominator = d; // sets denominator
12 reduction(); // store the fraction in reduced form
13 } // end Rational constructor
14
15 Rational Rational::addition( const Rational &a )
16 {
17 Rational t; // creates Rational object
18
19 t.numerator = a.numerator * denominator;
20 t.numerator += a.denominator * numerator;
21 t.denominator = a.denominator * denominator;
22 t.reduction(); // store the fraction in reduced form
23 return t;
24 } // end function addition
25
26 Rational Rational::subtraction( const Rational &s )
27 {
28 Rational t; // creates Rational object
29
30 t.numerator = s.denominator * numerator;
31 t.numerator -= denominator * s.numerator;
32 t.denominator = s.denominator * denominator;
33 t.reduction(); // store the fraction in reduced form
34 return t;
35 } // end function subtraction
36
37 Rational Rational::multiplication( const Rational &m )
38 {
39 Rational t; // creates Rational object
40
41 t.numerator = m.numerator * numerator;
42 t.denominator = m.denominator * denominator;
43 t.reduction(); // store the fraction in reduced form
44 return t;
45 } // end function multiplication
46
47 Rational Rational::division( const Rational &v )
48 {
49 Rational t; // creates Rational object
50
51 t.numerator = v.denominator * numerator;
52 t.denominator = denominator * v.numerator;
53 t.reduction(); // store the fraction in reduced form
54 return t;
55 } // end function division
56
57 void Rational::printRational ()
58 {
cpphtp5_09_IM.fm Page 489 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
490 Chapter 9 Classes: A Deeper Look, Part 1
59 if ( denominator == 0 ) // validates denominator
60 cout << "\nDIVIDE BY ZERO ERROR!!!" << '\n';
61 else if ( numerator == 0 ) // validates numerator
62 cout << 0;
63 else
64 cout << numerator << '/' << denominator;
65 } // end function printRational
66
67 void Rational::printRationalAsDouble()
68 {
69 cout << static_cast< double >( numerator ) / denominator;
70 } // end function printRationalAsDouble
71
72 void Rational::reduction()
73 {
74 int largest;
75 largest = numerator > denominator ? numerator : denominator;
76
77 int gcd = 0; // greatest common divisor
78
79 for ( int loop = 2; loop <= largest; loop++ )
80
81 if ( numerator % loop == 0 && denominator % loop == 0 )
82 gcd = loop;
83
84 if (gcd != 0)
85 {
86 numerator /= gcd;
87 denominator /= gcd;
88 } // end if
89 } // end function reduction
1 // Exercise 9.6 Solution: Ex09_06.cpp
2 #include <iostream>
3 using std::cout;
4 using std::endl;
5
6 #include "Rational.h" // include definition of class Rational
7
8 int main()
9 {
10 Rational c( 2, 6 ), d( 7, 8 ), x; // creates three rational objects
11
12 c.printRational(); // prints rational object c
13 cout << " + ";
14 d.printRational(); // prints rational object d
15 x = c.addition( d ); // adds object c and d; sets the value to x
16
17 cout << " = ";
18 x.printRational(); // prints rational object x
19 cout << '\n';
20 x.printRational(); // prints rational object x
21 cout << " = ";
22 x.printRationalAsDouble(); // prints rational object x as double
cpphtp5_09_IM.fm Page 490 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 491
23 cout << "\n\n";
24
25 c.printRational(); // prints rational object c
26 cout << " - ";
27 d.printRational(); // prints rational object d
28 x = c.subtraction( d ); // subtracts object c and d
29
30 cout << " = ";
31 x.printRational(); // prints rational object x
32 cout << '\n';
33 x.printRational(); // prints rational object x
34 cout << " = ";
35 x.printRationalAsDouble(); // prints rational object x as double
36 cout << "\n\n";
37
38 c.printRational(); // prints rational object c
39 cout << " x ";
40 d.printRational(); // prints rational object d
41 x = c.multiplication( d ); // multiplies object c and d
42
43 cout << " = ";
44 x.printRational(); // prints rational object x
45 cout << '\n';
46 x.printRational(); // prints rational object x
47 cout << " = ";
48 x.printRationalAsDouble(); // prints rational object x as double
49 cout << "\n\n";
50
51 c.printRational(); // prints rational object c
52 cout << " / ";
53 d.printRational(); // prints rational object d
54 x = c.division( d ); // divides object c and d
55
56 cout << " = ";
57 x.printRational(); // prints rational object x
58 cout << '\n';
59 x.printRational(); // prints rational object x
60 cout << " = ";
61 x.printRationalAsDouble(); // prints rational object x as double
62 cout << endl;
63 return 0;
64 } // end main

1/3 + 7/8 = 29/24
29/24 = 1.20833
1/3 - 7/8 = -13/24
-13/24 = -0.541667
1/3 x 7/8 = 7/24
7/24 = 0.291667
1/3 / 7/8 = 8/21
8/21 = 0.380952
cpphtp5_09_IM.fm Page 491 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
492 Chapter 9 Classes: A Deeper Look, Part 1
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:
a) Incrementing into the next minute.
b) Incrementing into the next hour.
c) Incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).
ANS:
1 // Exercise 9.7 Solution: Time.h
2 #ifndef TIME_H
3 #define TIME_H
4
5 class Time
6 {
7 public:
8 public:
9 Time( int = 0, int = 0, int = 0 ); // default constructor
10
11 // set functions
12 void setTime( int, int, int ); // set hour, minute, second
13 void setHour( int ); // set hour (after validation)
14 void setMinute( int ); // set minute (after validation)
15 void setSecond( int ); // set second (after validation)
16
17 // get functions
18 int getHour(); // return hour
19 int getMinute(); // return minute
20 int getSecond(); // return second
21
22 void tick(); // increment one second
23 void printUniversal(); // output time in universal-time format
24 void printStandard(); // output time in standard-time format
25 private:
26 int hour; // 0 - 23 (24-hour clock format)
27 int minute; // 0 - 59
28 int second; // 0 - 59
29 }; // end class Time
30
31 #endif
1 // Exercise 9.7: Time.cpp
2 // Member-function definitions for class Time.
3 #include <iostream>
4 using std::cout;
5
6 #include <iomanip>
7 using std::setfill;
8 using std::setw;
9
10 #include "Time.h" // include definition of class Time from Time.h
cpphtp5_09_IM.fm Page 492 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 493
11
12 // Time constructor initializes each data member to zero;
13 // ensures that Time objects start in a consistent state
14 Time::Time( int hr, int min, int sec )
15 {
16 setTime( hr, min, sec ); // validate and set time
17 } // end Time constructor
18
19 // set new Time value using universal time; ensure that
20 // the data remains consistent by setting invalid values to zero
21 void Time::setTime( int h, int m, int s )
22 {
23 setHour( h ); // set private field hour
24 setMinute( m ); // set private field minute
25 setSecond( s ); // set private field second
26 } // end function setTime
27
28 // set hour value
29 void Time::setHour( int h )
30 {
31 hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour
32 } // end function setHour
33
34 // set minute value
35 void Time::setMinute( int m )
36 {
37 minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute
38 } // end function setMinute
39
40 // set second value
41 void Time::setSecond( int s )
42 {
43 second = ( s >= 0 && s < 60 ) ? s : 0; // validate second
44 } // end function setSecond
45
46 // return hour value
47 int Time::getHour()
48 {
49 return hour;
50 } // end function getHour
51
52 // return minute value
53 int Time::getMinute()
54 {
55 return minute;
56 } // end function getMinute
57
58 // return second value
59 int Time::getSecond()
60 {
61 return second;
62 } // end function getSecond
63
64 // increment one second
65 void Time::tick()
cpphtp5_09_IM.fm Page 493 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
494 Chapter 9 Classes: A Deeper Look, Part 1
66 {
67 setSecond( getSecond() + 1 ); // increment second by 1
68
69 if ( getSecond() == 0 )
70 {
71 setMinute( getMinute() + 1 ); // increment minute by 1
72
73 if ( getMinute() == 0 )
74 setHour( getHour() + 1 ); // increment hour by 1
75 } // end if
76 } // end function tick
77
78 // print Time in universal-time format (HH:MM:SS)
79 void Time::printUniversal()
80 {
81 cout << setfill( '0' ) << setw( 2 ) << getHour() << ":"
82 << setw( 2 ) << getMinute() << ":" << setw( 2 ) << getSecond();
83 } // end function printUniversal
84
85 // print Time in standard-time format (HH:MM:SS AM or PM)
86 void Time::printStandard()
87 {
88 cout << ( ( getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 )
89 << ":" << setfill( '0' ) << setw( 2 ) << getMinute()
90 << ":" << setw( 2 ) << getSecond() << ( hour < 12 ? " AM" : " PM" );
91 } // end function printStandard
1 // Exercise 9.7: Ex09_07.cpp
2 #include <iostream>
3 using std::cout;
4 using std::endl;
5
6 #include "Time.h" // include definition of class Time
7
8 const int MAX_TICKS = 30;
9
10 int main()
11 {
12 Time t; // instantiate object t of class Time
13
14 t.setTime( 23, 59, 57 ); // set time
15
16 // output Time object t's values
17 for ( int ticks = 1; ticks < MAX_TICKS; ++ticks )
18 {
19 t.printStandard(); // invokes function printStandard
20 cout << endl;
21 t.tick(); // invokes function tick
22 } // end for
23
24 return 0;
25 } // end main
cpphtp5_09_IM.fm Page 494 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 495
9.8 (Enhancing Class Date) Modify the Date class of Fig. 9.17 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:
a) Incrementing into the next month.
b) Incrementing into the next year.
ANS:

11:59:57 PM
11:59:58 PM
11:59:59 PM
12:00:00 AM
12:00:01 AM
.
.
.
1 // Exercise 9.8 Solution: Date.h
2 #ifndef DATE_H
3 #define DATE_H
4
5 class Date
6 {
7 public:
8 Date( int = 1, int = 1, int = 1900 ); // default constructor
9 void print(); // print function
10 void setDate( int, int, int ); // set month, day, year
11 void setMonth( int ); // set month
12 void setDay( int ); // set day
13 void setYear( int ); // set year
14 int getMonth(); // get month
15 int getDay(); // get day
16 int getYear(); // get year
17 void nextDay(); // next day
18 private:
19 int month; // 1-12
20 int day; // 1-31 (except February(leap year), April, June, Sept, Nov)
21 int year; // 1900+
22 bool leapYear(); // leap year
23 int monthDays(); // days in month
24 }; // end class Date
25
26 #endif
1 // Exercise 9.8 Solution: Date.cpp
2 // Member-function definitions for class Date.
3 #include <iostream>
4 using std::cout;
5
6 #include "Date.h" // include definition of class Date
cpphtp5_09_IM.fm Page 495 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
496 Chapter 9 Classes: A Deeper Look, Part 1
7
8 Date::Date( int m, int d, int y )
9 {
10 setDate( m, d, y ); // sets date
11 } // end Date constructor
12
13 void Date::setDate( int mo, int dy, int yr )
14 {
15 setMonth( mo ); // invokes function setMonth
16 setDay( dy ); // invokes function setDay
17 setYear( yr ); // invokes function setYear
18 } // end function setDate
19
20 void Date::setDay( int d )
21 {
22 if ( month == 2 && leapYear() )
23 day = ( d <= 29 && d >= 1 ) ? d : 1;
24 else
25 day = ( d <= monthDays() && d >= 1 ) ? d : 1;
26 } // end function setDay
27
28 void Date::setMonth( int m )
29 {
30 month = m <= 12 && m >= 1 ? m : 1; // sets month
31 } // end function setMonth
32
33 void Date::setYear( int y )
34 {
35 year = y >= 1900 ? y : 1900; // sets year
36 } // end function setYear
37
38 int Date::getDay()
39 {
40 return day;
41 } // end function getDay
42
43 int Date::getMonth()
44 {
45 return month;
46 } // end function getMonth
47
48 int Date::getYear()
49 {
50 return year;
51 } // end function getYear
52
53 void Date::print()
54 {
55 cout << month << '-' << day << '-' << year << '\n'; // outputs date
56 } // end function print
57
58 void Date::nextDay()
59 {
60 setDay( day + 1 ); // increments day by 1
61
cpphtp5_09_IM.fm Page 496 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 497
62 if ( day == 1 )
63 {
64 setMonth( month + 1 ); // increments month by 1
65
66 if ( month == 1 )
67 setYear( year + 1 ); // increments year by 1
68 } // end if statement
69 } // end function nextDay
70
71 bool Date::leapYear()
72 {
73 if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) )
74 return true; // is a leap year
75 else
76 return false; // is not a leap year
77 } // end function leapYear
78
79 int Date::monthDays()
80 {
81 const int days[ 12 ] =
82 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
83
84 return month == 2 && leapYear() ? 29 : days[ month - 1 ];
85 } // end function monthDays
1 // Exercise 9.8 Solution: Ex09_08.cpp
2 #include <iostream>
3 using std::cout;
4 using std::endl;
5
6 #include "Date.h" // include definitions of class Date
7
8 int main()
9 {
10 const int MAXDAYS = 16;
11 Date d( 12, 24, 2004 ); // instantiate object d of class Date
12
13 // output Date object d's value
14 for ( int loop = 1; loop <= MAXDAYS; ++loop )
15 {
16 d.print(); // invokes function print
17 d.nextDay(); // invokes function next day
18 } // end for
19
20 cout << endl;
21 return 0;
22 } // end main
cpphtp5_09_IM.fm Page 497 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
498 Chapter 9 Classes: A Deeper Look, Part 1
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 ex-
isting class definitions.) Modify the tick function to call the nextDay function if the time incre-
ments 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.
ANS:

12-24-2004
12-25-2004
12-26-2004
12-27-2004
12-28-2004
12-29-2004
12-30-2004
12-31-2004
1-1-2005
1-2-2005
1-3-2005
1-4-2005
1-5-2005
1-6-2005
1-7-2005
1-8-2005
1 // Exercise 9.9 Solution: DateAndTime.h
2 #ifndef DATEANDTIME_H
3 #define DATEANDTIME_H
4
5 class DateAndTime
6 {
7 public:
8 DateAndTime( int = 1, int = 1, int = 1900,
9 int = 0, int = 0, int = 0 ); // default constructor
10 void setDate( int, int, int ); // set month, day, year
11 void setMonth( int ); // set month
12 void setDay( int ); // set day
13 void setYear( int ); // set year
14 void nextDay(); // next day
15 void setTime( int, int, int ); // set hour, minute, second
16 void setHour( int ); // set hour
17 void setMinute( int ); // set minute
18 void setSecond( int ); // set second
19 void tick(); // tick function
20 int getMonth(); // get month
21 int getDay(); // get day
22 int getYear(); // get year
23 int getHour(); // get hour
24 int getMinute(); // get minute
25 int getSecond(); // get second
26 void printStandard(); // print standard time
cpphtp5_09_IM.fm Page 498 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 499
27 void printUniversal(); // print universal time
28 private:
29 int month; // 1-12
30 int day; // 1-31 (except February(leap year), April, June, Sept, Nov)
31 int year; // 1900+
32 int hour; // 0-23 (24 hour clock format)
33 int minute; // 0-59
34 int second; // 0-59
35 bool leapYear(); // leap year
36 int monthDays(); // days in month
37 }; // end class DateAndTime
38
39 #endif
1 // Exercise 9.9 Solution: DateAndTime.cpp
2 // Member function definitions for class DateAndTime.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 #include "DateAndTime.h" // include definition of class DateAndTime
8
9 DateAndTime::DateAndTime(
10 int m, int d, int y, int hr, int min, int sec )
11 {
12 setDate( m, d, y ); // sets date
13 setTime( hr, min, sec ); // sets time
14 } // end DateAndTime constructor
15
16 void DateAndTime::setDate( int mo, int dy, int yr )
17 {
18 setMonth( mo ); // invokes function setMonth
19 setDay( dy ); // invokes function setday
20 setYear( yr ); // invokes function setYear
21 } // end function setDate
22
23 void DateAndTime::setDay( int d )
24 {
25 if ( month == 2 && leapYear() )
26 day = ( d <= 29 && d >= 1 ) ? d : 1;
27 else
28 day = ( d <= monthDays() && d >= 1 ) ? d : 1;
29 } // end function setDay
30
31 void DateAndTime::setMonth( int m )
32 {
33 month = m <= 12 && m >= 1 ? m : 1; // sets month
34 } // end function setMonth
35
36 void DateAndTime::setYear( int y )
37 {
38 year = y >= 1900 ? y : 1900; // sets year
39 } // end function setYear
40
cpphtp5_09_IM.fm Page 499 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
500 Chapter 9 Classes: A Deeper Look, Part 1
41 void DateAndTime::nextDay()
42 {
43 setDay( day + 1 ); // increments day by 1
44
45 if ( day == 1 )
46 {
47 setMonth( month + 1 ); // increments month by 1
48
49 if ( month == 1 )
50 setYear( year + 1 ); // increments year by 1
51 } // end if statement
52 } //end function nextDay
53
54 void DateAndTime::setTime( int hr, int min, int sec )
55 {
56 setHour( hr ); // invokes function setHour
57 setMinute( min ); // invokes function setMinute
58 setSecond( sec ); // invokes function setSecond
59 } // end function setTime
60
61 void DateAndTime::setHour( int h )
62 {
63 hour = ( h >= 0 && h < 24 ) ? h : 0; // sets hour
64 } // end function setHour
65
66 void DateAndTime::setMinute( int m )
67 {
68 minute = ( m >= 0 && m < 60 ) ? m : 0; // sets minute
69 } // end function setMinute
70
71 void DateAndTime::setSecond( int s )
72 {
73 second = ( s >= 0 && s < 60 ) ? s : 0; // sets second
74 } // end function setSecond
75
76 void DateAndTime::tick()
77 {
78 setSecond( second + 1 ); // increments second by 1
79
80 if ( second == 0 )
81 {
82 setMinute( minute + 1 ); // increments minute by 1
83
84 if ( minute == 0 )
85 {
86 setHour( hour + 1 ); // increments hour by 1
87
88 if ( hour == 0 )
89 nextDay(); // increments day by 1
90 } // end if
91 } // end if
92 } // end function tick
93
94 int DateAndTime::getDay()
95 {
cpphtp5_09_IM.fm Page 500 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 501
96 return day;
97 } // end function getDay
98
99 int DateAndTime::getMonth()
100 {
101 return month;
102 } // end function getMonth
103
104 int DateAndTime::getYear()
105 {
106 return year;
107 } // end function getYear
108
109 int DateAndTime::getHour()
110 {
111 return hour;
112 } // end function getHour
113
114 int DateAndTime::getMinute()
115 {
116 return minute;
117 } // end function getMinute
118
119 int DateAndTime::getSecond()
120 {
121 return second;
122 } // end function getSecond
123
124 void DateAndTime::printStandard()
125 {
126 cout << ( ( hour % 12 == 0 ) ? 12 : hour % 12 ) << ':'
127 << ( minute < 10 ? "0" : "" ) << minute << ':'
128 << ( second < 10 ? "0" : "" ) << second
129 << ( hour < 12 ? " AM " : " PM " )
130 << month << '-' << day << '-' << year << endl;
131 } // end function printStandard
132
133 void DateAndTime::printUniversal()
134 {
135 cout << ( hour < 10 ? "0" : "" ) << hour << ':'
136 << ( minute < 10 ? "0" : "" ) << minute << ':'
137 << ( second < 10 ? "0" : "" ) << second << " "
138 << month << '-' << day << '-' << year << endl;
139 } // end function printUniversal
140
141 bool DateAndTime::leapYear()
142 {
143 if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) )
144 return true; // is a leap year
145 else
146 return false; // is not a leap year
147 } // end function leapYear
148
149 int DateAndTime::monthDays()
150 {
cpphtp5_09_IM.fm Page 501 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
502 Chapter 9 Classes: A Deeper Look, Part 1
9.10 (Returning Error Indicators from Class Times 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 mem-
151 const int days[ 12 ] = {
152 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
153
154 return ( month == 2 && leapYear() ) ? 29 : days[ ( month - 1 ) ];
155 } // end function monthDays
1 // Exercise 9.9 Solution: Ex09_09.cpp
2 #include <iostream>
3 using std::cout;
4 using std::endl;
5
6 #include "DateAndTime.h" // include definitions of class DateAndTime
7
8 int main()
9 {
10 const int MAXTICKS = 30;
11 DateAndTime d( 12, 31, 2004, 23, 59, 57 ); // instantiates object d
12 // of class DateAndTime
13
14 for ( int ticks = 1; ticks <= MAXTICKS; ticks++ )
15 {
16 cout << "Universal time: ";
17 d.printUniversal(); // invokes function printUniversal
18 cout << "Standard time: ";
19 d.printStandard(); // invokes function printStandard
20 d.tick(); // invokes function tick
21 } // end for
22
23 cout << endl;
24 return 0;
25 } // end main
Universal time: 23:59:57 12-31-2004
Standard time: 11:59:57 PM 12-31-2004
Universal time: 23:59:58 12-31-2004
Standard time: 11:59:58 PM 12-31-2004
Universal time: 23:59:59 12-31-2004
Standard time: 11:59:59 PM 12-31-2004
Universal time: 00:00:00 1-1-2005
Standard time: 12:00:00 AM 1-1-2005
Universal time: 00:00:01 1-1-2005
Standard time: 12:00:01 AM 1-1-2005
.
.
.

cpphtp5_09_IM.fm Page 502 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 503
ber 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.
ANS:
1 // Exercise 9.10 Solution: Time.h
2 #ifndef TIME_H
3 #define TIME_H
4
5 class Time
6 {
7 public:
8 Time( int = 0, int = 0, int = 0 ); // default constructor
9 bool setTime( int, int, int ); // set hour, minute, second
10 bool setHour( int ); // set hour
11 bool setMinute( int ); // set minute
12 bool setSecond( int ); // set second
13 int getHour(); // get hour
14 int getMinute(); // get minute
15 int getSecond(); // get second
16 void printUniversal(); // print universal time
17 void printStandard(); // print standard time
18 private:
19 int hour; // 0-23
20 int minute; // 0-59
21 int second; // 0-59
22 }; // end class Time
23
24 #endif
1 // Exercise 9.10 Solution: Time.cpp
2 // Member-function definitions for class Time.
3 #include <iostream>
4 using std::cout;
5
6 #include "Time.h" // include definition of class Time
7
8 Time::Time( int hr, int min, int sec )
9 {
10 setTime( hr, min, sec );
11 } // end Time constructor
12
13 bool Time::setTime( int h, int m, int s )
14 {
15 bool hourValid = setHour( h ); // invokes function setHour
16 bool minuteValid = setMinute( m ); // invokes function setMinute
17 bool secondValid = setSecond( s ); // invokes function setSecond
18 return hourValid && minuteValid && secondValid;
19 } // end function setTime
20
21 bool Time::setHour( int hr )
22 {
23 if ( hr >= 0 && hr < 24 )
24 {
cpphtp5_09_IM.fm Page 503 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
504 Chapter 9 Classes: A Deeper Look, Part 1
25 hour = hr;
26 return true; // hour is valid
27 } // end if
28 else
29 {
30 hour = 0;
31 return false; // hour is invalid
32 } // end else
33 } // end function setHour
34
35 bool Time::setMinute( int min )
36 {
37 if ( min >= 0 && min < 60 )
38 {
39 minute = min;
40 return true; // minute is valid
41 } // end if
42 else
43 {
44 minute = 0;
45 return false; // minute is invalid
46 } // end else
47 } // end function setMinute
48
49 bool Time::setSecond( int sec )
50 {
51 if ( sec >= 0 && sec < 60 )
52 {
53 second = sec;
54 return true; // second is valid
55 } // end if
56 else
57 {
58 second = 0;
59 return false; // second is invalid
60 } // end else
61 } // end function setSecond
62
63 // return hour value
64 int Time::getHour()
65 {
66 return hour;
67 } // end function getHour
68
69 // return minute value
70 int Time::getMinute()
71 {
72 return minute;
73 } // end function getMinute
74
75 // return second value
76 int Time::getSecond()
77 {
78 return second;
79 } // end function getSecond
cpphtp5_09_IM.fm Page 504 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 505
80
81 void Time::printUniversal()
82 {
83 cout << ( hour < 10 ? "0" : "" ) << hour << ':'
84 << ( minute < 10 ? "0" : "" ) << minute << ':'
85 << ( second < 10 ? "0" : "" ) << second;
86 } // end function printUniversal
87
88 void Time::printStandard()
89 {
90 cout << ( ( hour % 12 == 0 ) ? 12 : hour % 12 ) << ':'
91 << ( minute < 10 ? "0": "" ) << minute << ':'
92 << ( second < 10 ? "0": "" ) << second
93 << ( hour < 12 ? " AM" : " PM" );
94 } // end function printStandard
1 // Exercise 9.10 Solution: Ex09_10.cpp
2 #include <iostream>
3 using std::cin;
4 using std::cout;
5 using std::endl;
6
7 #include "Time.h" // include definition of class Time
8
9 int main()
10 {
11 Time time; // the Time object
12
13
14 // all t1 object's times are valid
15 if ( !t1.getInvalidTime() )
16 cout << "Error: invalid time setting(s) attempted." << '\n'
17 << "Invalid setting(s) changed to zero." << '\n';
18
19 t1.printStandard(); // invokes function print standard
20
21 // object t2 has invalid time settings
22 if ( !t2.getInvalidTime() )
23 cout << "\nError: invalid time setting(s) attempted.\n"
24 << "Invalid setting(s) changed to zero.\n";
25
26 t2.printUniversal(); // invokes function print universal
27 cout << endl;
28
29 return 0;
30 } // end main
1 // Exercise 9.10 Solution: Ex09_10.cpp
2 #include <iostream>
3 using std::cin;
4 using std::cout;
5 using std::endl;
6
cpphtp5_09_IM.fm Page 505 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
506 Chapter 9 Classes: A Deeper Look, Part 1
7 #include "Time.h" // include definition of class Time
8
9 int getMenuChoice(); // prototype
10
11 int main()
12 {
13 Time time; // the Time object
14 int choice = getMenuChoice();
15 int hours;
16 int minutes;
17 int seconds;
18
19 while ( choice != 4 )
20 {
21 switch ( choice )
22 {
23 case 1: // set hour
24 cout << "Enter Hours: ";
25 cin >> hours;
26
27 if ( !time.setHour( hours ) )
28 cout << "Invalid hours." << endl;
29 break;
30 case 2: // set minute
31 cout << "Enter Minutes: ";
32 cin >> minutes;
33
34 if ( !time.setMinute( minutes ) )
35 cout << "Invalid minutes." << endl;
36 break;
37 case 3: // set seconds
38 cout << "Enter Seconds: ";
39 cin >> seconds;
40
41 if ( !time.setSecond( seconds ) )
42 cout << "Invalid seconds." << endl;
43 break;
44 } // end switch
45
46 cout << "Hour: " << time.getHour() << " Minute: "
47 << time.getMinute() << " Second: " << time.getSecond() << endl;
48 cout << "Universal time: ";
49 time.printUniversal();
50 cout << " Standard time: ";
51 time.printStandard();
52 cout << endl;
53
54 choice = getMenuChoice();
55 } // end while
56 } // end main
57
58 // prints a menu and returns a value corresponding to the menu choice
59 int getMenuChoice()
60 {
61 int choice;
cpphtp5_09_IM.fm Page 506 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 507
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 ver-
ify that length and width are each floating-point numbers larger than 0.0 and less than 20.0.
ANS:
62
63 cout << "1. Set Hour\n2. Set Minute\n3. Set Second\n"
64 << "4. Exit\nChoice: " << endl;
65 cin >> choice;
66 return choice;
67 } // end function getMenuChoice
1. Set Hour
2. Set Minute
3. Set Second
4. Exit
Choice:
1
Enter Hours: 17
Hour: 17 Minute: 0 Second: 0
Universal time: 17:00:00 Standard time: 5:00:00 PM
1. Set Hour
2. Set Minute
3. Set Second
4. Exit
Choice:
2
Enter Minutes: 65
Invalid minutes.
Hour: 17 Minute: 0 Second: 0
Universal time: 17:00:00 Standard time: 5:00:00 PM
1. Set Hour
2. Set Minute
3. Set Second
4. Exit
Choice:
3
Enter Seconds: 23
Hour: 17 Minute: 0 Second: 23
Universal time: 17:00:23 Standard time: 5:00:23 PM
1. Set Hour
2. Set Minute
3. Set Second
4. Exit
Choice:
4
1 // Exercise 9.11 Solution: Rectangle.h
2 #ifndef RECTANGLE_H
3 #define RECTANGLE_H
4
5 class Rectangle
6 {
cpphtp5_09_IM.fm Page 507 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
508 Chapter 9 Classes: A Deeper Look, Part 1
7 public:
8 Rectangle( double = 1.0, double = 1.0 ); // default constructor
9 void setWidth( double w ); // set width
10 void setLength( double l ); // set length
11 double getWidth(); // get width
12 double getLength(); // get length
13 double perimeter(); // perimeter
14 double area(); // area
15 private:
16 double length; // 1.0 < length < 20.0
17 double width; // 1.0 < width < 20.0
18 }; // end class Rectangle
19
20 #endif
1 // Exercise 9.11 Solution: Rectangle.cpp
2 // Member-function definitions for class Rectangle.
3
4 #include "Rectangle.h" // include definition of class Rectangle
5
6 Rectangle::Rectangle( double w, double l )
7 {
8 setWidth(w); // invokes function setWidth
9 setLength(l); // invokes function setLength
10 } // end Rectangle constructor
11
12 void Rectangle::setWidth( double w )
13 {
14 width = w > 0 && w < 20.0 ? w : 1.0; // sets width
15 } // end function setWidth
16
17 void Rectangle::setLength( double l )
18 {
19 length = l > 0 && l < 20.0 ? l : 1.0; // sets length
20 } // end function setLength
21
22 double Rectangle::getWidth()
23 {
24 return width;
25 } // end function getWidth
26
27 double Rectangle::getLength()
28 {
29 return length;
30 } // end fucntion getLength
31
32 double Rectangle::perimeter()
33 {
34 return 2 * ( width + length ); // returns perimeter
35 } // end function perimeter
36
37 double Rectangle::area()
38 {
39 return width * length; // returns area
cpphtp5_09_IM.fm Page 508 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 509
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 func-
tion also verifies that the supplied coordinates do, in fact, specify a rectangle. Provide member func-
tions 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.
ANS:
40 } // end function area
1 // Exercise 9.11 Solution: Ex09_11.cpp
2 #include <iostream>
3 using std::cout;
4 using std::endl;
5 using std::fixed;
6
7 #include <iomanip>
8 using std::setprecision;
9
10 #include "Rectangle.h" // include definition of class Rectangle
11
12 int main()
13 {
14 Rectangle a, b( 4.0, 5.0 ), c( 67.0, 888.0 );
15
16 cout << fixed;
17 cout << setprecision( 1 );
18
19 // output Rectangle a
20 cout << "a: length = " << a.getLength() << "; width = "
21 << a.getWidth() << "; perimeter = " << a.perimeter()
22 << "; area = " << a.area() << '\n';
23
24 // output Rectangle b
25 cout << "b: length = " << b.getLength() << "; width = "
26 << b.getWidth() << "; perimeter = " << b.perimeter()
27 << "; area = " << b.area() << '\n';
28
29 // output Rectangle c; bad values attempted
30 cout << "c: length = " << c.getLength() << "; width = "
31 << c.getWidth() << "; perimeter = " << c.perimeter()
32 << "; area = " << c.area() << endl;
33 return 0;
34 } // end main

a: length = 1.0; width = 1.0; perimeter = 4.0; area = 1.0
b: length = 5.0; width = 4.0; perimeter = 18.0; area = 20.0
c: length = 1.0; width = 1.0; perimeter = 4.0; area = 1.0
1 // Exercise 9.12 Solution: Point.h
cpphtp5_09_IM.fm Page 509 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
510 Chapter 9 Classes: A Deeper Look, Part 1
2 #ifndef POINT_H
3 #define POINT_H
4
5 class Point
6 {
7 public:
8 Point( double = 0.0, double = 0.0 ); // default constructor
9
10 // set and get functions
11 void setX( double );
12 void setY( double );
13 double getX();
14 double getY();
15 private:
16 double x; // 0.0 <= x <= 20.0
17 double y; // 0.0 <= y <= 20.0
18 }; // end class Point
19
20 #endif
1 // Exercise 9.12 Solution: Point.cpp
2 // Member-function definitions for class Point.
3
4 #include "Point.h" // include definition of class Point
5
6 Point::Point( double xCoord, double yCoord )
7 {
8 setX( xCoord ); // invoke function setX
9 setY( yCoord ); // invoke function setY
10 } // end Point constructor
11
12 // set x coordinate
13 void Point::setX( double xCoord )
14 {
15 x = ( xCoord >= 0.0 && xCoord <= 20.0 ) ? xCoord : 0.0;
16 } // end function setX
17
18 // set y coordinate
19 void Point::setY( double yCoord )
20 {
21 y = ( yCoord >= 0.0 && yCoord <= 20.0 ) ? yCoord : 0.0;
22 } // end function setY
23
24 // return x coordinate
25 double Point::getX()
26 {
27 return x;
28 } // end function getX
29
30 // return y coordinate
31 double Point::getY()
32 {
33 return y;
cpphtp5_09_IM.fm Page 510 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 511
34 } // end function getY
1 // Exercise 9.12 Solution: Rectangle.h
2 #ifndef RECTANGLE_H
3 #define RECTANGLE_H
4
5 #include "Point.h" // include definition of class Point
6
7 class Rectangle
8 {
9 public:
10 // default constructor
11 Rectangle( Point = Point( 0.0, 1.0 ), Point = Point( 1.0, 1.0 ),
12 Point = Point( 1.0, 0.0 ), Point = Point( 0.0, 0.0 ) );
13
14 // sets x, y, x2, y2 coordinates
15 void setCoord( Point, Point, Point, Point );
16 double length(); // length
17 double width(); // width
18 void perimeter(); // perimeter
19 void area(); // area
20 bool square(); // square
21 private:
22 Point point1;
23 Point point2;
24 Point point3;
25 Point point4;
26 }; // end class Rectangle
27
28 #endif
1 // Exercise 9.12 Solution: Rectangle.cpp
2 // Member-function definitions for class Rectangle.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 #include <iomanip>
8 using std::fixed;
9 using std::setprecision;
10
11 #include <cmath>
12 using std::fabs;
13
14 #include "Rectangle.h" // include definition of class Rectangle
15
16 Rectangle::Rectangle( Point a, Point b, Point c, Point d )
17 {
18 setCoord( a, b, c, d ); // invokes function setCoord
19 } // end Rectangle constructor
20
21 void Rectangle::setCoord( Point p1, Point p2, Point p3, Point p4 )
22 {
cpphtp5_09_IM.fm Page 511 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
512 Chapter 9 Classes: A Deeper Look, Part 1
23 // Arrangement of points
24 // p4.........p3
25 // . .
26 // . .
27 // p1.........p2
28
29 // verify that points form a rectangle
30 if ( ( p1.getY() == p2.getY() && p1.getX() == p4.getX()
31 && p2.getX() == p3.getX() && p3.getY() == p4.getY() ) )
32 {
33 point1 = p1;
34 point2 = p2;
35 point3 = p3;
36 point4 = p4;
37 } // end if
38 else
39 {
40 cout << "Coordinates do not form a rectangle!\n"
41 << "Use default values.\n";
42 point1 = Point( 0.0, 1.0 );
43 point2 = Point( 1.0, 1.0 );
44 point3 = Point( 1.0, 0.0 );
45 point4 = Point( 0.0, 0.0 );
46 } // end else
47 } // end function setCoord
48
49 double Rectangle::length()
50 {
51 double side1 = fabs( point4.getY() - point1.getY() ); // get side1
52 double side2 = fabs( point2.getX() - point1.getX() ); // get side2
53 double length = ( side1 > side2 ? side1 : side2 );
54 return length;
55 } // end function length
56
57 double Rectangle::width()
58 {
59 double side1 = fabs( point4.getY() - point1.getY() ); // get side1
60 double side2 = fabs( point2.getX() - point1.getX() ); // get side2
61 double width = ( side1 < side2 ? side1 : side2 );
62 return width;
63 } // end function width
64
65 void Rectangle::perimeter()
66 {
67 cout << fixed << "\nThe perimeter is: " << setprecision( 1 )
68 << 2 * ( length() + width() ) << endl;
69 } // end function perimeter
70
71 void Rectangle::area()
72 {
73 cout << fixed << "The area is: " << setprecision( 1 )
74 << length() * width() << endl;
75 } // end function area
76
77 bool Rectangle::square()
cpphtp5_09_IM.fm Page 512 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 513
78 {
79 return ( fabs( point4.getY() - point1.getY() ) ==
80 fabs( point2.getX() - point1.getX() ) );
81 } // end function square
1 // Exercise 9.12 Solution: Ex09_12.cpp
2 #include <iostream>
3 using std::cout;
4
5 #include "Rectangle.h" // include definition of class Rectangle
6
7 int main()
8 {
9 Point w( 1.0, 1.0 );
10 Point x( 5.0, 1.0 );
11 Point y( 5.0, 3.0 );
12 Point z( 1.0, 3.0 );
13 Point j( 0.0, 0.0 );
14 Point k( 1.0, 0.0 );
15 Point m( 1.0, 1.0 );
16 Point n( 0.0, 1.0 );
17 Point v( 99.0, -2.3 );
18
19 Rectangle rectangles[ 4 ]; // array stores four rectangles
20
21 // output rectangles
22 for ( int i = 0; i < 4; i++ )
23 {
24 cout << "Rectangle" << i + 1 << ":\n";
25
26 switch ( i ) // initialize four different rectangles
27 {
28 case 0: // first rectangle
29 rectangles[ i ] = Rectangle( z, y, x, w );
30 break;
31 case 1: // second rectangle
32 rectangles[ i ] = Rectangle( j, k, m, n );
33 break;
34 case 2: // third rectangle
35 rectangles[ i ] = Rectangle( w, x, m, n );
36 break;
37 case 3: // fourth rectangle
38 rectangles[ i ] = Rectangle( v, x, y, z );
39 break;
40 } // end switch
41
42 cout << "length = " << rectangles[ i ].length();
43 cout << "\nwidth = " << rectangles[ i ].width();
44 rectangles[ i ].perimeter();
45 rectangles[ i ].area();
46 cout << "The rectangle "
47 << ( rectangles[ i ].square() ? "is" : "is not" )
48 << " a square.\n";
49 } // end for
cpphtp5_09_IM.fm Page 513 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
514 Chapter 9 Classes: A Deeper Look, Part 1
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 desig-
nated portion of the first quadrant.
ANS:
50
51 return 0;
52 } // end main
Rectangle1:
length = 4
width = 2
The perimeter is: 12.0
The area is: 8.0
The rectangle is not a square.
Rectangle2:
length = 1.0
width = 1.0
The perimeter is: 4.0
The area is: 1.0
The rectangle is a square.
Rectangle3:
Coordinates do not form a rectangle!
Use default values.
length = 1.0
width = 1.0
The perimeter is: 4.0
The area is: 1.0
The rectangle is a square.
Rectangle4:
Coordinates do not form a rectangle!
Use default values.
length = 1.0
width = 1.0
The perimeter is: 4.0
The area is: 1.0
The rectangle is a square.
1 // Exercise 9.13 Solution: Rectangle.h
2 #ifndef RECTANGLE_H
3 #define RECTANGLE_H
4
5 #include "Point.h" // include definition of class Point
6
7 class Rectangle
8 {
9 public:
10 // default constructor
11 Rectangle( Point = Point( 0.0, 1.0 ), Point = Point( 1.0, 1.0 ),
12 Point = Point( 1.0, 0.0 ), Point = Point( 0.0, 0.0 ),
cpphtp5_09_IM.fm Page 514 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 515
13 char = '*', char = '*' );
14
15 // sets x, y, x2, y2 coordinates
16 void setCoord( Point, Point, Point, Point );
17 double length(); // length
18 double width(); // width
19 void perimeter(); // perimeter
20 void area(); // area
21 bool square(); // square
22 void draw(); // draw rectangle
23 void setPerimeterCharacter( char ); // set perimeter character
24 void setFillCharacter( char ); // set fill character
25 private:
26 Point point1;
27 Point point2;
28 Point point3;
29 Point point4;
30 char fillCharacter;
31 char perimeterCharacter;
32 }; // end class Rectangle
33
34 #endif
1 // Exercise 9.13 Solution: Rectangle.cpp
2 // Member-function definitions for class Rectangle.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 #include <iomanip>
8 using std::fixed;
9 using std::setprecision;
10
11 #include <cmath>
12 using std::fabs;
13
14 #include "Rectangle.h" // include definition of class Rectangle
15
16 Rectangle::Rectangle( Point a, Point b, Point c, Point d,
17 char fillChar, char perimeterChar )
18 {
19 setCoord( a, b, c, d ); // invoke function setCoord
20 setFillCharacter( fillChar ); // set fill character
21 setPerimeterCharacter( perimeterChar ); // set perimeter character
22 } // end Rectangle constructor
23
24 void Rectangle::setCoord( Point p1, Point p2, Point p3, Point p4 )
25 {
26 // Arrangement of points
27 // p4.........p3
28 // . .
29 // . .
30 // p1.........p2
31
cpphtp5_09_IM.fm Page 515 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
516 Chapter 9 Classes: A Deeper Look, Part 1
32 // verify that points form a rectangle
33 if ( ( p1.getY() == p2.getY() && p1.getX() == p4.getX()
34 && p2.getX() == p3.getX() && p3.getY() == p4.getY() ) )
35 {
36 point1 = p1;
37 point2 = p2;
38 point3 = p3;
39 point4 = p4;
40 } // end if
41 else
42 {
43 cout << "Coordinates do not form a rectangle!\n"
44 << "Use default values.\n";
45 point1 = Point( 0.0, 1.0 );
46 point2 = Point( 1.0, 1.0 );
47 point3 = Point( 1.0, 0.0 );
48 point4 = Point( 0.0, 0.0 );
49 } // end else
50 } // end function setCoord
51
52 double Rectangle::length()
53 {
54 double side1 = fabs( point4.getY() - point1.getY() ); // get side1
55 double side2 = fabs( point2.getX() - point1.getX() ); // get side2
56 double length = ( side1 > side2 ? side1 : side2 );
57 return length;
58 } // end function length
59
60 double Rectangle::width()
61 {
62 double side1 = fabs( point4.getY() - point1.getY() ); // get side1
63 double side2 = fabs( point2.getX() - point1.getX() ); // get side2
64 double width = ( side1 < side2 ? side1 : side2 );
65 return width;
66 } // end function width
67
68 void Rectangle::perimeter()
69 {
70 cout << fixed << "\nThe perimeter is: " << setprecision( 1 )
71 << 2 * ( length() + width() ) << endl;
72 } // end function perimeter
73
74 void Rectangle::area()
75 {
76 cout << fixed << "The area is: " << setprecision( 1 )
77 << length() * width() << endl;
78 } // end function area
79
80 bool Rectangle::square()
81 {
82 return ( fabs( point4.getY() - point1.getY() ) ==
83 fabs( point2.getX() - point1.getX() ) );
84 } // end function square
85
86 // draw rectangle
cpphtp5_09_IM.fm Page 516 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 517
87 void Rectangle::draw()
88 {
89 for ( double y = 25.0; y >= 0.0; y-- )
90 {
91 for ( double x = 0.0; x <= 25.0; x++ )
92 {
93 if ( ( point1.getX() == x && point1.getY() == y ) ||
94 ( point4.getX() == x && point4.getY() == y ) )
95 {
96 // print horizontal perimeter of rectangle
97 while ( x <= point2.getX() )
98 {
99 cout << perimeterCharacter;
100 x++;
101 } // end while
102
103 cout << '.'; // print remainder of quadrant
104 } // end if
105 else if ( ( ( x <= point4.getX() && x >= point1.getX() ) ) &&
106 point4.getY() >= y && point1.getY() <= y )
107 {
108 cout << perimeterCharacter;
109
110 // fill inside of rectangle
111 for ( x++; x < point2.getX(); )
112 {
113 cout << fillCharacter;
114 x++;
115 } // end for
116
117 cout << perimeterCharacter;
118 } // end else if
119 else
120 cout << '.'; // print quadrant background
121 } // end for
122
123 cout << '\n';
124 } // end for
125 } // end function draw
126
127 // set fill character
128 void Rectangle::setFillCharacter( char fillChar )
129 {
130 fillCharacter = fillChar;
131 } // end function setFillCharacter
132
133 // set perimeter character
134 void Rectangle::setPerimeterCharacter( char perimeterChar )
135 {
136 perimeterCharacter = perimeterChar;
137 } // end function setPerimeterCharacter
1 // Exercise 9.13 Solution: Ex09_13.cpp
2
cpphtp5_09_IM.fm Page 517 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
518 Chapter 9 Classes: A Deeper Look, Part 1
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 sub-
stract. For comparing HugeInteger objects, provide functions isEqualTo, isNotEqualTo, isGrea-
terThan, isLessThan, isGreaterThanOrEqualTo and isLessThanOrEqualToeach of these is a
predicate function that simply returns true if the relationship holds between the two Huge-
Integers and returns false if the relationship does not hold. Also, provide a predicate function is-
Zero. If you feel ambitious, provide member functions multiply, divide and modulus.
ANS:
3 #include "Rectangle.h" // include definition of class Rectangle
4
5 int main()
6 {
7 Point point1( 12.0, 12.0 );
8 Point point2( 18.0, 12.0 );
9 Point point3( 18.0, 20.0 );
10 Point point4( 12.0, 20.0 );
11 Rectangle rectangle( point1, point2, point3, point4, '?', '*' );
12 rectangle.draw(); // invokes function draw
13 return 0;
14 } // end main
..........................
..........................
..........................
..........................
..........................
............*******.......
............*?????*.......
............*?????*.......
............*?????*.......
............*?????*.......
............*?????*.......
............*?????*.......
............*?????*.......
............*******.......
..........................
..........................
..........................
..........................
..........................
..........................
..........................
..........................
..........................
..........................
..........................
..........................
1 // Exercise 9.14 Solution: HugeInteger.h
2 // HugeInteger class definition.
3 #ifndef HUGEINTEGER_H
4 #define HUGEINTEGER_H
5
cpphtp5_09_IM.fm Page 518 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 519
6 class HugeInteger
7 {
8 public:
9
10 HugeInteger( long = 0 ); // conversion/default constructor
11 HugeInteger( const char * ); // copy constructor
12
13 // addition operator; HugeInteger + HugeInteger
14 HugeInteger add( const HugeInteger & );
15
16 // addition operator; HugeInteger + int
17 HugeInteger add( int );
18
19 // addition operator;
20 // HugeInteger + string that represents large integer value
21 HugeInteger add( const char * );
22
23 // subtraction operator; HugeInteger - HugeInteger
24 HugeInteger subtract( const HugeInteger & );
25
26 // subtraction operator; HugeInteger - int
27 HugeInteger subtract( int );
28
29 // subtraction operator;
30 // HugeInteger - string that represents large integer value
31 HugeInteger subtract( const char * );
32
33 bool isEqualTo( HugeInteger & ); // is equal to
34 bool isNotEqualTo( HugeInteger & ); // not equal to
35 bool isGreaterThan(HugeInteger & ); // greater than
36 bool isLessThan( HugeInteger & ); // less than
37 bool isGreaterThanOrEqualTo( HugeInteger & ); // greater than
38 // or equal to
39 bool isLessThanOrEqualTo( HugeInteger & ); // less than or equal
40 bool isZero(); // is zero
41 void input( const char * ); // input
42 void output(); // output
43 short* getInteger(); // get integer
44 private:
45 short integer[ 40 ]; // 40 element array
46 }; // end class HugeInteger
47
48 #endif
1 // Exercise 9.14 Solution: HugeInteger.cpp
2 // Member-function definitions for class HugeInteger.
3 #include <iostream>
4 using std::cin;
5 using std::cout;
6
7 #include "HugeInteger.h" // include definiton of class HugeInteger
8
9 // default constructor; conversion constructor that converts
10 // a long integer into a HugeInteger object
cpphtp5_09_IM.fm Page 519 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
520 Chapter 9 Classes: A Deeper Look, Part 1
11 HugeInteger::HugeInteger( long value )
12 {
13 // initialize array to zero
14 for ( int i = 0; i < 40; i++ )
15 integer[ i ] = 0;
16
17 // place digits of argument into array
18 for ( int j = 39; value != 0 && j >= 0; j-- )
19 {
20 integer[ j ] = static_cast< short > ( value % 10 );
21 value /= 10;
22 } // end inner for
23 } // end HugeInteger constructor
24
25 // copy constructor;
26 // converts a char string representing a large integer into a HugeInteger
27 HugeInteger::HugeInteger( const char *string )
28 {
29 this->input( string ); // call input to initialize HugeInteger
30 } // end HugeInteger constructor
31
32 // addition operator; HugeInteger + HugeInteger
33 HugeInteger HugeInteger::add( const HugeInteger &op2 )
34 {
35 HugeInteger temp; // temporary result
36 int carry = 0;
37
38 // iterate through HugeInteger
39 for ( int i = 39; i >= 0; i-- )
40 {
41 temp.integer[ i ] =
42 integer[ i ] + op2.integer[ i ] + carry;
43
44 // determine whether to carry a 1
45 if ( temp.integer[ i ] > 9 )
46 {
47 temp.integer[ i ] %= 10; // reduce to 0-9
48 carry = 1;
49 } // end if
50 else // no carry
51 carry = 0;
52 } // end for
53
54 return temp; // return the sum
55 } // end function add
56
57 // addition operator; HugeInteger + int
58 HugeInteger HugeInteger::add( int op2 )
59 {
60 // convert op2 to a HugeInteger, then invoke add
61 return this->add( HugeInteger( op2 ) );
62 } // end function add
63
64 // HugeInteger + string that represents large integer value
65 HugeInteger HugeInteger::add( const char *op2 )
cpphtp5_09_IM.fm Page 520 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 521
66 {
67 // convert op2 to a HugeInteger, then invoke add
68 return this->add( HugeInteger( op2 ) );
69 } // end function add
70
71 // function to subtract two HugeIntegers
72 HugeInteger HugeInteger::subtract( const HugeInteger &op2 )
73 {
74 HugeInteger temp; // temporary result
75 int borrow = 0;
76
77 // iterate through HugeInteger
78 for ( int i = 39; i >= 0; i-- )
79 {
80 // determine to add 10 to smaller integer
81 if ( integer[i] < op2.integer[i] )
82 {
83 temp.integer[ i ] =
84 ( integer[ i ] + 10 ) - op2.integer[ i ] - borrow;
85 borrow = 1; // set borrow to one
86 } // end if
87 else // if borrowing is not needed
88 {
89 temp.integer[ i ] =
90 integer[ i ] - op2.integer[ i ] - borrow;
91 borrow = 0; // set borrow to zero
92 } // end else
93 } // end for
94
95 return temp; // return difference of two HugeIntegers
96 } // end function subtract
97
98 // function to subtract an integer from a HugeInteger
99 HugeInteger HugeInteger::subtract( int op2 )
100 {
101 // convert op2 to a HugeInteger, then invoke subtract
102 return this->subtract( HugeInteger( op2 ) );
103 } // end function subtract
104
105 // function that takes string represeting a number
106 // and subtracts it from a HugeInteger
107 HugeInteger HugeInteger::subtract( const char *op2 )
108 {
109 // convert op2 to a HugeInteger, then invoke subtract
110 return this->subtract( HugeInteger( op2 ) );
111 } // end function subtract
112
113 // function that tests if two HugeIntegers are equal
114 bool HugeInteger::isEqualTo( HugeInteger &x )
115 {
116 return integer == x.getInteger();
117 } // end function isEqualTo
118
119 // function that tests if two HugeIntegers are not equal
120 bool HugeInteger::isNotEqualTo( HugeInteger &x )
cpphtp5_09_IM.fm Page 521 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
522 Chapter 9 Classes: A Deeper Look, Part 1
121 {
122 return !( this->isEqualTo( x ) );
123 } // end function isNotEqualTo
124
125 // function to test if one HugeInteger is greater than another
126 bool HugeInteger::isGreaterThan( HugeInteger &x )
127 {
128 return integer < x.getInteger();
129 } // end function isGreaterThan
130
131 // function that tests if one HugeInteger is less than another
132 bool HugeInteger::isLessThan( HugeInteger &x )
133 {
134 return integer > x.getInteger();
135 } // end function isLessThan
136
137 // function that tests if one HugeInteger is greater than
138 // or equal to another
139 bool HugeInteger::isGreaterThanOrEqualTo( HugeInteger &x )
140 {
141 return integer <= x.getInteger();
142 } // end function isGreaterThanOrEqualTo
143
144 // function that tests if one HugeInteger is less than or
145 // equal to another
146 bool HugeInteger::isLessThanOrEqualTo( HugeInteger &x )
147 {
148 return integer >= x.getInteger();
149 } // end function isLessThanOrEqualTo
150
151 // function that tests if a HugeInteger is zero
152 bool HugeInteger::isZero()
153 {
154 return ( getInteger() == 0 );
155 } // end function isZero
156
157 // converts a char string representing a large integer into a HugeInteger
158 void HugeInteger::input( const char *string )
159 {
160 // initialize array to zero
161 for ( int i = 0; i < 40; i++ )
162 integer[ i ] = 0;
163
164 // place digits of argument into array
165 int length = strlen( string );
166
167 for ( int j = 40 - length, k = 0; j < 40; j++, k++ )
168
169 if ( isdigit( string[ k ] ) )
170 integer[ j ] = string[ k ] - '0';
171 } // end function input
172
173 // overloaded output operator
174 void HugeInteger::output()
175 {
cpphtp5_09_IM.fm Page 522 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 523
176 int i; // used for looping
177
178 for ( i = 0; ( integer[ i ] == 0 ) && ( i <= 39 ); i++ )
179 ; // skip leading zeros
180
181 if ( i == 40 )
182 cout << 0;
183 else
184 for ( ; i <= 39; i++ ) // display the HugeInteger
185 cout << integer[ i ];
186
187 } // end function output
188
189 // return a HugeInteger
190 short* HugeInteger::getInteger()
191 {
192 return integer;
193 } // end function getInteger
1 // Exercise 9.14 Solution: Ex09_14.cpp
2 // HugeInteger test program.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 #include "HugeInteger.h" // include definiton of class HugeInteger
8
9 int main()
10 {
11
12 HugeInteger n1( 7654321 ); // HugeInteger object n1
13 HugeInteger n2( "7891234" ); // HugeInteger object n2
14 HugeInteger n3; // HugeInteger object n3
15 HugeInteger n4( 5 ); // HugeInteger object n4
16 HugeInteger n5; // HugeInteger object n5
17
18 // outputs the sum of n1 and n2
19 n5 = n1.add( n2 );
20 n1.output();
21 cout << " + ";
22 n2.output();
23 cout << " = ";
24 n5.output();
25 cout << "\n\n";
26
27 // assigns the difference of n2 and n4 to n5 then outputs n5
28 n5 = n2.subtract( n4 );
29 n2.output();
30 cout<< " - ";
31 n4.output();
32 cout << " = ";
33 n5.output();
34 cout << "\n\n";
35
cpphtp5_09_IM.fm Page 523 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
524 Chapter 9 Classes: A Deeper Look, Part 1
36 // checks for equality between n1 and n1
37 if ( n1.isEqualTo( n1 ) == true )
38 {
39 n1.output();
40 cout << " is equal ";
41 n1.output();
42 cout << "\n\n";
43 } // end if
44
45 // checks for inequality between n1 and n2
46 if ( n1.isNotEqualTo( n2 ) == true )
47 {
48 n1.output();
49 cout << " is not equal to ";
50 n2.output();
51 cout << "\n\n";
52 } // end if
53
54 // tests for greater number between n2 and n1
55 if ( n2.isGreaterThan( n1 ) == true )
56 {
57 n2.output();
58 cout << " is greater than ";
59 n1.output();
60 cout << "\n\n";
61 } // end if
62
63 // tests for smaller number between n2 and n4
64 if ( n2.isLessThan( n4 ) == true )
65 {
66 n4.output();
67 cout << " is less than ";
68 n2.output();
69 cout << "\n\n";
70 } // end if
71
72 // tests for smaller or equal number between n4 and n4
73 if ( n4.isLessThanOrEqualTo( n4 ) == true )
74 {
75 n4.output();
76 cout << " is less than or equal to ";
77 n4.output();
78 cout << "\n\n";
79 } // end if
80
81 // tests for greater or equal number between n3 and n3
82 if ( n3.isGreaterThanOrEqualTo( n3 ) == true )
83 {
84 n3.output();
85 cout << " is greater than or equal to ";
86 n3.output();
87 cout << "\n\n";
88 } // end if
89
90 // tests for zero at n3
cpphtp5_09_IM.fm Page 524 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 525
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 X in the specified square. Place an O 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-dimen-
sional 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!]
ANS:
91 if ( n3.isZero() != true )
92 {
93 cout << "n3 contains value ";
94 n3.output();
95 cout << "\n\n";
96 } // end if statement
97
98 return 0;
99 } // end main
7654321 + 7891234 = 15545555
7891234 - 5 = 7891229
7654321 is equal 7654321
7654321 is not equal to 7891234
5 is less than or equal to 5
0 is greater than or equal to 0
n3 contains value 0
1 // Exercise 9.15 Solution: TicTacToe.h
2 #ifndef TICTACTOE_H
3 #define TICTACTOE_H
4
5 class TicTacToe
6 {
7 private:
8 enum Status { WIN, DRAW, CONTINUE }; // enumeration constants
9 int board[ 3 ][ 3 ];
10 public:
11 TicTacToe(); // default constructor
12 void makeMove(); // make move
13 void printBoard(); // print board
14 bool validMove( int, int ); // validate move
15 bool xoMove( int ); // x o move
16 Status gameStatus(); // game status
17 }; // end class TicTacToe
cpphtp5_09_IM.fm Page 525 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
526 Chapter 9 Classes: A Deeper Look, Part 1
18
19 #endif
1 // Exercise 9.15 Solution: TicTacToe.cpp
2 // Member-function definitions for class TicTacToe.
3 #include <iostream>
4 using std::cin;
5 using std::cout;
6
7 #include <iomanip>
8 using std::setw;
9
10 #include "TicTacToe.h" // include definiton of class TicTacToe
11
12 TicTacToe::TicTacToe()
13 {
14 for ( int j = 0; j < 3; j++ ) // initialize board
15
16 for ( int k = 0; k < 3; k++ )
17 board[ j ][ k ] = ' ';
18 } // end TicTacToe constructor
19
20 bool TicTacToe::validMove( int r, int c )
21 {
22 return r >= 0 && r < 3 && c >= 0 && c < 3 && board[ r ][ c ] == ' ';
23 } // end function validMove
24
25 // must specify that type Status is part of the TicTacToe class.
26 // See Chapter 24 for a discussion of namespaces.
27 TicTacToe::Status TicTacToe::gameStatus()
28 {
29 int a;
30
31 // check for a win on diagonals
32 if ( board[ 0 ][ 0 ] != ' ' && board[ 0 ][ 0 ] == board[ 1 ][ 1 ] &&
33 board[ 0 ][ 0 ] == board[ 2 ][ 2 ] )
34 return WIN;
35 else if ( board[ 2 ][ 0 ] != ' ' && board[ 2 ][ 0 ] ==
36 board[ 1 ][ 1 ] && board[ 2 ][ 0 ] == board[ 0 ][ 2 ] )
37 return WIN;
38
39 // check for win in rows
40 for ( a = 0; a < 3; ++a )
41
42 if ( board[ a ][ 0 ] != ' ' && board[ a ][ 0 ] ==
43 board[ a ][ 1 ] && board[ a ][ 0 ] == board[ a ][ 2 ] )
44 return WIN;
45
46 // check for win in columns
47 for ( a = 0; a < 3; ++a )
48
49 if ( board[ 0 ][ a ] != ' ' && board[ 0 ][ a ] ==
50 board[ 1 ][ a ] && board[ 0 ][ a ] == board[ 2 ][ a ] )
51 return WIN;
cpphtp5_09_IM.fm Page 526 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 527
52
53 // check for a completed game
54 for ( int r = 0; r < 3; ++r )
55
56 for ( int c = 0; c < 3; ++c )
57
58 if ( board[ r ][ c ] == ' ' )
59 return CONTINUE; // game is not finished
60
61 return DRAW; // game is a draw
62 } // end function gameStatus
63
64 void TicTacToe::printBoard()
65 {
66 cout << " 0 1 2\n\n";
67
68 for ( int r = 0; r < 3; ++r )
69 {
70 cout << r;
71
72 for ( int c = 0; c < 3; ++c )
73 {
74 cout << setw( 3 ) << static_cast< char > ( board[ r ][ c ] );
75
76 if ( c != 2 )
77 cout << " |";
78 } // end for
79
80 if ( r != 2 )
81 cout << "\n ____|____|____\n | | \n";
82 } // end for
83
84 cout << "\n\n";
85 } // end function printBoard
86
87 void TicTacToe::makeMove()
88 {
89 printBoard();
90
91 while ( true )
92 {
93 if ( xoMove( 'X' ) )
94 break;
95 else if ( xoMove( 'O' ) )
96 break;
97 } // end while structure
98 } // end function makeMove
99
100 bool TicTacToe::xoMove( int symbol )
101 {
102 int x;
103 int y;
104
105 do
106 {
cpphtp5_09_IM.fm Page 527 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
528 Chapter 9 Classes: A Deeper Look, Part 1
107 cout << "Player " << static_cast< char >( symbol )
108 << " enter move: ";
109 cin >> x >> y;
110 cout << '\n';
111 } while ( !validMove( x, y ) );
112
113 board[ x ][ y ] = symbol;
114 printBoard();
115 Status xoStatus = gameStatus();
116
117 if ( xoStatus == WIN )
118 {
119 cout << "Player " << static_cast< char >( symbol ) << " wins!\n";
120 return true;
121 } // end if
122 else if ( xoStatus == DRAW )
123 {
124 cout << "Game is a draw.\n";
125 return true;
126 } // end else if
127 else // CONTINUE
128 return false;
129 } // end function xoMove
1 // Exercise 9.15 Solution: Ex09_15.cpp
2
3 #include "TicTacToe.h" // include definiton of class TicTacToe
4
5 int main()
6 {
7 TicTacToe g; // creates object g of class TicTacToe
8 g.makeMove(); // invokes function makeMove
9 return 0;
10 } // end main
cpphtp5_09_IM.fm Page 528 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.
Solutions 529
0 1 2
0 | |
____|____|____
| |
1 | |
____|____|____
| |
2 | |
Player X enter move: 2 0
0 1 2
0 | |
____|____|____
| |
1 | |
____|____|____
| |
2 X | |
Player O enter move: 2 2
0 1 2
0 | |
____|____|____
| |
1 | |
____|____|____
| |
2 X | | O
Player X enter move: 1 1
...
Player X enter move: 0 2
0 1 2
0 | | X
____|____|____
| |
1 | X | O
____|____|____
| |
2 X | | O
Player X wins!
cpphtp5_09_IM.fm Page 529 Thursday, December 23, 2004 4:14 PM
Copyright 1992-2005 by Deitel & Associates, Inc. All Rights Reserved.