283 lines
9.5 KiB
HTML
283 lines
9.5 KiB
HTML
<!DOCTYPE html>
|
|
<head>
|
|
<style>
|
|
body {
|
|
margin:0;
|
|
padding:0;
|
|
}
|
|
main {
|
|
margin:2em;
|
|
}
|
|
header {
|
|
width: 100%;
|
|
background-color:black;
|
|
padding:1em;
|
|
}
|
|
header * {
|
|
display: inline;
|
|
color:white;
|
|
}
|
|
|
|
nav {
|
|
display:block
|
|
}
|
|
nav * {
|
|
padding-right: 1em;
|
|
}
|
|
code, pre {
|
|
whitespace: normal;
|
|
}
|
|
.cblock {
|
|
padding-left: 1em;
|
|
padding-right: 1em;
|
|
display: inline-block;
|
|
margin-left: 5em;
|
|
background-color:lightgray;
|
|
}
|
|
table,td, th {
|
|
text-align:left;
|
|
width: 50em;
|
|
border: 1px solid black;
|
|
border-collapse: collapse;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<header>
|
|
<span style="font-size: 36px;">Lucas Rufkahr</span>
|
|
<nav>
|
|
<a href="https://lukerufkahr.com">Home</a>
|
|
<a href="https://lukerufkahr.com/notes/">Notes</a>
|
|
<a href="https://lukerufkahr.com/fun_stuff.html">Fun</a>
|
|
</nav>
|
|
</header>
|
|
<main>
|
|
<h1>C++ Notes</h1>
|
|
|
|
<h2>ToC</h2>
|
|
<ul>
|
|
<li><a href="#basics">Basics</a></li>
|
|
<ul>
|
|
<li><a href="#basics-introduction">Introduction</a></li>
|
|
<li><a href="#basics-variables">Variables</a>
|
|
<li><a href="#basics-operators">Operators</a>
|
|
</ul>
|
|
<li><a href="#control_flow">Control Flow</a></li>
|
|
</ul>
|
|
<hr>
|
|
<h2 id="basics">Basics</h2>
|
|
<h3 id="basics-introduction">I. Introduction</h3>
|
|
<p>
|
|
A C++ program generally consists of preprocessor directives and the main function.<br>
|
|
<br>
|
|
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 <code>#include <iostream></code> This will include the iostream file from the C++ standard library.<br>
|
|
<br>
|
|
The main function looks like:
|
|
<code class="cblock"><pre>int main() {
|
|
<!---------> std::cout << "Hello world" << '\n';
|
|
<!---------> return 0;
|
|
<!--------->}</pre></code>
|
|
The <code>int main() {</code> is the declaration of the main function denoting it returns type int. <code>return 0;</code> is the return statement for the function. It is returning the integer 0. This tells the operating system, OK Quit the program safely.
|
|
</p>
|
|
<h3 id="basics-variables">II. Variables</h3>
|
|
<p>
|
|
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.<br>
|
|
<br>
|
|
Variables naming rules:
|
|
<ul>
|
|
<li>Cannot start with a number.</li>
|
|
<li>Can only contain alphanumeric characters and underscores.</li>
|
|
<li>Cannot contain any reserved keywords <a href="https://en.cppreference.com/w/cpp/keyword.html">(see here)</a>.
|
|
<li>If starting with an underscore, it can only start with one.</li>
|
|
</ul>
|
|
|
|
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.<br>
|
|
<br>
|
|
Variable types:
|
|
<ul>
|
|
<li>Integers:</li>
|
|
<ul>
|
|
<li>short int:</li>
|
|
<ul>
|
|
<li>Size: 2 bytes.</li>
|
|
<li>Range: -32,768 to 32,767</li>
|
|
</ul>
|
|
<li>int:</li>
|
|
<ul>
|
|
<li>Size: 4 bytes.</li>
|
|
<li>Range: -2 billion to 2 billion</li>
|
|
</ul>
|
|
<li>short:</li>
|
|
<ul>
|
|
<li>Size: 8 bytes.</li>
|
|
</ul>
|
|
</ul>
|
|
<li>Floats:</li>
|
|
<ul>
|
|
<li>float:</li>
|
|
<ul>
|
|
<li>Size: 4 bytes.</li>
|
|
<li>6 significant figures.</li>
|
|
</ul>
|
|
<li>double:</li>
|
|
<ul>
|
|
<li>Size: 8 bytes.</li>
|
|
<li>15 significant figures.</li>
|
|
</ul>
|
|
<li>long double:</li>
|
|
<ul>
|
|
<li>Size: 16 bytes.</li>
|
|
<li>19 significant figures.</li>
|
|
</ul>
|
|
</ul>
|
|
<li>Character:</li>
|
|
<ul>
|
|
<li>Type: char</li>
|
|
Stores an integer value representative of an "ASCII" character.
|
|
<li>Size: 1 byte.</li>
|
|
</ul>
|
|
<li>String:</li>
|
|
<ul>
|
|
Stores an indexable array of characters.
|
|
<li>Usage: <code>#include <string></code>
|
|
<li>Variable creation: <code>std::string identifier = "string literal";</code></li>
|
|
</ul>
|
|
<li>Boolean:</li>
|
|
<ul>
|
|
Stores an integer value of 0 or 1 representative of false or true respectively.
|
|
<li>Type: bool</li>
|
|
<li>Size: 1 byte</li>
|
|
</ul>
|
|
</ul>
|
|
<br>
|
|
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.<br>
|
|
<br>
|
|
<code class="cblock"><pre>int main() {
|
|
<!-----> int a; // Declaration
|
|
<!-----> a = 1; // Initialization
|
|
<!-----> int b = 2; // Declaration then initialization
|
|
<!----->}</pre></code><br>
|
|
<br>
|
|
Constant types are variables identified by <code>const</code> in their type definition. These are not allowed to be modified after they are declared and must be initialized at declaration.
|
|
</p>
|
|
<h3 id="#basics-operators">III. Operators</h3>
|
|
<p>
|
|
Just like in mathematics, operators let you perform functions on elements. We'll start by viewing arithmetic operators and assignment.<br>
|
|
<br>
|
|
The assignment operator, denoted by <code>=</code> 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: <code>static_cast<type>(var)</code>. 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.<br>
|
|
<br>
|
|
Arithmetic operators allow you to do operations such as addition, subtraction, multiplication, division, and remainder division.<br>
|
|
<br>
|
|
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 <code>5 / 2 = 2</code>. Suppose the 5 was a floating point type then, <code>5.0 / 2 = 2.5</code>. It is important to know what types you are using when performing division.<br>
|
|
<br>
|
|
When wanting to perform division and only return the remainder, you can use the modulus operator. For example, <code>5 / 2 = 2</code> whereas <code>5 % 2 = 1</code>. 2 is the quotient and 1 is the remainder.<br>
|
|
<ul>
|
|
<li>+ Addition</li>
|
|
<li>- Subtraction</li>
|
|
<li>* Multiplication</li>
|
|
<li>/ Division</li>
|
|
<li>% Modulus</li>
|
|
</ul>
|
|
There are also ways to quickly perform arithmetic operations using "fast operators".
|
|
<ul>
|
|
<li>
|
|
Post increment/decrement:<br>
|
|
This will return the current value and then perform the operation.<br>
|
|
<code>x++</code><br>
|
|
<code>x--</code>
|
|
</li>
|
|
<li>
|
|
Pre increment/decrement:<br>
|
|
This will perform the operation, then return the current value.<br>
|
|
<code>++x</code><br>
|
|
<code>--x</code>
|
|
</li>
|
|
<br>
|
|
The following is short for <code>x = x (operation) y</code>:
|
|
<li><code>x += y</code></li>
|
|
<li><code>x -= y</code></li>
|
|
<li><code>x *= y</code></li>
|
|
<li><code>x /= y</code></li>
|
|
<li><code>x %= y</code></li>
|
|
</ul>
|
|
|
|
Next we'll approach the relational and logical operators. These allow you to evaluate comparisons between two or more variables.<br>
|
|
<br>
|
|
Relational Operators
|
|
<ul>
|
|
<li>Less than: <</li>
|
|
<li>Greater than: ></li>
|
|
<li>Less than or equal to: <=</li>
|
|
<li>Greater than or equal to: >=</li>
|
|
<li>Is equal to: ==</li>
|
|
<li>Is not equal to: !=</li>
|
|
</ul>
|
|
Logical Operators
|
|
<ul>
|
|
<li>AND: &&</li>
|
|
<li>OR: ||</li>
|
|
<li>NOT: !</li>
|
|
</ul>
|
|
When using multiple operators, it is important to know that operators have a precedence, just like in mathematics.<br>
|
|
<br>
|
|
Operators with the same precedence are evaluated in the statement from left to right.
|
|
<table>
|
|
|
|
<tr>
|
|
<th>1st</th>
|
|
<th></th>
|
|
<th></th>
|
|
<th></th>
|
|
<th></th>
|
|
<th></th>
|
|
<th></th>
|
|
<th></th>
|
|
<th></th>
|
|
<th></th>
|
|
<th>Last</th>
|
|
</tr>
|
|
<tr>
|
|
<td>(a)</td>
|
|
<td>x++, x++</td>
|
|
<td>++x, --x</td>
|
|
<td>!</td>
|
|
<td>* / %</td>
|
|
<td>+ -</td>
|
|
<td>< > <= >=</td>
|
|
<td>== !=</td>
|
|
<td>&&</td>
|
|
<td>||</td>
|
|
<td>=</td>
|
|
</tr>
|
|
</table>
|
|
</p>
|
|
<h2 id="control_flow">Control Flow</h2>
|
|
<h3 id="control_flow-branchinig">I. Decision Branching</h3>
|
|
<p>
|
|
An <code>if</code> 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:
|
|
<code class="cblock"><pre>int main() {
|
|
<!---------> int x = 1;
|
|
<!---------> if (x == 1) {
|
|
<!---------> std::cout << x << " is equal to 1.\n";
|
|
<!---------> }
|
|
<!---------> return 0;
|
|
<!--------->}</pre></code>
|
|
will execute "1 is equal to 1".<br>
|
|
<br>
|
|
An <code>if-else</code> statement will execute instructions if the condition is true and execute different instructions when the condition is false.<br><br>
|
|
<code class="cblock"><pre>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;
|
|
<!--------->}</pre></code><br><br>
|
|
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.
|
|
</p>
|
|
<h3 id="control_flow-loops">II. Loops</h3>
|
|
</main>
|
|
</body>
|