Table of Contents:
C++ Programming Language Cheat Sheet (Complete Syntax, OOP Concepts, and Examples)
C++ Programming Language Cheat Sheet — Complete Reference for Developers
C++ is a high-performance, general-purpose programming language developed by Bjarne Stroustrup as an extension of C. It supports object-oriented programming (OOP), templates, and Standard Template Library (STL) — making it one of the most versatile languages for software, game, and system development.
This C++ Cheatsheet provides an easy-to-follow, structured overview of all the key syntax, features, and examples you need to become proficient in C++ programming.
C++ Basics for Beginners
| Concept | Details | Example |
|---|---|---|
| File Extension | .cpp | main.cpp |
| Compiler | GCC, Turbo C++, Clang | g++ main.cpp -o main |
| Execution | Run compiled file | ./main |
| Header File | Preprocessor directives | #include <iostream> |
| Namespace | Avoids naming conflicts | using namespace std; |
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, C++ World!";
return 0;
}
Data Types and Variables in C++
| Type | Keyword | Size (bytes) | Example |
|---|---|---|---|
| Integer | int | 4 | int age = 25; |
| Floating-point | float, double | 4 / 8 | double pi = 3.1416; |
| Character | char | 1 | char grade = 'A'; |
| Boolean | bool | 1 | bool isValid = true; |
| String | string | Variable | string name = "John"; |
C++ Operators
| Type | Operators | Example |
|---|---|---|
| Arithmetic | + - * / % | x + y |
| Relational | == != > < >= <= | if (a > b) |
| Logical | `&& | |
| Assignment | = += -= *= /= | a += 10 |
| Bitwise | `& | ^ ~ << >>` |
| Ternary | ? : | result = (a > b) ? a : b; |
c++ programming cheat sheet, c++ syntax examples, c++ reference guide, c++ programming basics, c++ classes and objects, c++ oops concepts, c++ functions examples, c++ templates guide, c++ stl cheat sheet, c++ beginner tutorial
Input and Output in C++
C++ replaces the old scanf and printf with stream-based I/O.
Example:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You are " << age << " years old.";
return 0;
}
Conditional Statements
| Type | Syntax | Example |
|---|---|---|
| if | if (condition) | if(x>0){} |
| if-else | if (condition) else | if(a>b) cout<<a; else cout<<b; |
| else-if ladder | Multiple conditions | if(x>0)... else if(x<0)... else... |
| switch | switch(expression) | switch(choice) { case 1: break; } |
Loops in C++
| Type | Syntax | Example |
|---|---|---|
| for | for(init; condition; inc) | for(int i=0;i<5;i++) |
| while | while(condition) | while(i<10) |
| do-while | do{ }while(condition); | Executes once before checking |
Example:
for(int i = 1; i <= 5; i++) {
cout << i << " ";
}
Functions in C++
| Type | Syntax | Example |
|---|---|---|
| No return | void greet() | void greet() { cout<<"Hi"; } |
| With return | int sum(int a, int b) | return a+b; |
| Inline Function | inline int square(int x) | Small reusable code |
| Default Argument | int add(int a, int b=5) | Optional parameters |
Example:
int add(int a, int b) {
return a + b;
}
Object-Oriented Programming (OOP) Concepts in C++
| Concept | Description | Example |
|---|---|---|
| Class | Blueprint of objects | class Car { }; |
| Object | Instance of a class | Car c1; |
| Encapsulation | Data hiding | private: keyword |
| Inheritance | Reusing base class | class Child : public Parent |
| Polymorphism | Same name, different behavior | virtual void show() |
| Abstraction | Hiding implementation details | Using abstract classes |
| Constructor/Destructor | Initialize or cleanup | Car() {}, ~Car() |
Example:
class Student {
string name;
public:
Student(string n) { name = n; }
void display() { cout << "Name: " << name; }
};
int main() {
Student s("Alice");
s.display();
}
Arrays and Strings in C++
Array Example:
int marks[5] = {85, 90, 75, 88, 92};
for(int i=0; i<5; i++)
cout << marks[i] << " ";
String Example:
string name = "C++ Programming";
cout << "Length: " << name.length();
Pointers in C++
| Concept | Description | Example |
|---|---|---|
| Pointer Declaration | int *ptr; | Stores address |
| Address-of | &variable | Gives memory address |
| Dereference | *ptr | Access value |
Example:
int num = 10;
int *ptr = #
cout << *ptr; // Output: 10
File Handling in C++
| Mode | Meaning |
|---|---|
ios::in | Read mode |
ios::out | Write mode |
ios::app | Append mode |
Example:
#include <fstream>
ofstream file("data.txt");
file << "Hello C++";
file.close();
STL (Standard Template Library)
| Component | Example | Purpose |
|---|---|---|
| Vector | vector<int> v; | Dynamic array |
| Map | map<int,string> m; | Key-value pair |
| Set | set<int> s; | Unique elements |
| Stack | stack<int> s; | LIFO |
| Queue | queue<int> q; | FIFO |
Templates in C++
Example:
template <class T>
T add(T a, T b) {
return a + b;
}
Usage:
Templates enable generic programming, allowing code reusability for multiple data types.
Exception Handling in C++
Example:
try {
int x = 0;
if(x == 0)
throw "Division by zero!";
}
catch(const char* msg) {
cout << msg;
}
FAQ — C++ Programming Language
Q1: What makes C++ different from C?
C++ adds Object-Oriented Programming, templates, and STL to the base C language.
Q2: Is C++ good for beginners?
Yes, it’s one of the best languages to understand low-level and high-level programming concepts.
Q3: What are real-world uses of C++?
Game engines, desktop applications, operating systems, and browsers use C++.
Q4: Which compiler should I use?
Use GCC, Clang, or Visual Studio for development.
Q5: Can I run C++ online?
Yes, use the PHPOnline C++ Compiler to run C++ programs instantly.

