Skip to main content
eLearner.app

C++ Course

Cheatsheet

A quick reference — the essential syntax of modern C++ on a single page. Press Ctrl/Cmd + P to print it.

C++ · Cheatsheet — eLearner.app

Variables and basic types

  • Declarations

    int x = 42;             // Integer
    double pi = 3.14159;    // Floating point
    char grade = 'A';       // Single character (single quotes)
    bool isFun = true;      // Boolean (true/false)
    const int Max = 100;    // Constant
    auto inferred = 5.5;    // Automatic type deduction

    Every statement in C++ must end with a semicolon (;).

  • Standard strings

    #include <string>
    
    std::string name = "Alice";
    std::string greet = "Hello " + name;
    int len = greet.length();  // or greet.size()

    Always use double quotes for strings and include <string>.

Control Flow

  • Conditionals (if / else)

    if (score >= 90) {
        std::cout << "Excellent!";
    } else if (score >= 60) {
        std::cout << "Passed";
    } else {
        std::cout << "Failed";
    }
  • Loops (for / while)

    // Classic for loop
    for (int i = 0; i < 5; ++i) {
        std::cout << i << " ";
    }
    
    // While loop
    while (count > 0) {
        count--;
    }
  • Switch statement

    switch (op) {
        case '+':
            res = a + b;
            break;
        case '-':
            res = a - b;
            break;
        default:
            res = 0;
    }

    Do not forget the break; statement, otherwise execution falls through to next cases.

Functions and References

  • Basic definition

    int sum(int a, int b) {
        return a + b;
    }
    
    void greet() {
        std::cout << "Hello!";
    }

    void functions do not return any value.

  • Pass by Reference (&)

    // Modifies the original variable directly
    void doubleIt(int& n) {
        n = n * 2;
    }
    
    // Pass by const reference (efficient, read-only)
    void showName(const std::string& s) {
        std::cout << s;
    }

    Use const references to pass large objects and avoid expensive copying.

Classes and OOP

  • Class declaration

    class Rectangle {
    private:
        int width;
        int height;
    
    public:
        // Constructor
        Rectangle(int w, int h) : width(w), height(h) {}
    
        int getArea() {
            return width * height;
        }
    };

    Note the mandatory semicolon (;) after the closing brace of the class.

  • Object usage

    Rectangle rect(5, 4);       // Instantiation
    int area = rect.getArea();  // Method call: 20

Input / Output Stream

  • <iostream> library

    #include <iostream>
    
    // Writing to console
    std::cout << "Value: " << x << std::endl;
    
    // Reading from console
    std::cin >> x;
    
    // boolalpha to print true/false instead of 1/0
    std::cout << std::boolalpha << true;

    std::endl inserts a new line and flushes the output buffer.

eLearner.app · C++ Course · cheatsheet generated from lesson content.