Skip to main content

Exercises 18.15 Exercises

1.

This exercises makes use of the Point class created in the previous chapter. See Listingย 17.6.1 for the code for it.
A Segment should connect two points (its endpoints). When we make a segment, we will give the constructor the addresses of two Points - we want the segment to use those points as its endpoints. The segment should link to those points, not make copies of them. (Aggregation)
Finish the Segment class by:
  • Adding the member variables m_p1 and m_p2 needed to hold the addresses of two Points.
  • Make a constructor that accepts two memory addresses to store.

2.

This exercises makes use of the Point class created in the previous chapter. See Listingย 17.6.1 for the code for it.
This exercises builds on Exerciseย 18.15.1 - begin by copying your code for the member variables and constructors into this problem.
Then add code for a shift(int deltaX, int deltaY) function to Segment so that we can shift the segment by some amount in x (deltaX) and some amount in y (deltaY). Moving a segment should move both of its endpoints.
Hint.
Your Segment class does not have access to the private data of Point. You will need to use Point functions to modify the two endpoints.

3.

Below is a very simple implementation of a Friend class. In the TEST_CASE, add code to create a Friend f1 named Ronnie, a Friend named Dakota, and a friend named Mary. Make Ronnie Friends with Dakota, Dakota friends with Mary, and Mary friends with Ronnie. Note that friendship is only set in one direction in this program.

4.

Add a bool bestieCheck() function to the Friend class. If a Friendโ€™s m_bestFriend is a null pointer return false. If they have a m_bestFriend, check to see if their best friendโ€™s m_bestFriend is the current object (this), if so return true, otherwise return false. In other words, your function returns true if the Friend and their best friend agree that they are best friends. Otherwise it returns false.
Hint 1.
You will need to compare the best friendโ€™s best friend to this.
Hint 2.
You should check if the current object has a best friend before you try to access best friendโ€™s best friend variable.
You have attempted of activities on this page.