Everything in one place
Use this chapter as a quick reference once you've worked through the rest of the course. It collects every core construct of C++ into runnable, copy-pasteable snippets you can scan whenever you forget the exact syntax.
Hello world & variables
#include <iostream>
int main() {
std::cout << "Hello, World!\n";
return 0;
}
int age = 25;
std::string name = "Aman";
bool isStudent = true;
std::cout << name << " " << age << "\n";Data types & operators
int n = 42;
double d = 3.14;
char c = 'A';
bool b = true;
std::string s = "hello";
std::vector<int> v = {1, 2, 3};
int a = 10, b = 3;
std::cout << a + b << " " << a % b << "\n";
std::cout << (a > b && b < 5) << "\n";Conditionals & loops
int score = 72;
if (score >= 90) std::cout << "A";
else if (score >= 60) std::cout << "Pass";
else std::cout << "Fail";
for (int i = 0; i < 5; i++) std::cout << i << "\n";
std::vector<int> v = {1,2,3};
for (int n : v) std::cout << n << "\n";Functions & arrays
int add(int a, int b) {
return a + b;
}
int main() {
std::cout << add(2, 3);
}
#include <vector>
std::vector<int> nums = {10, 20, 30};
nums.push_back(40);
for (int n : nums) std::cout << n << "\n";Strings & objects
std::string name = "PlayLearn";
std::cout << name.size() << "\n";
std::cout << name.substr(0, 4) << "\n";
class User {
public:
std::string name;
int age;
User(std::string n, int a) : name(n), age(a) {}
std::string greet() { return "Hi " + name; }
};
User u("Aman", 20);
std::cout << u.greet();Errors, modules & I/O
try {
throw std::runtime_error("Something broke");
} catch (const std::exception &e) {
std::cerr << e.what() << "\n";
}
// math.h
int add(int a, int b);
// math.cpp
int add(int a, int b) { return a + b; }
// main.cpp
#include "math.h"
int main() { return add(2, 3); }
std::string name;
std::cout << "Your name? ";
std::getline(std::cin, name);
std::cout << "Hello, " << name;How to use this page
Bookmark it. When you start a new project, open this cheatsheet alongside your editor and use it as a syntax lookup. Once a construct becomes second nature you'll stop needing the reference for it — and that's exactly when you know you've mastered that part of C++.
Practice and verify
Run reference snippets separately; they are not a single source file to concatenate. Check required imports and entry points for each.