0% found this document useful (0 votes)
3 views3 pages

CPP Guide

C++ is a multi-paradigm programming language that supports object-oriented, generic, procedural, and functional programming, created by Bjarne Stroustrup in 1985. Modern C++ introduces features like smart pointers, lambdas, and move semantics, enhancing safety and performance. The document serves as a concise guide covering essential aspects such as classes, the Standard Template Library, and best practices for writing modern C++ code.

Uploaded by

brave.browser.a
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

CPP Guide

C++ is a multi-paradigm programming language that supports object-oriented, generic, procedural, and functional programming, created by Bjarne Stroustrup in 1985. Modern C++ introduces features like smart pointers, lambdas, and move semantics, enhancing safety and performance. The document serves as a concise guide covering essential aspects such as classes, the Standard Template Library, and best practices for writing modern C++ code.

Uploaded by

brave.browser.a
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++

Programming Language Guide

A concise reference to modern C++: classes, templates, STL, and beyond.

Paradigm Multi-paradigm: OOP, generic, procedural, functional


Typing Static, strong
Created by Bjarne Stroustrup (1985)
Standard C++11 / C++14 / C++17 / C++20 / C++23
Use cases Game engines, OS, compilers, embedded, finance
1. Introduction

C++ is a powerful, statically-typed, compiled language that extends C with object-oriented and generic
programming features. Modern C++ (C++11 and later) introduced smart pointers, lambdas, move
semantics, and a rich standard library, making the language both expressive and safe without
sacrificing performance.

2. Hello World & Program Structure

#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0;
}

Compile with: g++ -std=c++17 -Wall [Link] -o hello

3. Classes & OOP

class Animal { public: Animal(std::string name) : name_(name) {} virtual std::string


speak() const = 0; // pure virtual std::string name() const { return name_; } private:
std::string name_; }; class Dog : public Animal { public: using Animal::Animal;
std::string speak() const override { return name() + " says Woof!"; } };

4. Smart Pointers (Modern C++)

#include <memory> // unique_ptr — sole ownership auto p1 =


std::make_unique<Dog>("Rex"); // shared_ptr — shared ownership auto p2 =
std::make_shared<Dog>("Buddy"); auto p3 = p2; // ref count = 2 // No manual delete
needed — RAII handles cleanup

Prefer unique_ptr by default; use shared_ptr only when shared ownership is genuinely required.
Avoid raw new / delete.

5. Standard Template Library (STL)

Container Header Typical use


std::vector <vector> Dynamic array
std::map <map> Sorted key-value (tree)
std::unordered_map <unordered_map> Hash key-value
std::set <set> Unique sorted elements
std::list <list> Doubly-linked list
std::queue <queue> FIFO queue
std::stack <stack> LIFO stack

6. Lambdas & Algorithms

#include <algorithm> #include <vector> std::vector<int> nums = {5, 2, 8, 1, 9}; //


Sort std::sort([Link](), [Link]()); // Lambda + for_each
std::for_each([Link](), [Link](), [](int n){ std::cout << n << " "; }); //
Range-based for (C++11) for (auto& n : nums) n *= 2;

7. Move Semantics (C++11)

Move semantics allow resources to be transferred instead of copied, dramatically improving


performance for containers and user-defined types.

std::vector<int> a = {1, 2, 3}; std::vector<int> b = std::move(a); // a is now empty


// Move constructor in class MyClass(MyClass&& other) noexcept :
data_(std::exchange(other.data_, nullptr)) {}

8. Modern C++ Best Practices

• Use smart pointers instead of raw new/delete.


• Prefer auto for type deduction where it improves readability.
• Use const and constexpr generously.
• Leverage RAII: tie resource lifetimes to object lifetimes.
• Prefer algorithms from over hand-written loops.
• Enable warnings: -Wall -Wextra -pedantic and treat them as errors.
• Follow the Rule of Zero: let the compiler generate special member functions.

You might also like