Chapter 11 Exercise Set 0: Chapter ReviewΒΆ
As you did back in the Structures chapter, rewrite the
increment
function from the Another example section more effeciently without iteration.In the Initialize or construct? section, you defined a constructor for
Time
s that had two integers and a double as parameters. The combination of function name and the ordered types of its parameters is known as the function signature. The C++ compiler uses this signature to determine which version of an overloaded function to call.Try creating a new
Time
with this constructor by passing it threeint
arguments:Time current_time(9, 14, 3);
What happens? Now define a new constructor with the following
Time(int h, int m, int s);
Print out a different message inside each constructor so you can see which one was called. Create
current_time
again and run your program. What happens?Use what you learned in this chapter to create a
Fraction
structure that hasnumerator
anddenominator
instance variables, andprint
,plus
,minus
,times
, anddivided_by
member functions. Give it two constructors, one that takes no arguments and creates a fraction with numerator set to0
and denominator set to1
, and another which takes two integer arguments and initializes the numerator and denominator instance variables of the constructed object with them.Create both a
Fraction.cpp
and aFraction.h
header file, and write a programtest_fraction.cpp
to test your new fraction objects.