Chapter 11 Exercise Set 0: Chapter ReviewΒΆ

  1. As you did back in the A more efficient increment exercise in chapter 8, 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. Rewrite the distance function from Structues as parameters as a member function of the Point struct.

  4. Overload the - operator for points, to support

    Point p1(7, 4);
    Point p2(3, 1);
    Point p3 = p1 - p2;
    cout << p3 << end;
    

    which should print (4, 3).

  5. Add two constructors to the Rectangle structs introduced in the Structures chapter with the following protoypes:

    Rectangle::Rectangle(Point upper_left, double width, double length);
    Rectangle::Rectangle(Point upper_left, Point lower_right);
    
  6. Add a member function area to Rectangle returns the area of the rectangle.