Sei sulla pagina 1di 2

1. a.

Create a class named Fraction having two integer data members named for a
fractions numerator and denominator. The classs default constructor should
provide both data members with default values of 1 if no explicit user
initialization is provided. The constructor must also prohibit a 0 denominator
value. Include member functions for displaying an objects data values and
mathematical functions capable of adding, subtracting, multiplying, and dividing
two Fraction objects according to the following formulas:

Sum of two fractions:

a c ad cb

b d
bd

Difference of two fractions:


Product of two fractions:

Division of two fractions:

a c ad cb

b d
bd

a c ac

b d bd
a
b

c
d

ad
bc

b. Include the class written for Exercise 1a in a working C++ driver program that
tests each of the classs member functions.
2. a. Include a member function named gcd() in the Fraction class constructed
in Exercise 1a that reduces a fraction to its lowest common terms. For example, a
fraction such as 2/4 is reduced to 1/2. The way to do this is dividing both the
numerator and denominator values by their greatest common divisor (GCD).
b. Modify the construct written in Exercise 2a to include a call to gcd() so that
every initialized fraction is in lowest common terms. Also make sure each
mathematical function also uses gcd() to return a fraction in lowest common
terms.
c. Include the class written for Exercise 2b in a working C++ driver program that
tests each of the classs member functions.
3. You operate several hot dog stands distributed throughout town. Define a class
named HotDogStand that has a data member for the hot dog stands ID number
and a data member for how many hot dogs the stand sold that day. Create a
constructor that allows a user of the class to initialize both values. Also create a
function named JustSold that increments the number of hot dogs the stand has

sold by one. This function will be invoked each time the stand sells a hot dog so
that you can track the total number of hot dogs sold by the stand. Add another
function that returns the number of hot dog sold. Finally, add a static data member
that track the total number of hot dogs sold by all hot dog stands and a static
function that return the value in this data member. Write a main function to test
your class with at least three hot dog stands that each sells a variety of hot dogs.
Chap9: #9.14,# 9.16, #9.17, and #9.18

Potrebbero piacerti anche