Codeloft
0 0 XP

C++ reference

Short entries with an example for each. When you need every detail, each entry links to the official documentation.

C++

std::cout

Writes text and values to the console.

std::cout << "Score: " << score << std::endl;

Variables and types

Every variable has a type: int, double, bool, std::string and more.

int age = 12;
double price = 4.99;
bool ready = true;
std::string name = "Ada";

if / else

Runs code when a condition is true, and other code when it isn't.

if (marks >= 40) {
    std::cout << "Pass";
} else {
    std::cout << "Try again";
}

for loop

Repeats a block a set number of times.

for (int i = 0; i < 3; i++) {
    std::cout << i;
}

Functions

Named, reusable code with a return type and parameters. Define them above main.

int square(int n) {
    return n * n;
}

std::vector

A list that can grow. push_back adds; size counts.

std::vector<int> v = {1, 2};
v.push_back(3);
std::cout << v.size();

class

A blueprint that bundles data and methods. Ends with };

class Dog {
public:
    std::string name;
    void bark() { std::cout << "Woof"; }
};

std::cin

Reads input typed by the user.

int age;
std::cin >> age;