C++ is a powerful, high-performance programming language used for systems programming, game engines, browsers, operating systems, and performance-critical applications. It combines the low-level control of C with high-level abstractions.
Why C++?
- Performance — One of the fastest languages, close to hardware
- Control — Manual memory management, no runtime overhead
- Abstraction — Templates, classes, lambdas for clean code
- Ubiquitous — Browsers (Chrome), games (Unreal), OS (Windows), databases
- Evolving — Modern C++ (C++20/23) is productive and expressive
- Interoperable — Easy to call from/to C, and most languages
C++ vs Other Languages
| Feature | C++ | Rust | Java | Python |
|---|---|---|---|---|
| Speed | Excellent | Excellent | Good | Slow |
| Memory | Manual | Ownership | GC | GC |
| Compile | Native | Native | JVM bytecode | Interpreted |
| Learning curve | Steep | Steep | Moderate | Easy |
| Use case | Systems, games | Systems, safety | Enterprise | Scripting |
Hello World
CPP
#include <iostream>
#include <string>
#include <vector>
int main() {
std::cout << "Hello, World!" << std::endl;
// Modern C++ (C++11+)
std::string name = "Alice";
int age = 28;
std::cout << "Name: " << name << ", Age: " << age << std::endl;
return 0;
}Variables and Types
CPP
// Basic types
int x = 42;
double pi = 3.14159;
bool active = true;
char c = 'A';
std::string name = "Alice";
// Modern C++ type deduction
auto y = 42; // int
auto pi2 = 3.14; // double
auto str = "hello"; // const char*
// Fixed-width types
#include <cstdint>
int32_t a = 1000;
uint64_t big = 18446744073709551615ULL;
float f = 3.14f;
// Constexpr (compile-time evaluation)
constexpr int MAX_SIZE = 1024;
constexpr double PI = 3.14159265358979;
// References
int x = 42;
int& ref = x; // Reference (alias)
ref = 100; // x is now 100
// Pointers
int* ptr = &x; // Pointer (address of x)
*ptr = 200; // x is now 200Functions
CPP
// Basic function
int add(int a, int b) {
return a + b;
}
// Default parameters
void greet(std::string name, std::string greeting = "Hello") {
std::cout << greeting << ", " << name << "!" << std::endl;
}
// Overloading
int multiply(int a, int b) { return a * b; }
double multiply(double a, double b) { return a * b; }
// Lambda (anonymous function)
auto square = [](int x) { return x * x; };
square(5); // 25
// With capture
int factor = 3;
auto times = [factor](int x) { return x * factor; };
times(5); // 15
// Templates
template<typename T>
T max_val(T a, T b) {
return (a > b) ? a : b;
}
max_val(3, 7); // 7
max_val(3.14, 2.71); // 3.14Containers
CPP
#include <vector>
#include <map>
#include <set>
#include <array>
// Vector (dynamic array)
std::vector<int> nums = {1, 2, 3, 4, 5};
nums.push_back(6);
nums.pop_back();
nums.size(); // 5
nums[0]; // 1
nums.at(0); // 1 (bounds-checked)
// Range-based for
for (const auto& num : nums) {
std::cout << num << " ";
}
// Map (dictionary)
std::map<std::string, int> scores;
scores["Alice"] = 95;
scores["Bob"] = 87;
scores.count("Alice"); // 1 (exists)
for (const auto& [name, score] : scores) { // Structured binding
std::cout << name << ": " << score << "\n";
}
// Set (unique elements)
std::set<int> unique = {1, 2, 3, 2, 1}; // {1, 2, 3}
unique.insert(4);
unique.count(2); // 1
// Array (fixed-size)
std::array<int, 5> arr = {1, 2, 3, 4, 5};Smart Pointers (Modern C++)
CPP
#include <memory>
// Unique pointer (exclusive ownership)
auto ptr = std::make_unique<int>(42);
auto uptr = std::make_unique<std::string>("Hello");
// Cannot copy, can move
auto moved = std::move(uptr); // uptr is now nullptr
// Shared pointer (shared ownership)
auto shared = std::make_shared<int>(42);
auto copy = shared; // Both point to same object
shared.use_count(); // 2
// Weak pointer (non-owning reference)
std::weak_ptr<int> weak = shared;
if (auto locked = weak.lock()) {
std::cout << *locked << std::endl;
}💡 Tip: Use
autofor type deduction, smart pointers instead of rawnew/delete, range-based for loops instead of indices, andconstexprfor compile-time constants.
Next: C++ Functions