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.



Chapter 0Introduction / Getting Started0.1Introduction to these tutorials0.2Introduction to programming languages0.3Introduction to C/C++0.4Introduction to C++ development0.5Introduction to the compiler, linker, and libraries0.6Installing an Integrated Development Environment (IDE)0.7Compiling your first program0.8A few common C++ problems0.9Configuring your compiler: Build configurations0.10Configuring your compiler: Compiler extensions0.11Configuring your compiler: Warning and error levels0.12Configuring your compiler: Choosing a language standardChapter 1C++ Basics1.1Statements and the structure of a program1.2Comments1.3Introduction to objects and variables1.4Variable assignment and initialization1.5Introduction to iostream: cout, cin, and endl1.6Uninitialized variables and undefined behavior1.7Keywords and naming identifiers1.8Whitespace and basic formatting1.9Introduction to literals and operators1.10Introduction to expressions1.11Developing your first program1.xChapter 1 summary and quiz




Chapter 2C++ Basics: Functions and Files2.1Introduction to functions2.2Function return values2.3Introduction to function parameters and arguments2.4Introduction to local scope2.5Why functions are useful, and how to use them effectively2.6Forward declarations and definitions2.7Programs with multiple code files2.8Naming collisions and an introduction to namespaces2.9Introduction to the preprocessor2.10Header files2.11Header guards2.12How to design your first programs2.xChapter 2 summary and quizChapter 3Debugging C++ Programs3.1Syntax and semantic errors3.2The debugging process3.3A strategy for debugging3.4Basic debugging tactics3.5More debugging tactics3.6Using an integrated debugger: Stepping3.7Using an integrated debugger: Running and breakpoints3.8Using an integrated debugger: Watching variables3.9Using an integrated debugger: The call stack3.10Finding issues before they become problems3.xChapter 3 summary and quizChapter 4Fundamental Data Types4.1Introduction to fundamental data types4.2Void4.3Object sizes and the sizeof operator4.4Signed integers4.5Unsigned integers, and why to avoid them4.6Fixed-width integers and size_t4.7Introduction to scientific notation4.8Floating point numbers4.9Boolean values4.10Introduction to if statements4.11Chars4.12Introduction to type conversion and static_castNew4.13An introduction to std::stringMoved4.14Literals4.15Const, constexpr, and symbolic constants4.xChapter 4 summary and quizChapter 5Operators5.1Operator precedence and associativity5.2Arithmetic operators5.3Modulus and Exponentiation5.4Increment/decrement operators, and side effects5.5Comma and conditional operators5.6Relational operators and floating point comparisons5.7Logical operators5.xChapter 5 summary and quizChapter OBit Manipulation (optional chapter)O.1Bit flags and bit manipulation via std::bitsetO.2Bitwise operatorsO.3Bit manipulation with bitwise operators and bit masksO.4Converting between binary and decimalChapter 6Scope, Duration, and Linkage6.1Compound statements (blocks)6.2User-defined namespaces and the scope resolution operator6.3Local variables6.4Introduction to global variables6.5Variable shadowing (name hiding)6.6Internal linkage6.7External linkage6.8Why (non-const) global variables are evil6.9Sharing global constants across multiple files (using inline variables)6.10Static local variables6.11Scope, duration, and linkage summary6.12Using declarations and using directives6.13Unnamed and inline namespaces6.xChapter 6 summary and quizChapter 7Control Flow and Error Handling7.1Control flow introduction7.2If statements and blocks7.3Common if statement problems7.4Switch statement basics7.5Switch fallthrough and scoping7.6Goto statements7.7Intro to loops and while statements7.8Do while statements7.9For statements7.10Break and continue7.11Halts (exiting your program early)7.12Introduction to testing your code7.13Code coverage7.14Common semantic errors in C++7.15Detecting and handling errors7.16std::cin and handling invalid input7.17Assert and static_assert7.xChapter 7 summary and quiz

Jun 18: Chapter 8 is newly added.Chapter 8Type Conversion and Function Overloading8.1Implicit type conversion (coercion)8.2Floating-point and integral promotion8.3Numeric conversions8.4Arithmetic conversions8.5Explicit type conversion (casting) and static_cast8.6Typedefs and type aliases8.7Type deduction for objects using the auto keyword8.8Type deduction for functions8.9Introduction to function overloading8.10Function overload differentiation8.11Function overload resolution and ambiguous matches8.12Default arguments8.13Function templates8.14Function template instantiation8.15Function templates with multiple template types8.xChapter 8 summary and quizChapter 9Compound Types (under construction)9.1Using a language reference9.2Enumerated types9.3Enum classes9.4Structs9.5Random number generation9.xChapter 9 summary and quizChapter 10Arrays, Strings, Pointers, and References10.1Arrays (Part I)10.2Arrays (Part II)10.3Arrays and loops10.4Sorting an array using selection sort10.5Multidimensional Arrays10.6C-style strings10.7An introduction to std::string_view10.8Introduction to pointers10.9Null pointers10.10Pointers and arrays10.11Pointer arithmetic and array indexing10.12C-style string symbolic constants10.13Dynamic memory allocation with new and delete10.14Dynamically allocating arrays10.15Pointers and const10.16Reference variables10.17References and const10.18Member selection with pointers and references10.19For-each loops10.20Void pointers10.21Pointers to pointers and dynamic multidimensional arrays10.22An introduction to std::array10.23An introduction to std::vector10.24Introduction to iterators10.25Introduction to standard library algorithms10.xChapter 10 comprehensive quizChapter 11Functions11.1Function parameters and arguments11.2Passing arguments by value11.3Passing arguments by reference11.4Passing arguments by address11.5Returning values by value, reference, and address11.6Inline functions11.7Function Pointers11.8The stack and the heap11.9std::vector capacity and stack behavior11.10Recursion11.11Command line arguments11.12Ellipsis (and why to avoid them)11.13Introduction to lambdas (anonymous functions)11.14Lambda captures11.xChapter 11 comprehensive quizChapter 12Basic Object-oriented Programming12.1Welcome to object-oriented programming12.2Classes and class members12.3Public vs private access specifiers12.4Access functions and encapsulation12.5Constructors12.6Constructor member initializer lists12.7Non-static member initialization12.8Overlapping and delegating constructorsUpdated12.9Destructors12.10The hidden “this” pointer12.11Class code and header files12.12Const class objects and member functions12.13Static member variables12.14Static member functions12.15Friend functions and classes12.16Anonymous objects12.17Nested types in classes12.18Timing your code12.xChapter 12 comprehensive quizChapter 13Operator overloading13.1Introduction to operator overloading13.2Overloading the arithmetic operators using friend functions13.3Overloading operators using normal functions13.4Overloading the I/O operators13.5Overloading operators using member functions13.6Overloading unary operators +, -, and !13.7Overloading the comparison operators13.8Overloading the increment and decrement operators13.9Overloading the subscript operator13.10Overloading the parenthesis operator13.11Overloading typecasts13.12The copy constructor13.13Copy initialization13.14Converting constructors, explicit, and delete13.15Overloading the assignment operator13.16Shallow vs. deep copying13.17Overloading operators and function templates13.xChapter 13 comprehensive quizChapter 14Reserved for Future ReorganizationChapter 15Reserved for Future ReorganizationChapter 16An Introduction to Object Relationships16.1Object relationships16.2Composition16.3Aggregation16.4Association16.5Dependencies16.6Container classes16.7std::initializer_list16.xChapter 16 comprehensive quizChapter 17Inheritance17.1Introduction to inheritance17.2Basic inheritance in C++17.3Order of construction of derived classes17.4Constructors and initialization of derived classes17.5Inheritance and access specifiers17.6Adding new functionality to a derived class17.7Calling inherited functions and overriding behavior17.8Hiding inherited functionality17.9Multiple inheritance17.xChapter 17 comprehensive quizChapter 18Virtual Functions18.1Pointers and references to the base class of derived objects18.2Virtual functions and polymorphism18.3The override and final specifiers, and covariant return types18.4Virtual destructors, virtual assignment, and overriding virtualization18.5Early binding and late binding18.6The virtual table18.7Pure virtual functions, abstract base classes, and interface classes18.8Virtual base classes18.9Object slicing18.10Dynamic casting18.11Printing inherited classes using operator18.xChapter 18 comprehensive quizChapter 19Templates and Classes19.1Template classes19.2Template non-type parameters19.3Function template specialization19.4Class template specialization19.5Partial template specialization19.6Partial template specialization for pointers19.xChapter 19 comprehensive quizChapter 20Exceptions20.1The need for exceptions20.2Basic exception handling20.3Exceptions, functions, and stack unwinding20.4Uncaught exceptions and catch-all handlers20.5Exceptions, classes, and inheritance20.6Rethrowing exceptions20.7Function try blocks20.8Exception dangers and downsides20.9Exception specifications and noexcept20.xChapter 20 comprehensive quiz

Chapter MMove Semantics and Smart PointersM.1Intro to smart pointers and move semanticsM.2R-value referencesM.3Move constructors and move assignmentM.4std::moveM.5std::move_if_noexceptM.6std::unique_ptrM.7std::shared_ptrM.8Circular dependency issues with std::shared_ptr, and std::weak_ptrM.xChapter M comprehensive reviewChapter 21The Standard Template Library21.1The Standard Library21.2STL containers overview21.3STL iterators overview21.4STL algorithms overviewChapter 22std::string22.1std::string and std::wstring22.2std::string construction and destruction22.3std::string length and capacity22.4std::string character access and conversion to C-style arrays22.5std::string assignment and swapping22.6std::string appending22.7std::string insertingChapter 23Input and Output (I/O)23.1Input and output (I/O) streams23.2Input with istream23.3Output with ostream and ios23.4Stream classes for strings23.5Stream states and input validation23.6Basic file I/O23.7Random file I/OAppendix AMiscellaneous SubjectsA.1Static and dynamic librariesA.2Using libraries with Visual StudioA.3Using libraries with Code::BlocksAppendix BC++ UpdatesB.1Introduction to C++11B.2Introduction to C++14B.3Introduction to C++17B.4Introduction to C++20Appendix CThe EndC.1The end?