C++

Getting Started

hello.cpp

#include <iostream>

int main() {
    std::cout << "Hello Vietelligent\n";
    return 0;
}

Compiling and running

$ g++ hello.cpp -o hello
$ ./hello
Hello Vietelligent

Variables

int number = 5;       // Integer
float f = 0.95;       // Floating number
double PI = 3.14159;  // Floating number
char yes = 'Y';       // Character
std::string s = "ME"; // String (text)
bool isRight = true;  // Boolean

// Constants
const float RATE = 0.8;

Primitive Data Types

Data Type
Size
Range

int

4 bytes

-231 to 231-1

float

4 bytes

N/A

double

8 bytes

N/A

char

1 byte

-128 to 127

bool

1 byte

true / false

void

N/A

N/A

wchar_t

2 or 4 bytes

1 wide character

User Input

Swap

Comments

If statement

See: Conditionals

Loops

See: Loops

Functions

See: Functions

References

ri and i refer to the same memory location.

Namespaces


Namespaces allow global identifiers under a name

C++ Arrays

Declaration

Manipulation


Displaying

Multidimensional


C++ Conditionals

If Clause


Else if Statement

Operators

Relational Operators

a == b

a is equal to b

a != b

a is NOT equal to b

a < b

a is less than b

a > b

a is greater b

a <= b

a is less than or equal to b

a >= b

a is greater or equal to b

Assignment Operators

a += b

Aka a = a + b

a -= b

Aka a = a - b

a *= b

Aka a = a * b

a /= b

Aka a = a / b

a %= b

Aka a = a % b

Logical Operators

exp1 && exp2

Both are true (AND)

exp1 || exp2

Either is true (OR)

!exp

exp is false (NOT)

Bitwise Operators

a & b

Binary AND

a | b

Binary OR

a ^ b

Binary XOR

~ a

Binary One's Complement

a << b

Binary Shift Left

a >> b

Binary Shift Right

Ternary Operator



Switch Statement

C++ Loops

While

Do-while

Continue statements

Infinite loop

for_each (Since C++11)

Range-based (Since C++11)


Break statements

Several variations

C++ Functions

Arguments & Returns

add is a function taking 2 ints and returning int

Overloading

Built-in Functions

C++ Classes & Objects

Defining a Class

Creating an Object

Constructors

Destructors

Class Methods

Access Modifiers

Getters and Setters

Inheritance

C++ Preprocessor

Preprocessor

Includes

Defines

If

Error

Macro

Token concat

Stringification

file and line

Miscellaneous

Escape Sequences

\b

Backspace

\f

Form feed

\n

Newline

\r

Return

\t

Horizontal tab

\v

Vertical tab

\\

Backslash

\'

Single quotation mark

\"

Double quotation mark

\?

Question mark

\0

Null Character

Last updated