Chapter 12 Exercise Set 1¶
In the Initialize or construct? section, you defined a constructor for
Times 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
Timewith this constructor by passing it three
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_timeagain and run your program. What happens?
Use what you learned in this chapter to create a
Fractionstructure that has
denominatorinstance variables, and
divided_bymember functions. Give it two constructors, one that takes no arguments and creates a fraction with numerator set to
0and denominator set to
1, 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.hheader file, and write a program
test_fraction.cppto test your new fraction objects.