C++ Documentation
Concise, example-driven C++ reference. Read the concept, see the code, then practice it in a Coddy journey.
Start a guided C++ journeyGetting Started
- What Is C++What C++ actually is, why it compiles straight to native machine code, and the kinds of high-performance software it is used to build.
- Install C++How to install a C++ compiler (GCC, Clang, or MSVC), pick a toolchain for your operating system, and confirm it works from the command line.
- Compiling C++How C++ turns .cpp source into a native executable: compile with g++ (or clang++/MSVC), run the binary, and read the compiler errors when it goes wrong.
- C++ SyntaxThe core syntax rules of a C++ program - statements and semicolons, curly-brace blocks, the main function, and how output works - explained from a first program.
- CommentsHow to write comments in C++ - single-line // notes and multi-line /* */ blocks - plus how to comment out code, why block comments don't nest, and what makes a comment worth keeping.
Variables & Types
- VariablesHow variables work in C++ - declaring with a type, the difference between assignment and initialization, brace init, naming rules, and the scope that decides where a variable lives and dies.
- Data TypesA practical tour of C++'s fundamental data types - integers, floating-point, char, and bool - plus sizes, signed vs unsigned, literals and suffixes, overflow, and how to pick the right type.
- auto KeywordHow the `auto` keyword lets the compiler deduce a variable's type for you - what it strips away, where it shines, and the gotchas that bite beginners.
- Constants & constHow to declare read-only values in C++ with const, the difference between const and constexpr, const pointers vs pointers to const, and const member functions.
- OperatorsLearn C++ operators - arithmetic, comparison, logical, assignment, and bitwise - plus the gotchas around integer division, precedence, and short-circuiting.
- Type CastingHow type casting works in C++ - implicit conversions, the integer-division trap, and the four named casts (static_cast, const_cast, reinterpret_cast, dynamic_cast) - with the gotchas that cause silent data loss.
Control Flow
- if-elseHow to make decisions in C++ with if, else if, and else - conditions, chaining branches, nesting, the ternary operator, if with initializer, and the gotchas that trip people up.
- switchThe C++ switch statement explained - case labels, break and fall-through, the default branch, grouping cases, switching on enums, and the gotchas around declarations inside cases.
- for-loopHow to repeat code with the C++ for loop - the three-part header, counting up and down, looping over arrays, nesting, break and continue, and the off-by-one and unsigned bugs that bite everyone.
- while LoopsThe C++ while loop explained - the condition-first while, the run-at-least-once do-while, looping until a sentinel, break and continue, and how to avoid infinite loops.
- Range-Based forThe C++ range-based for loop explained - clean iteration over arrays, vectors, strings and maps, why you should use auto& and const auto&, and the copy and iterator-invalidation gotchas to avoid.
Functions
- FunctionsHow to write functions in C++ - the return-type/name/parameters anatomy, declarations vs definitions, returning values, void functions, and the gotchas like missing returns and forgetting prototypes.
- Function ParametersHow arguments get into C++ functions - pass by value vs by reference, const references for cheap read-only access, default arguments, pointers, and the copy-cost gotchas that quietly slow programs down.
- ReferencesC++ references explained - how the & in a parameter creates an alias, why pass-by-reference avoids copies and lets a function modify its caller's variables, when to reach for const& and references vs returning values.
- Function OverloadingC++ function overloading lets several functions share one name as long as their parameter lists differ. Learn how overload resolution picks the match, why return type alone doesn't count, and the ambiguity and default-argument traps to avoid.
- LambdasWrite small inline functions on the fly with C++ lambdas - the syntax, how captures work, when to use `mutable`, and the dangling-capture trap that bites everyone.
Pointers & Memory
- PointersC++ pointers explained from scratch - declaring a pointer, the & address-of and * dereference operators, nullptr, pointers to arrays, and the dangling-pointer and uninitialized-pointer gotchas that cause crashes.
- References vs PointersA practical comparison of references and pointers in C++ - what they share, where they differ (rebinding, null, arithmetic), and a clear rule for which one to reach for in everyday code.
- Dynamic MemoryHow to allocate memory at runtime with `new`, free it with `delete`, and avoid the leaks, dangling pointers, and double-frees that come with managing the heap by hand.
- Smart PointersSmart pointers own heap memory and free it automatically. Learn `unique_ptr`, `shared_ptr`, `make_unique`, and `make_shared` - and why you should almost never write `new`/`delete` again.
Strings & I/O
- StringsHow to use `std::string` in C++ - building, joining, searching, and slicing text safely, plus why you almost never want a raw `char*` for real work.
- Input & OutputHow console I/O works in C++ - printing with cout, reading with cin, the classic getline-after-cin newline bug, and how to recover when input fails.
- String StreamsHow to use std::stringstream, istringstream, and ostringstream to parse text, split strings on whitespace, convert between strings and numbers, and build formatted strings in memory.
STL Containers
- ArraysC++ raw arrays explained - how to declare and initialize them, index safely, loop over them with size, the array-to-pointer decay trap, and why std::array and vector usually beat them.
- vectorstd::vector is C++'s resizable array - the container you should reach for by default. Learn how to create, access, grow, and loop over a vector, plus the iterator-invalidation and out-of-bounds gotchas.
- mapThe C++ std::map explained - a sorted key-value container with logarithmic lookup. Insert, find, iterate, and avoid the classic operator[] gotcha that silently inserts keys.
- unordered_mapLearn std::unordered_map in C++ - the hash-table sibling of map that gives average O(1) insert and lookup. Covers basic operations, the [] auto-insert gotcha, count vs find, and when to pick it over an ordered map.
- setHow std::set stores unique, automatically sorted values in C++ - inserting, checking membership with count and find, iterating in order, and the differences between set, multiset, and unordered_set.
- pair and tupleHow `std::pair` and `std::tuple` bundle two or more values into one object - making them, accessing the fields, structured bindings, and where each one fits.
- IteratorsHow C++ iterators work as generalized pointers into containers - begin() and end(), dereferencing, advancing, the const/reverse variants, and the invalidation and end()-dereference gotchas that cause undefined behavior.
STL Algorithms & Templates
- AlgorithmsUse the C++ standard algorithms - `find`, `count_if`, `transform`, `accumulate`, `remove` - to do real work over ranges without hand-written loops, plus the iterator-pair and erase-remove gotchas.
- SortingSort vectors and arrays in C++ with `std::sort` - default order, custom comparators, sorting structs by a field, and the strict-weak-ordering trap that causes crashes.
- TemplatesWrite code once and let it work for every type with C++ templates - function templates, class templates, type deduction, and the confusing compiler errors they cause.
Classes & Objects
- ClassesLearn how C++ classes bundle data and behavior into reusable types - declaring member variables and methods, creating objects, the public/private split, and the gotchas like uninitialized members and the this pointer.
- ConstructorsA constructor is the special member function that runs when an object is created. Learn default, parameterized, and copy constructors, member initializer lists, and how to avoid leaving objects half-initialized.
- DestructorsA destructor runs automatically when an object is destroyed. Learn the `~ClassName()` syntax, when it fires, why it frees resources, and the Rule of Three/Five.
- InheritanceLearn how C++ inheritance lets a derived class reuse and extend a base class - the syntax, public vs private inheritance, constructor and destructor ordering, and the gotchas like object slicing.
- Virtual FunctionsVirtual functions let a base-class pointer call the derived class's version of a method at runtime. Learn `virtual`, `override`, abstract classes, and why a base destructor must be virtual.
- Operator OverloadingC++ operator overloading lets your own types work with built-in operators like +, ==, and <<. Learn the member vs non-member rules, how to overload comparison and stream operators, and the gotchas around return types and the assignment operator.
- Access SpecifiersHow public, private, and protected control who can touch a class's members in C++ - the foundation of encapsulation, with getters, setters, and the friend escape hatch.
- StructsC++ structs explained - how to bundle related variables into a single type, declare and initialize struct objects, give a struct member functions and constructors, and how struct really differs from class.
- enumsLearn C++ enums - how to declare them, why scoped enum class is safer than plain enum, custom underlying values, switching on enumerators, and converting to and from integers.
Errors & Debugging
- ExceptionsExceptions report errors that a function cannot handle locally. Learn how to `throw`, what the standard exception types are, the `what()` message, and why exceptions beat return codes for the failures that matter.
- try-catchWrap risky code in `try`, react in `catch`. Learn how to catch exceptions by const reference, order multiple handlers, use `catch (...)`, and rethrow - without leaking resources.
- Undefined BehaviorUndefined behavior (UB) is code the C++ standard places no rules on - it may crash, corrupt data, or appear to work. Learn the common causes, why "it ran fine" proves nothing, and the tools that catch UB.