Sunday, 31 May 2020

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

Define a Striped_Rectangle where instead of fill, the rectangle is "filled" by drawing one-pixel-wide horizontal lines across the inside of the rectangle (say, draw every second like that). You may have to play with the width of the lines and the line spacing to get a pattern you like.

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


He kind of contradicts himself here. Does he want me to fill it with one pixel wide lines or whatever width I'd like? I decided to go with whatever width you'd like.


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

This also made me realise a much easier and cheaper way to do colour gradients as a fill:


Saturday, 30 May 2020

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

Define a class Immobile_Circle, which is just like a Circle but can't be moved.

Github: N/A

For this you can declare a private section of your class and then put the functions you don't want a class to be able to override in there:


When you go to use the functions on a named Immobile_circle object the compiler will error:


Friday, 29 May 2020

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

Try to copy a shape. What happens?

Github: N/A


This usually shouldn't work, however if you're using a recent version of my Graph.h and Graph.cpp files; it does work. Early in Chapter 11, I got annoyed that I couldn't return shapes; so I commented out the constructor that prevents you from doing so. Back then it wasn't explained why he had done it but he does in this chapter and it's to prevent you from attaching shapes to the window in places where the shape can go out of scope. It's useful to be able to return shapes though so I will leave it the way it is.

Chapter 14 // Exercise 3

Define an abstract class and try to define an object of that type. What happens?

Github: N/A


This question is identical to Drill 5. If you make a class and define any part of it as abstract/pure virtual (i.e., '=0') then the compiler will fail to build. Abstract classes must be derived and given definitions in the child classes.

Thursday, 28 May 2020

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

Define two classes Smiley and Frowny, which are both derived from class Circle and have two eyes and a mouth. Next, derive classes from Smiley and Frowny which add an appropriate hat to each.

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


This took me a bit longer than I thought it would, mainly because I forgot how fl_pie() works:
The top left corner of the rectangle is where fl_pie() draws from; not from the center of the circle which would honestly make more sense. So instead of figuring out where you want the center of the circle to be; calculate the top-left of the bounding box.

For the "appropriate hat", I wanted to use images. However, there was no way to resize the image to fit whatever radius the circle was. I looked around FLTK and found a badly named function called copy() that creates a new version of the image and resizes it if given a width and height.


Tuesday, 26 May 2020

Chapter 14 // Drill 1, 2, 3, 4, 5, 6, 7 - Principles & Practice Using C++

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

Chapter 14 // Drill 1

Define a class B1 with a virtual function vf() and a non-virtual function f(). Define both of these functions within class B1. Implement each function to output its name (e.g., B1::vf()). Make the functions public. Make a B1 object and call each function.

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


Chapter 14 // Drill 2

Derive a class D1 from B1 and override vf(). Make a D1 object and call vf() and f() for it.


Chapter 14 // Drill 3

Define a reference to B1 (a B1&) and intialise that to the D1 object you just defined. Call vf() and f() for that reference.

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


Chapter 14 // Drill 4

Now define a function called f() for D1 and repeat 1-3. Explain the results.

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


Chapter 14 // Drill 5

Add a pure virtual function called pvf() to B1 and try to repeat 1-4. Explain the result.

Github: N/A

The compiler will fail to build on this unless you delete the B1 object creation. If any functions in a class are pure virtual then the class cannot be an object by itself. 

Chapter 14 // Drill 6

Define a class D2 derived from D1 and override pvf() in D2. Make an object of class D2 and invoke f(), vf(), and pvf() for it.

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

Chapter 14 // Drill 7

Define a class B2 with a pure virtual function pvf(). Define a class D21 with a string data member and a function that overrides pvf(); D21::pvf() should output the value of the string. Define a class D22 that is just like D21 except that its data member is an int. Define a function f() that takes a B2& argument and calls pvf() for its argument. Call f() with a D21 and a D22.

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

The wording of this drill reminded me of this scene from friends: