This week I’ve worked through book 2 “Learn C++ for games development”. Part 1 goes through procedural programming; this involves using reusable patterns to build a working program. It started by covering variables, though I’ve learned this previously I was introduced to some new concepts. For example, I was made aware of how variables are stored in memory and the bytes each type takes. Below is a table from the book:
This covers types that store numbers (Strings become more complicated as the bytes they use varies depending on the length of the string).
I also discovered unsigned variables, I didn’t know these existed but I can see the usefulness of them. Basically an unsigned variable has an increased maximum value but no negatives (the minimum is always 0).
After covering other types, the book moved on to creating a simple guessing game. Here’s what it did:
- Random no. is generated
- Player guesses a no. between 0-100
- Prints the players number and the actual no.
I enjoyed this, especially since it introduced me to random numbers (Something needed often in games development). But the original program was simple so I extended it using what I’ve already learnt in last week’s lessons. I added continuous input so the player keeps guessing until they’re correct (using a while loop) & used if statements to print whether you guessed higher or lower than the actual number. Extending the project myself, without the aid of learning resources shows I’m getting somewhere with C++. I’m quite happy with the progress I’m making.
Next I went through chapter 4 covering arrays. This part’s vital as it also covered pointers which I was advised to understand during task 1. Pointers are difficult to explain; I’ll try making it simple by including a screenshot of an example the book used:
Here I’ll explain each line:
- Prints a variable (int) initialised with the value 5
- Prints the address of that variable (hexadecimal memory address, the place in memory where the variable’s stored)
- Prints a pointer (pointers store a variable with a value but it’s a memory address rather than the value itself)
- Dereferences pointer (used to display the actual value of the variable being pointed to)
Pointers seemed complicated to me at first (probably because they’re a completely new concept) but they’re quite simple. They were introduced here because arrays are actually pointers (I found this interesting).
If you’ve experienced other languages you know indexing is used with arrays, example:
C++ can use indexing, but you can also dereference the array itself, example:
This works because as mentioned arrays are pointers so printing the array itself (without dereferencing):
apologies for getting technical but it’s the easiest way to explain what I’ve learned.
To end week 2, I created a new project that will become a text adventure. This is extended throughout the other lessons in the book. Next week I will continue with this book and try to progress with this project.