C++
Here’s a comprehensive guide to C++ programming, from fundamentals to advanced concepts, with examples along the way. This will cover syntax, memory management, OOP, templates, STL, and modern C++ (C++11–C++23).
🧠 Overview of C++
- General-purpose, compiled, statically typed language.
- Supports procedural, object-oriented, generic, and functional programming.
- Known for performance, fine-grained memory control, and close-to-hardware programming.
🧱 1. Basic Syntax
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}Key Concepts
| Concept | Description | Example |
|---|---|---|
#include |
Preprocessor directive | #include <iostream> |
main() |
Entry point | int main() { ... } |
std::cout |
Output stream | std::cout << "Hi"; |
return |
Exit code | return 0; |
🔢 2. Variables, Data Types, and Operators
int age = 23;
float price = 19.99;
char grade = 'A';
bool isActive = true;Operators
- Arithmetic:
+ - * / % - Logical:
&& || ! - Comparison:
== != < > <= >= - Assignment:
= += -=
🧭 3. Control Flow
int num = 10;
if (num > 5) {
std::cout << "Big number\n";
} else {
std::cout << "Small number\n";
}Loops
// For loop
for (int i = 0; i < 5; ++i)
std::cout << i << " ";
// While loop
int i = 0;
while (i < 5)
std::cout << i++ << " ";📦 4. Functions
int add(int a, int b) {
return a + b;
}Overloading
int multiply(int a, int b) { return a * b; }
double multiply(double a, double b) { return a * b; }🧰 5. Object-Oriented Programming (OOP)
Classes & Objects
class Car {
public:
string model;
void drive() { std::cout << "Driving " << model << std::endl; }
};
int main() {
Car c;
c.model = "Tesla";
c.drive();
}Encapsulation, Inheritance, Polymorphism
class Animal {
public:
virtual void speak() { std::cout << "Animal sound\n"; }
};
class Dog : public Animal {
public:
void speak() override { std::cout << "Woof!\n"; }
};
int main() {
Animal* a = new Dog();
a->speak(); // Woof!
}🎯 6. Memory Management
Pointers
int a = 10;
int* ptr = &a;
std::cout << *ptr; // dereferencenew and delete
int* arr = new int[10];
delete[] arr;🏗️ 7. Templates (Generic Programming)
template <typename T>
T max(T a, T b) {
return (a > b) ? a : b;
}📚 8. Standard Template Library (STL)
Includes:
- Containers:
vector,map,set,list, etc. - Algorithms:
sort,find,accumulate - Iterators and Functionals
Vector Example
#include <vector>
#include <algorithm>
std::vector<int> v = {5, 2, 9, 1};
std::sort(v.begin(), v.end());⚙️ 9. Modern C++ Features
✅ C++11
auto,nullptr,range-based for, lambdas
auto x = 42;
for (auto n : v) std::cout << n;✅ C++14/17
- Structured bindings
std::pair<int, int> p = {1, 2};
auto [a, b] = p;✅ C++20/23
- Concepts
- Coroutines
std::rangesstd::format
#include <format>
std::string s = std::format("The answer is {}", 42);🧪 10. Compile and Run
g++ main.cpp -o main
./main🧭 Summary Table
| Feature | C++98/03 | C++11 | C++14 | C++17 | C++20 | C++23 |
|---|---|---|---|---|---|---|
auto |
❌ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Lambda | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ |
std::shared_ptr |
❌ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Ranges | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ |
| Coroutines | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ |
🚀 Projects to Learn C++
- Tic Tac Toe Game
- Banking System Simulation
- Simple Web Server with Sockets
- Memory Allocator
- Tiny STL clone
- Embedded system sensor logger (e.g., with Arduino)