Skip to main content

Exercises 17.15 Exercises

The exercises on this page make use of the point class that we used throughout the chapter. See ListingΒ 17.6.1 for the code for it.

1.

The code given below does not compile due to const issues. Modify the existing class code and add const in the appropriate locations to make it compile with the existing main and printNumber functions.

2.

The code below has a class representing people in a family who all share a last name. It implements the last name as a static class variable (an idea that almost certainly wouldn’t make sense to use in a real program.)
It is missing an initialization for the last name. Add the initialization statement for the last name so that the family members have the last name "Jones".

3.

The code below defines a class Book. It includes a declaration for a member function isLonger. It should compare the number of pages in this book with another book and return true if this book has more pages.
Hint.
You have access to the other book’s member variables. Public/private are enforced at the class level, not the object level.

4.

The code below defines a class Book. It also defines a class Reader. Implement the member function readBook for the Reader class. It should add the title of the book to the reader’s history.
Hint.
You do not have access to the private member variables of the Book class.

5.

Add a getEnd function that returns the end point of the segment. It is assumed you will return a Point value. But you may also return a reference or a const reference if you prefer.

6.

Add a Segment constructor that takes four doubles. The x and y of one end point followed by the x and y of the other point.

7.

The Segment has a function minX() declared. Implement that function after the class declaration. (Don’t forget to use Segment:: when naming the function.). It should return the lowest x value from the two Points.

8.

Write a moveBy(double dx, double dy) function that shifts a segment by the amount dy in the x dimension and dy in the y dimension. (To shift the segment, you will need to shift both endpoints.)

9.

This code uses a simplified Point class that lacks a no-arg (default) constructor. Thus while constructing a Segment, we must use the initializer list to set up m_start and m_end. Add a 2-Point constructor that uses the initializer list.
You have attempted of activities on this page.