You are on page 1of 3

Book: Text Book Object-Oriented Programming in C++ (4th

Edition) by Robert Lafore


(Question 07 Chapter 6 Objects & Classes)

1. In ocean navigation, locations are measured in degrees and minutes of latitude and longitude.
Thus if you’re lying off the mouth of Papeete Harbor in Tahiti, your location is 149 degrees 34.8
minutes west longitude, and 17 degrees 31.5 minutes south latitude. This is written as 149°34.8’
W, 17°31.5’ S. There are 60 minutes in a degree. (An older system also divided a minute into 60
seconds, but the modern approach is to use decimal minutes instead.) Longitude is measured
from 0 to 180 degrees, east or west from Greenwich, England, to the international dateline in the
Pacific. Latitude is measured from 0 to 90 degrees, north or south from the equator to the poles.
Create a class angle that includes three member variables: an int for degrees, a float for minutes,
and a char for the direction letter (N, S, E, or W). This class can hold either a latitude variable or a
longitude variable. Write one member function to obtain an angle value (in degrees and minutes)
and a direction from the user, and a second to display the angle value in 179°59.9’ E format. Also
write a three-argument constructor. Write a main() program that displays an angle initialized with
the constructor, and then, within a loop, allows the user to input any angle value, and then
displays the value. You can use the hex character constant ‘\xF8’, which usually prints a degree (°)
symbol.

(Question 03 Chapter 6 Objects & Classes)


2. Create a class called time that has separate int member data for hours, minutes, and seconds.
One constructor should initialize this data to 0, and another should initialize it to fixed values.
Another member function should display it, in 11:59:59 format. The final member function should
add two objects of type time passed as arguments. A main() program should create two initialized
time objects (should they be const?) and one that isn’t initialized. Then it should add the two
initialized values together, leaving the result in the third time variable. Finally it should display the
value of this third variable. Make appropriate member functions const.

(Question 01 Chapter 6 Objects & Classes)


3. Create a class that imitates part of the functionality of the basic data type int. Call the class Int
(note different capitalization). The only data in this class is an int variable. Include member
functions to initialize an Int to 0, to initialize it to an int value, to display it (it looks just like an int),
and to add two Int values.

Write a program that exercises this class by creating one uninitialized and two initialized Int
values, adding the two initialized values and placing the response in the uninitialized value, and
then displaying this result.

(Question 4 Chapter 8 Operator Overloading)


4. Create a class Int based on Question 3. Overload four integer arithmetic operators (+, -, *, and /)
so that they operate on objects of type Int. If the result of any such arithmetic operation exceeds
the normal range of ints (in a 32-bit environment)—from 2,147,483,648 to –2,147,483,647—have
the operator print a warning and terminate the program. Such a data type might be useful where
mistakes caused by arithmetic overflow are unacceptable. Hint: To facilitate checking for
overflow, perform the calculations using type long double. Write a program to test this class.
(Question 3 Chapter 8 Operator Overloading)
5. Modify the time class from Question 2 so that instead of a function add_time() it uses the
overloaded + operator to add two times. Write a program to test this class.

(Question 11 Chapter 9 Inheritance)


6. Various situations require that pairs of numbers be treated as a unit. For example, each screen
coordinate has an x (horizontal) component and a y (vertical) component. Represent such a pair
of numbers as a structure called pair that comprises two int member variables. Now, assume you
want to be able to store pair variables on a stack. That is, you want to be able to place a pair
(which contains two integers) onto a stack using a single call to a push() function with a structure
of type pair as an argument, and retrieve a pair using a single call to a pop() function, which will
return a structure of type pair. Start with the Stack2 class in the STAKEN program in this chapter,
and from it derive a new class called pairStack. This new class need contain only two members:
the overloaded push() and pop() functions. The pairStack::push() function will need to make two
calls to Stack2::push() to store the two integers in its pair, and the pairStack::pop() function will
need to make two calls to Stack2::pop() (although not necessarily in the same order).

(Question 05 Chapter 10 Pointers)


7. Suppose you have a main() with three local arrays, all the same size and type (say float). The first
two are already initialized to values. Write a function called addarrays() that accepts the addresses
of the three arrays as arguments; adds the contents of the first two arrays together, element by
element; and places the results in the third array before returning. A fourth argument to this
function can carry the size of the arrays. Use pointer notation throughout; the only place you need
brackets is in defining the arrays.

(Question 01 Chapter 9 Inheritance)


8. Imagine a publishing company that markets both book and audiocassette versions of its works.
Create a class publication that stores the title (a string) and price (type float) of a publication.
From this class derive two classes: book, which adds a page count (type int), and tape, which adds
a playing time in minutes (type float). Each of these three classes should have a getdata() function
to get its data from the user at the keyboard, and a putdata() function to display its data. Write a
main() program to test the book and tape classes by creating instances of them, asking the user
to fill in data with getdata(), and then displaying the data with putdata().

(Question 01 Chapter 11 Virtual Functions)


9. Imagine the same publishing company described in Question 8 that markets both book and
audiocassette versions of its works. As in that exercise, create a class called publication that stores
the title (a string) and price (type float) of a publication. From this class derive two classes: book,
which adds a page count (type int); and tape, which adds a playing time in minutes (type float).
Each of the three classes should have a getdata() function to get its data from the user at the
keyboard, and a putdata() function to display the data. Write a main() program that creates an
array of pointers to publication. This is similar to the VIRTPERS example in this chapter. In a loop,
ask the user for data about a particular book or tape, and use new to create an object of type
book or tape to hold the data. Put the pointer to the object in the array. When the user has
finished entering the data for all books and tapes, display the resulting data for all the books and
tapes entered, using a for loop and a single statement such as
pubarr[j]->putdata();
to display the data from each object in the array.

(Question 01 Chapter 12 Streams & files)

10. Start with the Distance class from the ENGLCON example in Chapter 6, “Objects and Classes.”
Using a loop similar to that in the DISKFUN example in this chapter, get a number of Distance
values from the user, and write them to a disk file. Append them to existing values in the file, if
any. When the user signals that no more values will be input, read the file and display all the
values.

(Question 04 Chapter 12 Streams & files)


11. In a loop, prompt the user to enter name data consisting of a first name, middle initial, last name,
and employee number (type unsigned long). Then, using formatted I/O with the insertion (<<)
operator, write these four data items to an ofstream object. Don’t forget that strings must be
terminated with a space or other whitespace character. When the user indicates that no more
name data will be entered, close the ofstream object, open an ifstream object, read and display
all the data in the file, and terminate the program.

(Question 04 Chapter 14 Templates & files)


12. Create a function called swaps() that interchanges the values of the two arguments sent to it. (You
will probably want to pass these arguments by reference.) Make the function into a template, so
it can be used with all numerical data types (char, int, float, and so on). Write a main() program
to exercise the function with several types.

13. Linked List Example Chapter 10 Pointers

You might also like