C++ by Example

By Matt Mahoney

This tutorial gives examples of the most important language features of C++. For more details on specific features, there is a quick reference and detailed reference.

Prerequisites: you should have a C++ compiler installed. The examples here were tested with MinGW g++. If you are using Windows, you must know how to open a command window, be familiar with commands like dir, cd, copy, del, mkdir etc., and know how to set your PATH.

To use this tutorial, examine each program, then compile and run it.

All programs on this page are written by Matt Mahoney and may be used freely for any purpose.

  • guess.cpp - a number guessing game

    Simple output

  • hello1.cpp - prints "hello world"
  • hello2.cpp - chained output, endl
  • hello3.cpp - dropping "std::" from cout
  • hello4.cpp - dropping "std::" everywhere
  • hello5.cpp - comments and whitespace

    Arithmetic

  • arith1.cpp - integer input and arithmetic
  • arith2.cpp - double (real) input and arithmetic
  • arith3.cpp - operator precedence
  • arith4.cpp - initialization
  • arith5.cpp - mixed mode arithmetic
  • arith6.cpp - computing pi (roundoff error, formatting)

    Control statements

  • control1.cpp - if, comparison operators
  • control2.cpp - if-else, and, or
  • control3.cpp - while
  • control4.cpp - do
  • control5.cpp - for
  • control6.cpp - nested loops
  • control7.cpp - break
  • control8.cpp - computing prime numbers (trial division)

    Strings and characters

  • string1.cpp - string input as words
  • string2.cpp - checking for end of file
  • string3.cpp - reading lines of input
  • string4.cpp - reading chars, skipping whitespace
  • string5.cpp - reading chars, including whitespace
  • string6.cpp - character arithmetic
  • string7.cpp - accessing chars in strings
  • string8.cpp - substring search
  • string9.cpp - concatenation, parsing
  • string10.cpp - string representation of big numbers (pi)

    Functions

  • function1.cpp - void functions
  • function2.cpp - arguments and return, prototypes
  • function3.cpp - pass by reference

    Classes

  • class1.cpp - structs and objects
  • class2.cpp - classes, member functions, constructors, private data
  • class3.cpp - implicit conversion, default arguments
  • class4.cpp - data abstraction (pi using Fixed class)
  • class5.cpp - operator overloading

    Vectors

  • vector1.cpp - vector as an array
  • vector2.cpp - vector as a stack
  • vector3.cpp - finding largest element
  • vector4.cpp - bubble sort
  • vector5.cpp - removing duplicates
  • vector6.cpp - sieve of Eratosthenes (finding primes)
  • vector7.cpp - vector as class member (pi, base 10000)
  • vector8.cpp - bitwise operators (pi, base 65536)

    Iterators and Arrays

  • iter1.cpp - vector iterators
  • iter2.cpp - arrays
  • iter3.cpp - character arrays
  • iter4.cpp - command line arguments

    Files

  • file1.cpp - text files
  • file2.cpp - binary files

    Structs and Maps

  • map1.cpp - counting words using a vector of struct
  • map2.cpp - counting words using a map

    Advanced Features

  • misc1.cpp - templates, memory allocation, inheritance, const, exceptions