Search

The C++ Tutorial

LearnCpp.com is a free website devoted to teaching you how to program in C++. Whether you’ve had any prior programming experience or not, the tutorials on this site will walk you through all the steps to write, compile, and debug your C++ programs, all with plenty of examples.

Becoming an expert won’t happen overnight, but with a little patience, you’ll get there. And LearnCpp.com will show you the way.

Having trouble remembering where you saw something? Not sure where to find something? Use our site index to find what you’re looking for!


Chapter 0Introduction / Getting Started
0.1Introduction to these tutorials
0.2Introduction to programming languages
0.3Introduction to C/C++
0.4Introduction to C++ development
0.5Introduction to the compiler, linker, and libraries
0.6Installing an Integrated Development Environment (IDE)
0.7Compiling your first program
0.8A few common C++ problems
0.9Configuring your compiler: Build configurations
0.10Configuring your compiler: Compiler extensions
0.11Configuring your compiler: Warning and error levels
0.12
Split
Configuring your compiler: Choosing a language standard
Chapter 1C++ Basics
1.1Statements and the structure of a program
1.2Comments
1.3Introduction to variables
1.4Variable assignment and initialization
1.5Introduction to iostream: cout, cin, and endl
1.6Uninitialized variables and undefined behavior
1.7Keywords and naming identifiers
1.8Introduction to literals and operators
1.9Introduction to expressions
1.10Developing your first program
1.xChapter 1 summary and quiz
Chapter 2C++ Basics: Functions and Files
2.1Introduction to functions
2.2Function return values
2.3Introduction to function parameters and arguments
2.4Introduction to local scope
2.5Why functions are useful, and how to use them effectively
2.6Whitespace and basic formatting
2.7Forward declarations and definitions
2.8Programs with multiple code files
2.9
Updated
Naming collisions and an introduction to namespaces
2.10Introduction to the preprocessor
2.11Header files
2.12Header guards
2.13How to design your first programs
2.xChapter 2 summary and quiz
Chapter 3Debugging C++ Programs
3.1Syntax and semantic errors
3.2The debugging process
3.3A strategy for debugging
3.4Basic debugging tactics
3.5More debugging tactics
3.6Using an integrated debugger: Stepping
3.7Using an integrated debugger: Running and breakpoints
3.8Using an integrated debugger: Watching variables
3.9Using an integrated debugger: The call stack
3.10Finding issues before they become problems
3.xChapter 3 summary and quiz
Chapter 4Fundamental Data Types
4.1Introduction to fundamental data types
4.2Void
4.3Object sizes and the sizeof operator
4.4Signed integers
4.5Unsigned integers, and why to avoid them
4.6Fixed-width integers and size_t
4.7Introduction to scientific notation
4.8Floating point numbers
4.9Boolean values
4.10Introduction to if statements
4.11Chars
4.12Literals
4.13Const, constexpr, and symbolic constants
4.xChapter 4 summary and quiz
Chapter 5Operators
5.1Operator precedence and associativity
5.2Arithmetic operators
5.3Modulus and Exponentiation
5.4Increment/decrement operators, and side effects
5.5Comma and conditional operators
5.6Relational operators and floating point comparisons
5.7Logical operators
5.xChapter 5 summary and quiz
Chapter OBit Manipulation (optional chapter)
O.1Bit flags and bit manipulation via std::bitset
O.2Bitwise operators
O.3Bit manipulation with bitwise operators and bit masks
O.4Converting between binary and decimal
Chapter 6Object Scope and Conversions
6.1
Updated
Compound statements (blocks)
6.2
Updated
User-defined namespaces
6.3
Updated
Local variables
6.4
Updated
Introduction to global variables
6.5
Updated
Variable shadowing (name hiding)
6.6
Updated
Internal linkage
6.7
Updated
External linkage
6.8
Updated
Global constants and inline variables
6.9
Updated
Why global variables are evil
6.10
Updated
Static local variables
6.11
Updated
Scope, duration, and linkage summary
6.12
Updated
Using statements
6.13
Updated
Typedefs and type aliases
6.14
Updated
The auto keyword
6.15
Updated
Implicit type conversion (coercion)
6.16
Updated
Explicit type conversion (casting) and static_cast
6.17
Updated
Unnamed and inline namespaces
6.x
Updated
Chapter 6 summary and quiz

Please excuse the disjoint lesson numbering while we continue to update our tutorials.

Chapter SCompound Types (originally chapter 4)
S.4.4b
Updated
An introduction to std::string
S.4.5
Updated
Enumerated types
S.4.5a
Updated
Enum classes
S.4.7
Updated
Structs
S.7.x
Updated
Chapter 7 summary and quiz
Chapter LControl Flow (originally chapter 5)
L.5.1Control flow introduction
L.5.2If statements
L.5.3Switch statements
L.5.4Goto statements
L.5.5While statements
L.5.6Do while statements
L.5.7For statements
L.5.8Break and continue
L.5.9Random number generation
L.5.10std::cin, extraction, and dealing with invalid text input
L.5.11Introduction to testing your code
L.5.xChapter 5 comprehensive quiz

Chapter PArrays, Strings, Pointers, and References (originally chapter 6)
P.6.1Arrays (Part I)
P.6.2Arrays (Part II)
P.6.3Arrays and loops
P.6.4Sorting an array using selection sort
P.6.5Multidimensional Arrays
P.6.6C-style strings
P.6.6a
New
An introduction to std::string_view
P.6.7Introduction to pointers
P.6.7aNull pointers
P.6.8Pointers and arrays
P.6.8aPointer arithmetic and array indexing
P.6.8bC-style string symbolic constants
P.6.9Dynamic memory allocation with new and delete
P.6.9aDynamically allocating arrays
P.6.10Pointers and const
P.6.11Reference variables
P.6.11aReferences and const
P.6.12Member selection with pointers and references
P.6.12aFor-each loops
P.6.13Void pointers
P.6.14Pointers to pointers and dynamic multidimensional arrays
P.6.15An introduction to std::array
P.6.16An introduction to std::vector
P.6.17
New
Introduction to iterators
P.6.18
New
Introduction to standard library algorithms
P.6.xChapter P.6 comprehensive quiz
Chapter FFunctions (originally chapter 7)
F.7.1Function parameters and arguments
F.7.2Passing arguments by value
F.7.3Passing arguments by reference
F.7.4Passing arguments by address
F.7.4aReturning values by value, reference, and address
F.7.5Inline functions
F.7.6Function overloading
F.7.7Default arguments
F.7.8Function Pointers
F.7.9The stack and the heap
F.7.10std::vector capacity and stack behavior
F.7.11Recursion
F.7.12Handling errors, cerr and exit
F.7.12aAssert and static_assert
F.7.13Command line arguments
F.7.14Ellipsis (and why to avoid them)
F.7.15
New
Introduction to lambdas (anonymous functions)
F.7.16
New
Lambda captures
F.7.xChapter 7 comprehensive quiz
Chapter 8Basic object-oriented programming
8.1Welcome to object-oriented programming
8.2Classes and class members
8.3Public vs private access specifiers
8.4Access functions and encapsulation
8.5Constructors
8.5aConstructor member initializer lists
8.5bNon-static member initialization
8.6Overlapping and delegating constructors
8.7Destructors
8.8The hidden “this” pointer
8.9Class code and header files
8.10Const class objects and member functions
8.11Static member variables
8.12Static member functions
8.13Friend functions and classes
8.14Anonymous objects
8.15Nested types in classes
8.16Timing your code
8.xChapter 8 comprehensive quiz
Chapter 9Operator overloading
9.1Introduction to operator overloading
9.2Overloading the arithmetic operators using friend functions
9.2aOverloading operators using normal functions
9.3Overloading the I/O operators
9.4Overloading operators using member functions
9.5Overloading unary operators +, -, and !
9.6Overloading the comparison operators
9.7Overloading the increment and decrement operators
9.8Overloading the subscript operator
9.9Overloading the parenthesis operator
9.10Overloading typecasts
9.11The copy constructor
9.12Copy initialization
9.13Converting constructors, explicit, and delete
9.14Overloading the assignment operator
9.15Shallow vs. deep copying
9.xChapter 9 comprehensive quiz
Chapter 10An introduction to object relationships
10.1Object relationships
10.2Composition
10.3Aggregation
10.4Association
10.5Dependencies
10.6Container classes
10.7std::initializer_list
10.xChapter 10 comprehensive quiz
Chapter 11Inheritance
11.1Introduction to inheritance
11.2Basic inheritance in C++
11.3Order of construction of derived classes
11.4Constructors and initialization of derived classes
11.5Inheritance and access specifiers
11.6Adding new functionality to a derived class
11.6aCalling inherited functions and overriding behavior
11.6bHiding inherited functionality
11.7Multiple inheritance
11.xChapter 11 comprehensive quiz
Chapter 12Virtual Functions
12.1Pointers and references to the base class of derived objects
12.2Virtual functions and polymorphism
12.2aThe override and final specifiers, and covariant return types
12.3Virtual destructors, virtual assignment, and overriding virtualization
12.4Early binding and late binding
12.5The virtual table
12.6Pure virtual functions, abstract base classes, and interface classes
12.7Virtual base classes
12.8Object slicing
12.9Dynamic casting
12.10Printing inherited classes using operator<<
12.xChapter 12 comprehensive quiz
Chapter 13Templates
13.1Function templates
13.2Function template instances
13.3Template classes
13.4Template non-type parameters
13.5Function template specialization
13.6Class template specialization
13.7Partial template specialization
13.8Partial template specialization for pointers
13.xChapter 13 comprehensive quiz
Chapter 14Exceptions
14.1The need for exceptions
14.2Basic exception handling
14.3Exceptions, functions, and stack unwinding
14.4Uncaught exceptions, catch-all handlers, and exception specifiers
14.5Exceptions, classes, and inheritance
14.6Rethrowing exceptions
14.7Function try blocks
14.8Exception dangers and downsides
14.xChapter 14 comprehensive quiz
Chapter 15Move semantics and smart pointers
15.1Intro to smart pointers and move semantics
15.2R-value references
15.3Move constructors and move assignment
15.4std::move
15.5std::unique_ptr
15.6std::shared_ptr
15.7Circular dependency issues with std::shared_ptr, and std::weak_ptr
15.xChapter 15 comprehensive review
Chapter 16The Standard Template Library
16.1The Standard Library
16.2STL containers overview
16.3STL iterators overview
16.4STL algorithms overview
Chapter 17std::string
17.1std::string and std::wstring
17.2std::string construction and destruction
17.3std::string length and capacity
17.4std::string character access and conversion to C-style arrays
17.5std::string assignment and swapping
17.6std::string appending
17.7std::string inserting
Chapter 18Input and output (I/O)
18.1Input and output (I/O) streams
18.2Input with istream
18.3Output with ostream and ios
18.4Stream classes for strings
18.5Stream states and input validation
18.6Basic file I/O
18.7Random file I/O
Appendix AMiscellaneous Subjects
A.1Static and dynamic libraries
A.2Using libraries with Visual Studio
A.3Using libraries with Code::Blocks
Appendix BC++ Updates
B.1Introduction to C++11
B.2Introduction to C++14
B.3Introduction to C++17
Appendix CThe end
C.1The end?