Thursday, 23 April 2020

Chapter 13 // Exercise 4 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 13 // Exercise 4

Define functions n(), s(), e(), w(), center(), ne(), se(), sw(), and nw(). Each takes a Rectangle argument and returns a Point. These functions define "connection points" on and in the rectangle. For example, nw(r) is the northwest (top left) corner of a Rectangle called r.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2013/Exercise%204

Thinking about it, this makes a lot more sense to have as operations inside the Rectangle class than outside. The next exercise wants the same functions to be able to work with circles and ellipses so I decided to stick them in Shape. After some thinking and typing out all the function declarations, I figured there would be quite a bit of code duplication so I decided to create an enum of Point Directions that you can pass into one function and it returns the correct Point.

For a Circle and an Ellipse, the first initial point is the center whereas for a Rectangle its the top left. Instead of doing a dynamic_cast to determine what type of shape we are dealing with and then selecting on that; I decided to make the function virtual so a derived type can override it with it's own code if they wish.

Bjarne hasn't explicitly mentioned virtual and overriding outside a few brief mentions, but we are using that type of code so I don't consider it too advance for this stage.

In order to display all the marks I had to modify the graph files a bit as the Mark class was deriding from Marks which derides Marked_Polyline. This meant that all the marks were drawing lines between them which only Marked_Polyline should be doing. I uncommented the version of Marks below Mark and renamed it and did the same for the Marks::draw_line() function in Graph.cpp. 




No comments:

Post a Comment