Chapter 12 Exercise Set 1ΒΆ

  1. As you did back in the More structures chapter, rewrite the increment function from the Another example section more effeciently without iteration.

  2. 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 Time with this constructor by passing it three int 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?

  3. Use what you learned in this chapter to create a Fraction structure that has numerator and denominator instance variables, and print, plus, minus, times, and divided_by member functions. Give it two constructors, one that takes no arguments and creates a fraction with numerator set to 0 and 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.cpp and a Fraction.h header file, and write a program test_fraction.cpp to test your new fraction objects.