Thursday, 13 August 2020

Chapter 15 // Class Definition Drills 1, 2, 3, 4, 5, 6, 7 - Principles & Practice Using C++

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

Chapter 15 // Class Definition Drills

1. Define a struct Person containing a string name and an int age.
2. Define a variable of type Person , initialise it with "Goofy" and 63, and write it to the screen (cout).
3. Define an input (>>) and an output (<<) operator for Person; read in a Person from the keyboard (cin) and write it out to the screen (cout).
4. Give Person a constructor initialising name and age.
5. Make the representation of Person private, and provide const member functions name() and age() to read the name and age.
6. Modify >> and << to work with the redefined Person.
7. Modify the constructor to check that age is [0:150] and that name doesn't contain any of the characters ; : " ' [] * & ^ % $ # @ ! . Use error()  in case of error. Test.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2015/Class%20Definition%20Drills/Drills1-7

Wednesday, 12 August 2020

Chapter 15 // Shape Function Graphing Drills 1, 2, 3, 4, 5, 6, 7, 8, 9 - 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 15 // Function Graphing Drills

1. Graph the function  double one(double x) {return 1;} in the range [-10,11] with (0,0) at (300,300) using 400 points and no scaling (in the window).
2. Change it to use x scale 20 and y scale 20.
3. From now on use that range, scale, etc. for all graphs.
4. Add double slope(double x) {return x/2;} to the window.
5. Label the slope with a Text "x/2" at a point just above its bottom left end point.
6. Add double square(double x) {return x*x;} to the window.
7. Add a cosine to the window (don't write a new function).
8. Make the cosine blue.
9. Write a function sloping_cos() that adds a cosine to slope() (as defined above) and add it to the window.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2015/Function%20Graphing%20Drills%202


All of these functions can be found from section 15.2 to 15.3.2. If you have already followed along with the chapter you will have done them.

Drill 2-1:
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Drill 2-2:
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Drill 2-3, 2-4:
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Drill 2-5:
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Drill 2-6:
It annoys me that this one doesn't scale
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Drill 2-7, 2-8:
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Drill 2-9:
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Friday, 7 August 2020

Chapter 15 // Function Graphing Drills 1, 2, 3, 4, 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 15 // Function Graphing Drills

1. Make an empty 600-by-600 Window labeled "Function Graphs".
2. Note that you'll need to make a project with the properties specified in the "installation of FLTK" note from the course website.
3. You'll need to move Graph.cpp and Window.cpp into your project.
4. Add an x axis and a y axis each of length 400, labeled "1 == 20 pixels" and with a notch ever 20 pixels. The axes should cross at (300,300).
5. Make both axes red.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2015/Function%20Graphing%20Drills


I am lazy and I've been using the same project since the start of Chapter 12.




Wednesday, 5 August 2020

Notes on Chapter 15 // Principles & Practice using C++

So I've been reading though this chapter and got stuck on section 15.5 where he uses a lambda expression to capture some data. The code can be seen on page 533. Here he uses Function to graph another function however I got the following error when entering the code myself:

error C2440: 'initializing': cannot convert from 'initializer list' to 'Graph_lib::Function'

I triple checked the code in the book and my code and there was no difference. Unfortunately no one else seemed to have encountered this exact problem. I eventually fixed it with some guidance from this stackoverflow post: https://stackoverflow.com/questions/36030589/i-cannot-pass-lambda-as-stdfunction

In Graph.h, the struct Function has just one constructor, with the first argument being of the type Fct. Now Fct is a typedef of a function called Fct that takes in a double and returns a double. Unfortunately, Visual Studio did not like this and would not convert the lambda expression to fit. In order to get round this, I created a new constructor that instead takes in a std::function called f that takes in a double and returns a double:


Visual Studio was happy with this and built the solution.

Fixing Window label bug
EDIT 18/08/2020 - Please read the edit down below.

I also finally fixed the weird thing that was going on with the window label. After you click next it always disappeared for me or started displaying garbage. I looked into this and found that FLTK eventually relies on this constructor for label:

void Fl_Window::label(const char *name) {
  label(name, iconlabel()); // platform dependent
}

When it calls iconlabel() it goes here:

  /**  See void Fl_Window::iconlabel(const char*)   */
  const char* iconlabel() const {return iconlabel_;}

iconlabel_ is null. This is in Fl_Window.H which I am reluctant to modify. It appears that in our Window.h, Window is calling the constructor for label and passing a char array to it. However it is not setting up the icon label.

void set_label(const string& s) { label(s.c_str()); }


iconlabel_ is set to 0 in the constructor for Fl_Window(). To fix this, it is a one line change in Window.h:

void set_label(const string& s) { label(s.c_str()); show(); }

After trying to set iconlabel_ manually it took me to another function which checks to see if the window is to be shown. It returned false. show() is a function set in GUI.h and is only called once upon initialisation of the window (which is why the label correctly displays upon first viewing). The problem is that when the label is changed we need to call show() again otherwise iconlabel_ will not be updated.

EDIT 18/08/2020 - Nothing can ever be simple. Whilst doing exercise 2 of Chapter 15, I noticed the label would go missing again if the next button was pressed to be replaced with garbage. I had a look and even just after the window has been created it's showing the title label as null. I tried setting the label in the window init() function, changing the label in the Fl_wait loop but the label was always null.

I then noticed that Fl_Window wants a const char* in it's constructor:
In C, you can use char* as strings. char* will always point to the first character in the string. Our Window and Simple_window uses a string& and then converts that to a char* using the .c_str() function. This should work just fine but in case there was something being lost in translation, I changed all the string& references to const char*. Now, it's displaying labels correctly and storing them in Fl_window. You don't need to call show() in set_label either as I changed that to take in a char* as well.

Fixing Default Line Colour

Another thing that was annoying me was that the default line colour was a light grey; this was impossible to see on the background and I was sick of always having to set the colour for everything. So I just set the default line colour to black in the constructor for Shape:
Shape() { set_color(Color::black); }

Tuesday, 4 August 2020

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

The exceptions defined in the C++ standard library, such as exception, runtime_error, and out_of_range (section 5.6.3), are organised into a class hierarchy (with a useful virtual function what() returning a string supposedly explaining what went wrong). Search your information sources for the C++ standard exception class hierarchy and draw a class hierarchy diagram of it.

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


I used the hierarchy here: https://en.cppreference.com/w/cpp/error/exception

I procrastinated on this for two fucking months because I thought I would do it the proper way and make a full class that can draw any custom binary tree with fancy auto spacing...tonight I sat down and said to myself "life is too fucking short, just do it in the hackiest and shortest way possible you absolute fucking lemon". So that's what I did and I honestly don't care. It's done and I can move on.

Principles & Practice Using C++ Chapter 14 Exercise 17