C++ Syntax Cheat Sheet
1. Basic Structure
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
2. Data Types
- Primitive: int, float, double, char, bool
- Derived: pointers, arrays, references
- User-defined: struct, class, enum
3. Control Statements
- if, else if, else
- switch, case, default
- for, while, do-while loops
- break, continue, return, goto
4. Functions
- Declaration: int add(int, int);
- Definition: int add(int a, int b) { return a + b; }
- Overloading, inline, default arguments, lambda
5. Object-Oriented Programming
- Classes and Objects
- Inheritance: class Derived : public Base { ... };
- Polymorphism: virtual functions
- Encapsulation: private, public, protected
6. Templates
- Function Template: template <typename T> T add(T a, T b);
- Class Template: template <typename T> class MyClass { T data; };
7. Pointers and Memory
- Pointers: int* ptr = &var;
- Dynamic Memory: int* ptr = new int; delete ptr;
8. Operator Overloading
class MyClass {
MyClass operator+(const MyClass& obj) { ... }
};
9. Exception Handling
try { throw 20; }
catch (int e) { cout << "Caught: " << e; }
catch (...) { cout << "Caught unknown"; }
10. STL (Standard Template Library)
- Containers: vector<int>, map<int, string>
- Algorithms: sort, find, binary_search