Lucas Rufkahr

C++ Notes

ToC


Basics

I. Introduction

A C++ program generally consists of preprocessor directives and the main function.

A preprocessor directive tells the C++ preprocessor what to do before compiling. You can use this to include files, create macros, and determine compiling based on conditions. For example, include a file using #include This will include the iostream file from the C++ standard library.

The main function looks like:

int main() {
	std::cout << "Hello world" << '\n';
	return 0;
}
The int main() { is the declaration of the main function denoting it returns type int. return 0; is the return statement for the function. It is returning the integer 0. This tells the operating system, OK Quit the program safely.

II. Variables

A variable lets you allocate some memory in the computer and use it to store values. You can also recall the values for later. Variables contain a memory address and an identifier. An identifier can be *almost* anything you wish it to be.

Variables naming rules:

All variables have denoted types and they refer to what kind of variable it is. All variables also have a bit size and this denotes the range of values that can be stored.

Variable types:
Variables are always declared before they are initialized. This means you must determine the type and the identifier before you store any values into it.

int main() {
		int a; // Declaration
		a = 1; // Initialization
		int b = 2; // Declaration then initialization
	}


Constant types are variables identified by const in their type definition. These are not allowed to be modified after they are declared and must be initialized at declaration.

III. Operators

Just like in mathematics, operators let you perform functions on elements. We'll start by viewing arithmetic operators and assignment.

The assignment operator, denoted by = allows you to assign values to a variable. Assigning a variable with a value of a different type will give you a warning. This can be solved by casting the value being assigned to the same type as your variable: static_cast(var). Assignment also works from right to left. Values on the right of the assignment operator get assigned to the identifier on the left of the assignment operator.

Arithmetic operators allow you to do operations such as addition, subtraction, multiplication, division, and remainder division.

When dividing with numbers, it is important to know the type of numbers you are dividing. Division between integers will only return a whole number. For example 5 / 2 = 2. Suppose the 5 was a floating point type then, 5.0 / 2 = 2.5. It is important to know what types you are using when performing division.

When wanting to perform division and only return the remainder, you can use the modulus operator. For example, 5 / 2 = 2 whereas 5 % 2 = 1. 2 is the quotient and 1 is the remainder.

There are also ways to quickly perform arithmetic operations using "fast operators". Next we'll approach the relational and logical operators. These allow you to evaluate comparisons between two or more variables.

Relational Operators Logical Operators When using multiple operators, it is important to know that operators have a precedence, just like in mathematics.

Operators with the same precedence are evaluated in the statement from left to right.
1st Last
(a) x++, x++ ++x, --x ! * / % + - < > <= >= == != && || =

Control Flow

I. Decision Branching

An if statement will execute instructions if the condition evaluates to true. You can remember this as if something is true, then my program will do this. For example:

int main() {
	int x = 1;
	if (x == 1) {
		std::cout << x << " is equal to 1.\n";
	}
	return 0;
}
will execute "1 is equal to 1".

An if-else statement will execute instructions if the condition is true and execute different instructions when the condition is false.

int main() {
	int x = 1;
	if (x == 1) {
		std::cout << x << " is equal to 1.\n";
	}
	else {
		std::cout << x << " is not equal to 1.\n";
	}
	return 0;
}


One thing to note about these types of control statements, is that they can be nested to create a decision branch. A branch, like of a tree, splits off into many smaller branches. The more nested if-else statements you have, the more and more branches you add to your decision tree.

II. Loops