Friday, 26 June 2020

Chapter 14 // Exercise 16 - 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 14 // Exercise 16

Define a class Controller with four virtual functions on(), off(), set_level(int), and show(). Derive at least two classes from Controller. One should be a simple test class where show() prints out whether the class is set to on or off and what is the current level. The second derived class should somehow control the line color of a Shape; the exact meaning of "level" is up to you. Try to find a third "thing" to control with such a Controller class.

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


I really did not understand the point of this. It just made no sense to me. With the test class I don't know if he means to print it using shapes like a Text class in which case; we can't as we won't be able to attach anything to a Simple_window from within the Controller class or to print it using cout and display it in the console window.

He didn't specify what return type the functions had to be so I allowed show to return a shape that could be passed to the window to be displayed.

This exercise to me was a very confusing way of trying to get you to use inheritance. Maybe I completely misinterpreted it.


Chapter 14 // Exercise 16 - Principles & Practice Using C++

Chapter 14 // Exercise 16 - Principles & Practice Using C++

Chapter 14 // Exercise 16 - Principles & Practice Using C++

Chapter 14 // Exercise 16 - Principles & Practice Using C++

Chapter 14 // Exercise 16 - Principles & Practice Using C++

Chapter 14 // Exercise 16 - Principles & Practice Using C++

Sunday, 21 June 2020

Chapter 14 // Exercise 15 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the header file found here:
http://stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 14 // Exercise 15

Most class hierarchies have nothing to do with graphics. Define a class Iterator with a pure virtual function next() that returns a double* (see Chapter 17). 

Now derive Vector_iterator and List_iterator from Iterator so that next() for a Vector_iterator yields a pointer to the next element of a vector<double> and List_iterator does the same for a list<double>

You initialise a Vector_iterator with a vector<double> and the first call of next() yields a pointer to its first element, if any. If there is no next element, return 0. Test this by using a function void print(Iterator&) to print the elements of a vector<double> and a list<double>.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2014/Exercise%2015


Damn, he really just threw us a curve ball with this one. Iterator is actually already taken though so I renamed it My_Iterator.

The most horrible thing about this example though is that it uses concepts that haven't been introduced yet. Sure he's briefly mentioned pointers and we all know what a reference is but lists?? Returning a specific element in a list as a pointer?? I didn't know how to do these things and it's good thing I have to deal with Pointer Engine 4 at work otherwise I can imagine 2018 me staring at stackoverflow answers in confusion for days.

Anyway, I'll go through this:
Chapter 14 // Exercise 15 - Principles & Practice Using C++

The Vector_Iterator is relatively simple. I use a static int to keep track of which element we are up to as static variables don't lose scope when their functions are closed. 

The only confusing part is line 43. &v_double[i++] essentially returns a pointer to where the specific element is. We don't want to return the actual element value which we would be doing if not for the &. A pointer is an address of a variable on your computer. 

You've probably seen post-fix ++ quite a bit in code online however it's very different to pre-fix ++ (which I personally prefer). Legendary programmer Scott Meyers in Effective C++ (excellent book) also advises to "prefer pre-fix increment unless the behaviour of post-fix is specifically needed". This is because post-fix creates a temporary variable, returns that, then increments and applies it back to the original variable. Here, the post-fix is specifically needed as if 'i' is incremented before then we can go out of range and we can't increment after a return.

The List Iterator is more confusing as I've never used a list before. They are doubly linked lists and behave completely different to a vector or array. The most annoying thing about them is that you can't access the elements using the subscript [] operator. There are a few ways to access specific elements of a list but you always have to start at the first element and then go forward N elements. I decided to use the standard library function advance() which takes in a list iterator and an int. The iterator will then become a pointer to the element we want at N.

The worst part is line 70. We need to return a pointer so & is needed. However, the iterator itself is also a pointer so we need to get the actual value by using a *. The & then returns a pointer to the de-ref value...I think. Either way, it's very confusing and this should not have been introduced at this stage...or I've completely over-complicated this.

Saturday, 20 June 2020

Chapter 14 // Exercise 14 - 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 14 // Exercise 14

Add an operation to Binary_tree that adds text to a node. You may have to modify the design of Binary_tree to implement this elegantly. Choose a way to identify a node; for example, you might give a string "lrrlr" for navigating left, right, right, left, and right down a binary tree (the root node would match both an initial l and r.

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

Thankfully, because I store all the nodes in a vector in order, I can simply have the user enter a number to identify what node they want. I'm very happy about this because the left right stuff sounds tedious as fuck.

The only problem is that our programs do not have a message loop (you need to press next), so capturing data from the user is a no. This means, the programmer needs to know how many nodes there will be to know exactly which one to select and add text to. It's not great but it will do for now. The text also resizes itself based on how big the tree size is.

Chapter 14 // Exercise 14 - Principles & Practice Using C++

Friday, 19 June 2020

Chapter 14 // Exercise 13 - 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 14 // Exercise 13

Modify Binary_tree to take a parameter (or parameters) to indicate what kind of line to use to connect the nodes (e.g., an arrow pointing down or a red arrow pointing up). Note how this exercise and the last use two alternative ways of making a class hierarchy more flexible and useful.

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


Like in the last exercise, I already have the information for the lines vector. So I added a function that allows you to change the line to arrow with a direction and colour. I also added the ability to change the size of the tree.

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

This class is starting to get a bit hacky/hardcoded.

Monday, 15 June 2020

Chapter 14 // Exercise 12 - 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 14 // Exercise 12

Modify Binary_tree to draw its nodes using a virtual function. Then, derive a new class from Binary_tree that overrides that virtual function to use a different representation for a node (e.g., a triangle).

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


So I kinda cheated on this one and didn't implement any virtuals. Instead of creating new classes and new version of draw_lines for each Binary_tree type I simply created an enum that holds a node shape and then in draw_lines() it checks the shape type and then draws that shape using the types we've already defined:

Chapter 14 // Exercise 12 - Principles & Practice Using C++

This way, their individual draw_lines() can be called without having to change init_node() at all...which I really didn't want to do because I already don't understand what I wrote and it was only a week ago.

Chapter 14 // Exercise 12 - Principles & Practice Using C++