Files
website/notes/cpp.html
2026-02-23 08:02:31 -06:00

155 lines
4.8 KiB
HTML

<!DOCTYPE html>
<head>
<style>
body {
margin:0;
padding:0;
}
main {
margin:2em;
}
header {
display:block;
width: 100%;
background-color:black;
padding:1em;
}
header * {
color:white;
display:inline;
padding-left:1em;
}
code, pre {
whitespace: normal;
}
.cblock {
padding-left: 1em;
padding-right: 1em;
display: inline-block;
margin-left: 5em;
background-color:lightgray;
}
</style>
</head>
<body>
<header>
<span style="font-size: 32px;">Lucas Rufkahr</span>
<a href="https://lukerufkahr.com">Home</a>
<a href="https://lukerufkahr.com/notes.html">Notes</a>
<a href="https://lukerufkahr.com/fun_stuff.html">Fun</a>
</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">Operators</h3>
<h2 id="control_flow">Control Flow</h2>
</main>
</body>