C++ Cheatsheet

1. Variables and Data Types

int age = 30;
bool isStudent = true;
char grade = 'A';
double pi = 3.14159;

std::string name = "John";

int numbers[] = {1, 2, 3};

2. Control Flow (Conditionals and Loops)

if (age > 18) {
  std::cout << "Adult";
} else {
  std::cout << "Minor";
}

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

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

3. Functions

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

// Function with default arguments
int multiply(int a, int b = 2) {
  return a * b;
}

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

4. Object-Oriented Programming (OOP)

class Animal {
  public:
    std::string name;
    Animal(std::string n) : name(n) {}
    void speak() { std::cout << name << " makes a sound."; }
};

class Dog : public Animal {
  public:
    Dog(std::string n) : Animal(n) {}
    void bark() { std::cout << name << " barks."; }
};

Dog rex("Rex");
rex.speak();
rex.bark();

5. Pointers and Memory Management

int x = 10;
int* ptr = &x;

std::cout << "Value of x: " << *ptr;

// Dynamic memory allocation
int* arr = new int[5];
arr[0] = 10;

// Free memory
delete[] arr;

6. Standard Template Library (STL)

#include <vector>
#include <map>

std::vector<int> numbers = {1, 2, 3};
numbers.push_back(4);

for (int num : numbers) {
  std::cout << num << std::endl;
}

std::map<std::string, int> ageMap;
ageMap["John"] = 30;
std::cout << ageMap["John"];

7. Exception Handling

try {
  int result = 10 / 0;
} catch (const std::exception& e) {
  std::cout << "Exception: " << e.what();
}
finally {
  std::cout << "Cleanup code";
}

8. Lambda Expressions

auto add = [](int a, int b) -> int {
  return a + b;
};

std::cout << add(5, 10);

9. File Handling

#include <fstream>

std::ofstream outfile("example.txt");
outfile << "Writing to file.";
outfile.close();

std::ifstream infile("example.txt");
std::string content;
while (std::getline(infile, content)) {
  std::cout << content << std::endl;
}

10. Multithreading

#include <thread>

void print_message() {
  std::cout << "Hello from thread";
}

std::thread t(print_message);
t.join();

11. Constructors and Destructors

class MyClass {
  public:
    MyClass() { std::cout << "Constructor called"; }
    ~MyClass() { std::cout << "Destructor called"; }
};

MyClass obj;

12. Namespaces

namespace myNamespace {
  void greet() {
    std::cout << "Hello from myNamespace!";
  }
}

myNamespace::greet();

13. Templates

template <typename T>
T add(T a, T b) {
  return a + b;
}

std::cout << add<int>(5, 10);
std::cout << add<double>(5.5, 10.2);

14. Smart Pointers

#include <memory>

std::unique_ptr<int> ptr = std::make_unique<int>(100);
std::cout << *ptr;

std::shared_ptr<int> sharedPtr = std::make_shared<int>(200);
std::cout << *sharedPtr;

15. Casting

int x = 10;
double y = static_cast<double>(x);

class Base { public: virtual void speak() {} };
class Derived : public Base {};

Base* basePtr = new Derived();
Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);

16. Enumerations

enum class Color { Red, Green, Blue };

Color myColor = Color::Green;

switch (myColor) {
  case Color::Red:
    std::cout << "Red";
    break;
  case Color::Green:
    std::cout << "Green";
    break;
  case Color::Blue:
    std::cout << "Blue";
    break;
}