C++ Cheat Sheet
1. Basics
#include<iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
2. Data Types & Variables
int a = 10;
float b = 5.5;
double c = 10.99;
bool d = true;
char e = 'A';
string f = "Hello";
3. Conditionals
if (a > b) {
cout << "A is greater";
} else {
cout << "B is greater";
}
4. Loops
for (int i = 0; i < 5; i++) {
cout << i << " ";
}
int j = 0;
while (j < 5) {
cout << j << " ";
j++;
}
5. Functions
int add(int x, int y) {
return x + y;
}
cout << add(5, 10);
6. Arrays & Vectors
int arr[] = {1, 2, 3, 4, 5};
vector<int> vec = {1, 2, 3, 4, 5};
vec.push_back(6);
7. Pointers
int x = 10;
int *ptr = &x;
cout << *ptr;
8. Classes & Objects
class Car {
public:
string brand;
Car(string b) { brand = b; }
};
Car myCar("Tesla");
cout << myCar.brand;
9. Inheritance
class Animal {
public:
void speak() { cout << "Animal speaks"; }
};
class Dog : public Animal {};
Dog d;
d.speak();
10. File Handling
#include<fstream>
ofstream myfile("test.txt");
myfile << "Hello File";
myfile.close();
Let me know if you need additional details!