C++ Tutorials

This C++ tutorial series covers the language from basic syntax and variables through control flow, arrays, vectors, classes, exception handling, standard library features, and example programs. Beginners can follow the topics in order, while programmers looking for a specific C++ concept can use the sections below as a reference.

How to learn C++ with these tutorials

If you are learning C++ by yourself, start with a small working program and then add one concept at a time. A practical sequence is: understand program structure, learn variables and data types, read input, use operators and conditions, write loops, work with strings and arrays, and then move to classes and standard library containers such as std::vector.

For each topic, type and run the examples instead of only reading them. Change values, add statements, and observe the output. This makes syntax errors, type rules, control flow, and runtime behavior easier to understand.

A first C++ program

A basic C++ program can include the <iostream> header, define the main() function, write output with std::cout, and return an integer status value.

</>
Copy
#include <iostream>

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

Output

Hello, World!

The tutorials below build on these basic elements and introduce the language in progressively larger pieces.


C++ basics for beginners

Start with the structure of a C++ program, variables, built-in data types, comments, and recursion. These topics provide the foundation needed before working with conditions, loops, containers, and object-oriented programming.

  1. C++ Hello World Program
  2. C++ Variables
  3. C++ Datatypes
  4. C++ Comments
  5. C++ Recursion

C++ input and output with cin and cout

C++ console programs commonly use std::cin to read values and std::cout to print values. Learning standard input and output also introduces stream operators such as >> and <<.

  1. C++ cin

Related C++ output topic:

  1. C++: Difference Between \ and std::endl

C++ operators and expressions

Operators are used to calculate values, compare expressions, assign results, combine conditions, and manipulate individual bits. The tutorials in this section cover arithmetic, assignment, bitwise, logical, and relational operators in C++.

  1. C++ Operators
  2. C++ Arithmetic Operators
  3. C++ Assignment Operators
  4. C++ Bitwise Operators
  5. C++ Logical Operators
  6. C++ Relational Operators

C++ decision making with if, switch, and the ternary operator

Decision-making statements change program flow according to a condition. Use if and if-else for conditional branches, switch when selecting among suitable constant cases, and the conditional operator ?: when a compact conditional expression is appropriate.

  1. C++ If Else
  2. C++ Switch
  3. C++ Ternary Operator

C++ loops and loop control statements

Loops execute statements repeatedly. C++ provides while, do-while, traditional for, and range-based for loops. The break and continue statements let a program alter the normal flow of a loop.

  1. C++ While Loop
  2. C++ Do While Loop
  3. C++ For Loop
  4. C++ Foreach
  5. C++ Break
  6. C++ Continue

C++ type conversions between numbers, strings, and characters

Type conversion is needed when a program has a value in one representation but an operation requires another. Common examples include converting text to integers, numbers to strings, and characters to strings. Conversion should be chosen according to both the source type and the expected destination type.

  1. C++ int to string
  2. C++ string to int
  3. C++ string to double
  4. C++ char to string
  5. C++ string to char
  6. C++ Print datatype of variable

C++ strings and common string operations

C++ programs commonly use std::string for text. String operations include creating strings, reading and modifying characters, concatenating values, checking length, and using strings with input and output operations.

  1. C++ Strings

C++ exception handling with try and catch

C++ exceptions provide a structured way to report and handle certain error conditions. Code that may throw an exception can be placed in a try block, while matching catch handlers define how the program responds.

  1. C++ Try Catch

C++ object-oriented programming with classes

Classes let you define user-created types that combine data and behavior. They are central to object-oriented C++ and provide the basis for topics such as objects, constructors, access control, inheritance, and polymorphism.

  1. C++ Class

C++ arrays for fixed-size collections

A built-in C++ array stores a fixed number of elements of the same type in contiguous memory. Array elements are accessed by index, beginning at index 0. Arrays are useful for learning indexed collections before moving to higher-level standard library containers.

  1. C++ Arrays

C++ vectors for dynamically sized sequences

std::vector is a standard library sequence container that stores elements contiguously and can change its size as elements are added or removed. It is commonly preferred when the number of elements is not fixed in advance.

  1. C++ Vectors

C++ array and vector: which one should a beginner use?

Use a built-in array when you specifically need a fixed-size array or are learning how basic indexed storage works. Use std::vector when the collection should grow or shrink at runtime and you want standard container operations such as push_back() and size().


C++ example programs for programming practice

After learning individual C++ concepts, example programs provide practice in combining variables, conditions, loops, functions, arrays, strings, and other language features to solve small programming problems.

  1. C++ Programs

C++ date and time operations

Date and time programs may need to read the current system time and then format or process the returned value. The following tutorial demonstrates how to obtain the current time in C++.

  1. C++ Get Current Time

C++ standard library algorithms

The C++ standard library includes reusable algorithms that operate on ranges of elements. Learning these functions helps reduce manually written loops for common operations and introduces the iterator-based design used throughout the standard library.

  1. C++ algorithm

Suggested C++ learning order for complete beginners

A beginner does not need to learn every C++ feature at once. The following order keeps the early lessons focused on concepts that are required by later topics.

  1. Run a Hello World program and understand main(), headers, statements, and output.
  2. Learn variables, fundamental data types, initialization, and basic input with cin.
  3. Practice arithmetic, relational, logical, and assignment operators.
  4. Write programs using if, switch, and loops.
  5. Work with strings, arrays, and vectors.
  6. Learn functions and recursion, then combine them with collections and control flow.
  7. Move to classes and object-oriented C++ after you are comfortable with variables, functions, and basic program flow.
  8. Practice with small C++ programs and begin using appropriate standard library facilities instead of rewriting common operations from scratch.

Compiling and running C++ programs while learning

C++ source code must be compiled before it runs. If a C++ compiler is available from the command line, a source file such as main.cpp can be compiled into an executable and then run. The exact compiler command depends on the compiler and operating system.

For example, with g++:

</>
Copy
g++ main.cpp -o main
./main

When practicing, pay attention to compiler diagnostics. They often identify syntax errors, missing declarations, incompatible types, and other problems before the program runs.

How to get better at C++ after learning the syntax

Knowing C++ syntax is only the first part of learning the language. Build small programs that require several concepts at once. For example, read a list of values, store them in a vector, process them with a loop or standard algorithm, and print the result. Then rewrite parts of the program as functions or classes when that makes the code clearer.

As your programs become larger, also learn to read compiler messages, use a debugger, understand object lifetime, prefer standard library types where appropriate, and separate code into functions and files with clear responsibilities.

C++ tutorial path at a glance

Learning stageC++ topics to focus on
Starting C++Hello World, variables, data types, comments, input and output
Program flowOperators, if-else, switch, for loops, while loops, break, continue
Working with dataStrings, arrays, vectors, and type conversions
Program structureFunctions, recursion, and classes
Error handlingExceptions with try and catch
Practical C++Example programs, standard library algorithms, date and time operations

Use the individual tutorials as focused references when you encounter a concept in a program, and return to the example programs to practice combining multiple C++ features in working code.