Full C++ Cheat Sheet
1. Basic Structure
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
2. Data Types
| Data Type | Description | Example |
|-----------|----------------------|---------------|
| int | Integer | int x = 5; |
| float | Floating-point | float y = 3.14;|
| double | Double precision | double z = 3.14159;|
| char | Character | char c = 'A'; |
| bool | Boolean | bool flag = true; |
| string | Text (include <string>) | string name = "Alice";
3. Functions
int add(int a, int b) {
return a + b;
int main() {
int result = add(5, 3);
cout << result;
return 0;
}
4. Object-Oriented Programming
class Person {
public:
string name;
int age;
void introduce() {
cout << "Name: " << name << ", Age: " << age;
};
int main() {
Person p1;
p1.name = "Alice";
p1.age = 25;
p1.introduce();
5. Inheritance
class Parent {
public:
void display() {
cout << "This is the parent class." << endl;
};
class Child : public Parent {
};
int main() {
Child obj;
obj.display(); // Calls the parent class method
}
6. Polymorphism
class Animal {
public:
virtual void sound() {
cout << "Some sound" << endl;
};
class Dog : public Animal {
public:
void sound() override {
cout << "Bark" << endl;
};
int main() {
Animal* animal = new Dog();
animal->sound(); // Outputs: Bark
7. Standard Template Library (STL)
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
vector<int> nums = {5, 1, 4, 2, 3};
sort(nums.begin(), nums.end());
for (int num : nums) {
cout << num << " ";
}
return 0;
8. Exception Handling
#include <iostream>
using namespace std;
int main() {
try {
int a = 5, b = 0;
if (b == 0) throw "Division by zero!";
cout << a / b;
} catch (const char* msg) {
cout << msg << endl;
return 0;
9. Memory Management
int* ptr = new int(5);
cout << *ptr; // Outputs: 5
delete ptr; // Free the allocated memory
10. Lambda Functions
auto add = [](int a, int b) { return a + b; };
cout << add(3, 4); // Outputs: 7
11. Multithreading
#include <iostream>
#include <thread>
using namespace std;
void printMessage(string message) {
cout << message << endl;
int main() {
thread t1(printMessage, "Hello from Thread 1!");
thread t2(printMessage, "Hello from Thread 2!");
t1.join();
t2.join();
return 0;