C++ Notes
+ +ToC
+-
+
- Basics +
- Introduction +
- Variables +
- Operators +
- Control Flow +
-
+
+
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:
+
+ The int main() {
+ std::cout << "Hello world" << '\n';
+ return 0;
+ }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:
+
-
+
- Cannot start with a number. +
- Can only contain alphanumeric characters and underscores. +
- Cannot contain any reserved keywords (see here). +
- If starting with an underscore, it can only start with one. +
+
+ Variable types: +
-
+
- Integers: +
- short int: +
- Size: 2 bytes. +
- Range: -32,768 to 32,767 +
- int: +
- Size: 4 bytes. +
- Range: -2 billion to 2 billion +
- short: +
- Size: 8 bytes. +
- Floats: +
- float: +
- Size: 4 bytes. +
- 6 significant figures. +
- double: +
- Size: 8 bytes. +
- 15 significant figures. +
- long double: +
- Size: 16 bytes. +
- 19 significant figures. +
- Character: +
- Type: char + Stores an integer value representative of an "ASCII" character. +
- Size: 1 byte. +
- String: +
- Usage:
#include+ - Variable creation:
std::string identifier = "string literal";
+ - Boolean: +
- Type: bool +
- Size: 1 byte +
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+ Stores an indexable array of characters.
+
-
+ Stores an integer value of 0 or 1 representative of false or true respectively.
+
+ 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.
+