Getting Started

Namespaces

#include <iostream>
namespace ns1 {int val(){return 5;}}
int main()
{
    std::cout << ns1::val();
}

#include <iostream>
namespace ns1 {int val(){return 5;}}
using namespace ns1;
using namespace std;
int main()
{
    cout << val(); 
}

Namespaces allow global identifiers under a name

References

int i = 1;
int& ri = i; // ri is a reference to i
ri = 2; // i is now changed to 2
std::cout << "i=" << i;
i = 3;   // i is now changed to 3
std::cout << "ri=" << ri;

ri and i refer to the same memory location.

Functions

#include <iostream>

void hello(); // Declaring

int main() {  // main function
    hello();    // Calling
}

void hello() { // Defining
    std::cout << "Hello QuickRef!\n";
}

See: Functions

Loops

for (int i = 0; i < 10; i++) {
    std::cout << i << "\n";
}

See: Loops

If statement

if (a == 10) {
    // do something
}

See: Conditionals

Comments

// A single one line comment in C++
/\* This is a multiple line comment
 in C++ \*/

Swap

int a = 5, b = 10;
std::swap(a, b);
// Outputs: a=10, b=5
std::cout << "a=" << a << ", b=" << b;

User Input

int num;
std::cout << "Type a number: ";
std::cin >> num;
std::cout << "You entered " << num;

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

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;

int age {25};         // Since C++11
std::cout << age;     // Print 25

hello.cpp

#include <iostream>
int main() {
    std::cout << "Hello QuickRef\n";
    return 0;
}

Compiling and running

$ g++ hello.cpp -o hello
$ ./hello
Hello QuickRef
Comments