Basic Structure
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl;
return 0;
}
Variables & Types
int age = 25;
float price = 9.99f;
double pi = 3.14159;
char grade = 'A';
string name = "John";
bool isTrue = true;
Input/Output
cout << "Hello " << name << endl;
int age;
cin >> age;
string fullName;
getline(cin, fullName);
Arrays & Vectors
int arr[5] = {1, 2, 3, 4, 5};
arr[0] = 10;
#include <vector>
vector<int> vec = {1, 2, 3};
vec.push_back(4);
vec.pop_back();
Control Flow
if (age >= 18) {
cout << "Adult";
} else if (age >= 13) {
cout << "Teen";
} else {
cout << "Child";
}
string status = (age >= 18) ? "Adult" : "Minor";
Loops
for (int i = 0; i < 5; i++) {
cout << i;
}
while (count < 10) {
count++;
}
do {
count--;
} while (count > 0);
Functions
int add(int a, int b) {
return a + b;
}
void greet(string name) {
cout << "Hello " << name;
}
int main() {
int sum = add(5, 3);
greet("John");
}
Strings
#include <string>
string text = "Hello";
text += " World";
text.length();
text.substr(0, 5);
text.find("lo");
text.replace(0, 5, "Hi");