C++ Cheatsheet

Variables Data Types Input/Output Strings Numbers Arrays Vectors Maps Sets Pointers References Control Flow Functions Classes Inheritance Polymorphism Abstract Virtual Templates Exceptions File I/O STL Lambdas Namespaces Main Function

Variables

Variables
int x = 5;
double d = 3.14;
bool flag = true;
char c = 'A';
std::string s = "hello";

Data Types

Types
int a = 1;        // Integer
float b = 2.3f;   // Floating point
double c = 3.14;  // Double precision
char d = 'A';     // Character
bool e = true;    // Boolean
std::string s = "hi"; // String (C++11+)

Input / Output

I/O
#include 
int x;
std::cin >> x;
std::cout << "Value: " << x << std::endl;

Strings

String
#include 
std::string s = "hello";
s.length();
s[0] = 'H';
s.substr(1, 2);
s.find("ll");
s += " world";

Numbers & Math

Math
#include 
std::pow(2, 3);   // 8
std::sqrt(16);    // 4
std::abs(-5);     // 5
std::max(1, 2);   // 2
std::min(1, 2);   // 1

Arrays

Array
int arr[3] = {1, 2, 3};
arr[0] = 10;
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++) {
  std::cout << arr[i] << std::endl;
}

Vectors

STL
#include 
std::vector v = {1, 2, 3};
v.push_back(4);
v.size();
v[0] = 10;
for (int x : v) {
  std::cout << x << std::endl;
}

Maps

STL
#include 
std::map m;
m["a"] = 1;
m["b"] = 2;
for (auto& p : m) {
  std::cout << p.first << ": " << p.second << std::endl;
}

Sets

STL
#include 
std::set s;
s.insert(1);
s.insert(2);
s.count(1); // 1 if present, 0 if not
s.erase(2);
for (int x : s) {
  std::cout << x << std::endl;
}

Pointers

Pointer
int x = 5;
int* p = &x;
*p = 10;
std::cout << *p << std::endl; // 10

References

Reference
int x = 5;
int& ref = x;
ref = 10;
std::cout << x << std::endl; // 10

Control Flow

Flow
int x = 5;
if (x > 0) {
  std::cout << "Positive" << std::endl;
} else if (x < 0) {
  std::cout << "Negative" << std::endl;
} else {
  std::cout << "Zero" << std::endl;
}

int y = 10;
switch (y) {
  case 1:
    std::cout << "One" << std::endl;
    break;
  case 2:
    std::cout << "Two" << std::endl;
    break;
  default:
    std::cout << "Other" << std::endl;
    break;
}

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

int j = 0;
while (j < 3) {
  std::cout << j << std::endl;
  j++;
}

do {
  std::cout << j << std::endl;
  j--;
} while (j > 0);

Functions

Function
int add(int a, int b) {
  return a + b;
}

int add(int a, int b, int c) {
  return a + b + c;
}

int add(double a, double b) {
  return static_cast(a + b);
}

int add(int a = 0, int b = 0) {
  return a + b;
}

void printMessage() {
  std::cout << "Hello, World!" << std::endl;
}

Classes

Class
class MyClass {
public:
  int x;
  MyClass() : x(0) {}
  void print() {
    std::cout << "x = " << x << std::endl;
  }
};

MyClass obj;
obj.x = 10;
obj.print();

Inheritance

Inheritance
class Base {
public:
  int x;
  Base() : x(0) {}
  void print() {
    std::cout << "Base x = " << x << std::endl;
  }
};

class Derived : public Base {
public:
  int y;
  Derived() : y(0) {}
  void print() {
    std::cout << "Derived x = " << x << ", y = " << y << std::endl;
  }
};

Base baseObj;
baseObj.x = 10;
baseObj.print();

Derived derivedObj;
derivedObj.x = 20;
derivedObj.y = 30;
derivedObj.print();

Polymorphism

Polymorphism
class Animal {
public:
  virtual void makeSound() {
    std::cout << "Animal makes a sound" << std::endl;
  }
};

class Dog : public Animal {
public:
  void makeSound() override {
    std::cout << "Dog barks" << std::endl;
  }
};

Animal* animal = new Animal();
animal->makeSound();

Dog* dog = new Dog();
dog->makeSound();

Abstract

Abstract
class Shape {
public:
  virtual double area() = 0; // Pure virtual function
};

class Circle : public Shape {
public:
  double radius;
  Circle(double r) : radius(r) {}
  double area() override {
    return 3.14 * radius * radius;
  }
};

Shape* shape = new Circle(5.0);
std::cout << "Circle area: " << shape->area() << std::endl;

Virtual

Virtual
class Animal {
public:
  virtual void makeSound() {
    std::cout << "Animal makes a sound" << std::endl;
  }
};

class Dog : public Animal {
public:
  void makeSound() override {
    std::cout << "Dog barks" << std::endl;
  }
};

Animal* animal = new Animal();
animal->makeSound();

Dog* dog = new Dog();
dog->makeSound();

Polymorphism

OOP
class Base {
public:
  virtual void speak() { std::cout << "Base" << std::endl; }
};
class Derived : public Base {
public:
  void speak() override { std::cout << "Derived" << std::endl; }
};
Base* obj = new Derived();
obj->speak(); // Derived

Abstract Classes

OOP
class Shape {
public:
  virtual double area() = 0; // Pure virtual
};
class Circle : public Shape {
  double r;
public:
  Circle(double r) : r(r) {}
  double area() override { return 3.14 * r * r; }
};

Virtual Functions

OOP
class Animal {
public:
  virtual void speak() { std::cout << "Animal" << std::endl; }
};
class Dog : public Animal {
public:
  void speak() override { std::cout << "Woof" << std::endl; }
};
Animal* a = new Dog();
a->speak(); // Woof

Templates

Generics
template 
T add(T a, T b) {
  return a + b;
}
int x = add(1, 2);
double y = add(1.1, 2.2);

Exception Handling

Error
try {
  throw std::runtime_error("Error!");
} catch (const std::exception& e) {
  std::cout << e.what() << std::endl;
}

File I/O

I/O
#include 
std::ofstream out("file.txt");
out << "Hello" << std::endl;
out.close();
std::ifstream in("file.txt");
std::string line;
while (std::getline(in, line)) {
  std::cout << line << std::endl;
}
in.close();

STL (Standard Template Library)

STL
#include 
#include 
#include 
#include 
std::vector v = {3, 1, 2};
std::sort(v.begin(), v.end());
std::reverse(v.begin(), v.end());
std::map m;
std::set s;

Lambda Functions

C++11+
auto add = [](int a, int b) { return a + b; };
int sum = add(2, 3); // 5

Namespaces

Syntax
namespace myns {
  int x = 5;
}
int y = myns::x;

Main Function

Entry
int main() {
  std::cout << "Hello, C++!" << std::endl;
  return 0;
}