Basics

Hello WorldBasic C++ program
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}
Data typesPrimitive types
int x = 10;
float f = 3.14f;
double d = 3.14159;
char c = 'A';
bool b = true;
Input/OutputConsole I/O
int age;
cout << "Enter age: ";
cin >> age;
cout << "Age: " << age << endl;
ConstantsImmutable values
const int MAX = 100;
const double PI = 3.14159;

Control Flow

If-elseConditional execution
if (x > 0) {
    cout << "positive";
} else {
    cout << "not positive";
}
For loopIteration
for (int i = 0; i < 10; i++) {
    cout << i << endl;
}
Range-based forModern iteration (C++11)
vector<int> nums = {1, 2, 3};
for (int n : nums) {
    cout << n << endl;
}

OOP

ClassBlueprint for objects
class Car {
private:
    string model;
public:
    Car(string m) : model(m) {}
    void drive() {
        cout << model << " is driving\n";
    }
};
InheritanceDerived classes
class ElectricCar : public Car {
public:
    ElectricCar(string m) : Car(m) {}
};
Virtual functionPolymorphism
class Shape {
public:
    virtual double area() = 0;
};

STL

VectorDynamic array
#include <vector>
vector<int> nums = {1, 2, 3};
nums.push_back(4);
nums.size();
MapKey-value pairs
#include <map>
map<string, int> ages;
ages["Mishu"] = 25;
StringString class
#include <string>
string s = "hello";
s.length();
s.substr(0, 2);
SortSorting algorithm
#include <algorithm>
sort(nums.begin(), nums.end());