The C++ Type System
- C++ is an object-oriented language.
- Objects have state and behavior - Think of a light switch for example.
- You can describe the STATE as the condition the switch is in - on or off.
- You can also describe the BEHAVIOR of the switch - does it move from one state (on) to another (off) or is it a dimmer with many different states between the two?
- C++ is STRONGLY TYPED - which means each object has a predetermined data type.
Variables
- If you name an object, it’s called a variable.
- You declare variables by providing their type, their name and then a semicolon;
- You initialize variables by declaring them.
- Object intialization establishes an object’s state.
- In C++, you want to use Uniform initialization like int num1 {42};
- You can assign variables equal to the result of math expressions like int lucky_number = num1 / 6
Conditional Statements
- Conditional statements allow you to make decisions.
- They rest on Boolean expressions - they evaluate to True or False.
- You can build Boolean expressions with comparison operators (== , !=, >, <, >=, <=)
- You write interesting programs with this logic using condition statements like if or while.
- In if statements, if the Boolean evaluates to true, then the nested statement executes, otherwise it doesn’t.
- You can nest multiple statements together to execute, you call this a COMPOUND STATEMENT.
Printf format specifiers (a list)
- Here’s the short list you actually need in 95% of cases - NOT EXHAUSTIVE:
- Integers %d → signed int %u → unsigned int %ld → signed long %lu → unsigned long %lld → signed long long %llu → unsigned long long
- Floating Point %f → float or double %lf → technically for double, but in printf %f already expects a double Important nuance: When you pass a float to printf, it is automatically promoted to double.
- Characters & Strings %c → char %s → const char*
- Pointers %p → pointer (e.g., void*)
- Size_t %zu → size_t
Working examples
These are rendered from the source stored beside this drawer's notes.
variables.cpp
raw source ↗#include <cstdio>
// Type exploration
// This function doesn't necessarily need to return an int - sizeof() returns a specific number of format size_t
void printsizeoftype(){
printf("Size of int: %zu\n", sizeof(int)); // %zu is the specific format for interpolating size_t values into strings that printf needs
printf("Size of long: %zu\n", sizeof(long));
printf("Size of long long: %zu\n", sizeof(long long));
printf("Size of float: %zu\n", sizeof(float));
printf("Size of double: %zu\n", sizeof(double));
printf("Size of char: %zu\n", sizeof(char));
printf("Size of bool: %zu\n", sizeof(bool));
printf("Size of size_t: %zu\n", sizeof(size_t));
printf("Size of void*: %zu\n", sizeof(void*));
return; //void functions don't need the exit code.
}
// Declaring variables
// C uses the traditional way of initializing a variable, often with parentheses ()
int num1 = 42;
char ch1 = 'A';
// C++ Uniform Initialization utilizes a more modern version using curly braces {}
// Value of the variable determined by enclosed expressions.
// Generally recommended for new code because it stops it being confused with assignment operations.
// It also stops narrowing conversions happening which helps in engine-level code. Forces you to be explicit!
int num2 {42};
char ch2 {'A'};
//uninitialized variable - intialization establishes an object's state, with their detault value.
int num3;
char ch3;
//zero-initialized variable - kinda self-explanatory - variable initialized with a value of 0.
int num4 {0};
double price1 = 0.0;
int main(){
printsizeoftype(); // Calling declared function above. No argument needed.
return 0;
}