
C++ Notes
iostream
The library provides objects which can read user input and output data to the console or to a file.
fstream
The library provides classes for reading and writing into files or data streams.
cmath
The library has many functions that allow you to perform mathematical tasks on numbers.
string
The library has many functions that allow you to perform tasks on strings.
cstring
The library has many functions that allow you to perform tasks on arrays and C-style strings.
Note that C-style strings are different than regular strings. A C-style string is an array of characters, created with the char type.
ctime
The library has a variety of functions that allow you to measure dates and times.
Types
Switch Statement
Loops
Arrays
Structs
Similar to an object, these can be used when oop is not needed for a simple data object.
Casting Struct to a Function
If you are trying to modify the original values of the struct you passed into the function, you must also use the address-of operator &.
Enums
Enumerations are lists of data you can use to save memory space, and organize data with names.
Pointers
A pointer is a variable that stores the MEMORY ADDRESS of another variable. This allows you to directly manipulate memory and work with data directly.
Analogy: Instead of going door-to-door with 20 free pizzas, its easier to just tell people where the free pizza is.
Pointers are often used in game dev when working with memory, large objects or interacting with graphics API’s.
When to use a pointer?
When you need to allocate memory dynamically. Example: Creating objects, or arrays whose sizes are determined at runtime.
Dynamic Memory
Memory that is allocated after the program is already compiled and running. Use the ’new’ operator to allocate memory in the heap rather than the stack.
useful when we don’t know how much memory we will need. Makes our programs much more flexible especially when accepting user input.
For example:
Pointers with Arrays
When working with pointers and arrays, its good to be cognitive that using the address-of operator on an array will return its memory address!
So you can just pass the array to pointer, with the address-of operator.
Null Pointers
A special value that means something has no value. When a pointer is holding a null value that pointer is not pointing at anything (null pointer). Null pointers are helpful when determining if an address was successfully assigned to a pointer.
Lets show some example of null pointers below. You usually use these when defining pointer addresses that don’t yet have data to be stored in it.
References
A reference is an alias to another variable, meaning it refers to an existing variable.
Once a reference is initialized to a variable, it cannot be changed to another variable. References are generally easier and safer to use than pointers because they are guaranteed to point to valid data (not null). References will also automatically update themselves whenever the variable they are referencing changes.
When to use a reference to a variable?
Example: when passing objects like Player or Enemy to a function that will manipulate them, it’s often better to use references because the object is already created and guaranteed to exist. References make it more readable and don’t require pointer syntax (->)
Passing by Value vs Reference
Next we have to discuss passing by reference when creating functions. Check out the following code which initializes some variables and prints their data
All seems normal correct? It is printing the variables, now lets try making a function that requires x and y.
Okay cool, all is still well now lets try invoking this function and see what happens :)
So what happened, is when you passed x and y into the swap function, it merely made copies of those variables and it didn’t actually modify the original variables at all. This is a quirk of c++ you will have to get used to.
If you need to change the original values, you need to pass the variables by reference.
Example:
Now when we will call swap with x and y, it will update the original variables values and not the copies.
Memory Address
A location in memory where data is stored. Memory addresses can be accessed with & (address-of operator).
Function Templates
Describes what the function is. Can be used to generate many overloaded functions, each using different data types.
For example, say you have a max function that has 3 overloaded versions to accept different types.
So we have 3 functions, and they all have the exact same functionality, the only difference is the typing, so in an instance such as this you can define and use the T operator, which will allow you to pass any type to the function!
Imagine T represents “thing” because we aren’t really sure what the data type is!
Objects & Classes
A collection of attributes, methods, and variables. Examples are: Car, Player, Enemy, Phone, etc. created from a class which is the objects “blueprint”.
First lets define a class!
Class Constructors
Here’s how to make a constructor for a class.
Overloaded Constructors
These are good for when you have items that have multiple possible constructors to choose from.
Abstraction & Getters/Setters
Imagine, you are coding a Stove, and someone hacks into it and changes all the public variables we declared up there, and sets the furnace to 100000 degrees to destroy everything. 🔥
That would suck.
So we should use private variables and use getters and setters for better protection over our variables. And then use getters and setters that will allow these variables to be read / modified.
Inheritence
A class inherit the attributes and methods of another class. Classes can be children of parent classes.
Headers
Classes, like shown above can get a little messy so for better organization of concerns we should instead use headers with our classes. Let’s use the above Animal class, and make a Header file for it.
First create a animal.h file for the header, and animal.cpp for the class aka the implementation file.
Use #include “name” with quotes instead of brackets for self declared header include statements!