CPlusPlus Comprehensive Guide
CPlusPlus Comprehensive Guide
Comprehensive C++
Programming Guide
From First cout to Recursion, OOP & Modern C++
Table of Contents
§1 Basics
cout, cin, what is C++, compilation, structure
§2 Data Types & Type Casting
int, double, bool, char, string + casting + auto
§3 Loops
for, while, do-while, range-for, break/continue
§4 Nested Structures & Scope
nesting, block scope, shadowing, namespaces
§5 Operations
arithmetic, <cmath>, string ops, shorthand
§6 Functions & Parameters
functions, pass-by-value/reference, overloading, default args
§7 Input
cin, getline, istringstream, validation
§8 Conditionals
if / else if / else, switch, ternary
§9 String Formatting & Output
cout formatting, iomanip, string operations
§10 Random
<random> header, engines, distributions, seeding
§11 Algorithm Principles
Boolean zen, assert, lookahead, fencepost, DeMorgan
§12 File Processing
ifstream/ofstream, getline, token-based, error handling
§13 Arrays
C-arrays, std::array, value/reference, 2D, pointer basics
§14 Vectors, Maps & More
vector, map, unordered_map, set, STL algorithms
§15 Objects & Classes
fields, constructors, this, const, static, Rule of 3/5
§16 The Big 4
Encapsulation, Inheritance, Polymorphism, Abstraction
§17 Sorting & Searching
std::sort, comparators, binary_search, algorithms, Big O
§18 Recursion
base case, call stack, memoization, classic problems
Page 2
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
§1 Basics
cout · cin · what is C++ · compilation · program structure · headers
What is C++?
C++ is a general-purpose, statically-typed, compiled programming language created by Bjarne
Stroustrup in 1979 as an extension of C. It supports multiple paradigms: procedural, object-oriented,
generic (templates), and functional. C++ compiles directly to native machine code — there is no virtual
machine — giving it maximum performance. It is the language of choice for operating systems, game
engines, embedded systems, high-frequency trading, and performance-critical applications.
Key CS Definitions
Compiled Language
Source (.cpp) is compiled by the compiler (g++/clang++) directly to machine code (.exe or binary).
No bytecode, no runtime interpreter.
Statically Typed
Every variable must have a declared type at compile time. The compiler catches type errors before
running.
Preprocessor
Runs before compilation. Handles #include, #define, #ifdef. Output is pure C++.
Linker
Combines compiled object files (.o) into a final executable. Resolves references between files.
Page 3
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
// File: [Link]
#include <iostream> // header: gives us cout, cin, endl
#include <string> // header: gives us std::string
int main() {
// Output with cout (<< is the 'insertion' operator)
cout << "Hello, World!" << endl; // endl flushes buffer
cout << "Hello, World!" << "\n"; // \n is faster (no flush)
cout << 42 << "\n";
cout << 3.14 << "\n";
cout << true << "\n"; // prints 1
// Chaining
cout << "Name: " << "Alice" << ", Age: " << 30 << "\n";
return 0;
}
Comments
Page 4
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
// Single-line comment
/*
* Multi-line comment
*/
Compilation Steps
# Single file
g++ -std=c++17 -Wall -o program [Link]
# Multiple files
g++ -std=c++17 -Wall -o program [Link] [Link]
# Common flags:
# -std=c++17 use C++17 standard (also c++11, c++14, c++20)
# -Wall enable all warnings
# -O2 optimization level 2
# -g include debug symbols (for gdb)
■ Always compile with -Wall. Warnings often reveal real bugs. Never ignore undefined behavior — it won't crash
consistently, making it the hardest bug to find.
Page 5
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
Fundamental Types
bool true or false. Size: 1 byte typically.
unsigned int Non-negative int: 0 to ~4.3B. Never goes negative — wraps around!
size_t Unsigned type for sizes/counts. Use for array indices and .size()
returns.
Page 6
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
#include <iostream>
#include <climits> // INT_MAX etc.
#include <cfloat> // DBL_MAX etc.
using namespace std;
// Size inspection
cout << sizeof(int) << "\n"; // 4 (bytes)
cout << sizeof(double) << "\n"; // 8
cout << INT_MAX << "\n"; // 2147483647
std::string
std::string is a class in the C++ standard library. It manages its own memory. Include <string> to use it.
Unlike C-strings (char arrays), it is safe and resizable.
Page 7
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
#include <string>
using namespace std;
Type Casting
Page 8
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
■ Signed integer overflow is undefined behavior in C++. Use long long or unsigned types for large values. Never
assume overflow wraps around — the compiler may optimize it away.
Page 9
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
§3 Loops
for · while · do-while · range-for · break · continue
for Loop
for (int i = 0; i < 5; i++)
cout << i << " "; // 0 1 2 3 4
// Counting down
for (int i = 10; i >= 1; i--)
cout << i << " ";
// Step of 2
for (int i = 0; i <= 20; i += 2)
cout << i << " ";
// Multiple variables
for (int i=0, j=10; i < j; i++, j--)
cout << i << " " << j << "\n";
#include <vector>
using namespace std;
// With auto
for (auto& item : fruits)
item += "!"; // modifies in place (& = reference)
while Loop
Page 10
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
int count = 0;
while (count < 5) {
cout << count << "\n";
count++;
}
do-while Loop
int choice;
do {
cout << "1. Start 2. Help 3. Quit\n";
cout << "Choice: ";
cin >> choice;
} while (choice < 1 || choice > 3);
cout << "You chose: " << choice << "\n";
// continue
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) continue;
cout << i << " "; // 1 3 5 7 9
}
Page 11
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
Nested Loops
// Multiplication table
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++)
cout << i*j << "\t";
cout << "\n";
}
// Triangle
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= row; col++)
cout << "* ";
cout << "\n";
}
Scope in C++
Scope
The region of code where a variable is visible. C++ uses block scope — from declaration to the
closing }. Variables are DESTROYED when their scope ends (RAII).
void demo() {
int local = 10; // function scope
if (true) {
int block = 5; // block scope
cout << block << "\n"; // OK
cout << local << "\n"; // OK — outer visible
cout << global << "\n"; // OK — global visible
}
// cout << block; // ERROR — out of scope, also DESTROYED
Page 12
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
Variable Shadowing
int x = 10; // outer x
{
int x = 20; // inner x SHADOWS outer x
cout << x << "\n"; // 20
}
cout << x << "\n"; // 10 — outer x unchanged
// In a class:
class Foo {
int value;
public:
void set(int value) { // parameter shadows field
this->value = value; // this-> disambiguates
}
};
Namespaces
Namespace
A named scope that prevents name collisions between libraries. std:: is the standard library
namespace.
Page 13
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
// Full qualification
std::cout << "Hello\n";
std::string name = "Alice";
Page 14
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
§5 Operations
arithmetic · comparison · logical · bitwise · <cmath> · shorthand
Arithmetic Operators
+ Addition: 5 + 3 = 8
- Subtraction: 5 - 3 = 2
* Multiplication: 5 * 3 = 15
Page 15
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
// Logical
cout << (true && false) << "\n"; // 0
cout << (true || false) << "\n"; // 1
cout << (!true) << "\n"; // 0
// Short-circuit
int x = 0;
if (x != 0 && 10/x > 2) // 10/x never evaluated
cout << "yes\n";
Augmented Assignment
int x = 10;
x += 3; x -= 2; x *= 4;
x /= 5; x %= 3;
// No **= in C++; use pow()
string s = "Hello";
s += " World"; // std::string supports +=
Page 16
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
■ Integer division silently truncates in C++ — it's the number one source of bugs for beginners. Always cast to
double before dividing when you need a decimal result.
Page 17
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
Defining Functions
Function
A named reusable block of code. Must be declared before use (or use a prototype).
Return Type
The type returned via return. Use void for no return value.
int main() {
cout << add(3, 5) << "\n"; // 8
greet("Alice"); // Hello, Alice!
return 0;
}
Pass-by-Value vs Pass-by-Reference
Pass-by-Value
A copy is made. The original is unaffected.
Pass-by-Reference (&)
An alias to the original. Changes affect the caller. No copy — efficient for large objects.
Pass-by-const-Reference
Read-only alias — efficient AND safe. Use for large objects you don't need to modify.
Page 18
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
// By value — copy
void doubleVal(int x) { x *= 2; } // original unchanged
int n = 5;
doubleVal(n); cout << n << "\n"; // 5 — unchanged
doubleRef(n); cout << n << "\n"; // 10 — changed!
Default Arguments
void describePet(string name, string animal="dog", int age=1) {
cout << name << " is a " << age << "-yr-old " << animal << "\n";
}
Function Overloading
double area(double r) { return 3.14159 * r * r; } // circle
double area(double w, double h) { return w * h; } // rectangle
Function Prototypes
Page 19
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
int main() {
cout << toFahrenheit(100) << "\n"; // 212
return 0;
}
Page 20
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
§7 Input
cin · getline · istringstream · input validation · [Link]()
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "Enter your name: ";
cin >> name; // reads one word — stops at space
cout << "Hello, " << name << "!\n";
int age;
cout << "Enter age: ";
cin >> age; // reads int
double gpa;
cin >> gpa; // reads double
return 0;
}
string fullName;
int age;
Page 21
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
Input Validation
int age = -1;
while (age < 0 || age > 120) {
cout << "Enter age (0-120): ";
if (!(cin >> age)) { // extraction failed (non-numeric)
[Link](); // clear error flags
[Link](1000, '\n'); // discard bad input
age = -1;
} else if (age < 0 || age > 120) {
cout << "Out of range.\n";
}
}
cout << "Valid age: " << age << "\n";
■ After a failed cin >> extraction, the stream enters an error state. You MUST call [Link]() before any further
reads will work.
Page 22
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
§8 Conditionals
if / else if / else · switch · ternary · if-initializer (C++17)
if / else if / else
int score = 85;
string grade;
Ternary Operator
int age = 20;
string status = (age >= 18) ? "adult" : "minor";
cout << status << "\n"; // adult
// Compact abs
int x = -7;
int absX = (x >= 0) ? x : -x;
switch Statement
Page 23
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
int day = 3;
switch (day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
case 3: cout << "Wednesday"; break;
case 4:
case 5: cout << "Thu or Fri"; break; // fall-through
default: cout << "Weekend";
}
// Switch on char
char grade = 'B';
switch (grade) {
case 'A': cout << "Excellent"; break;
case 'B': cout << "Good"; break;
case 'C': cout << "Average"; break;
default: cout << "Below average";
}
// if (initializer; condition)
if (int val = compute(); val > 0) {
cout << "Positive: " << val << "\n";
} else {
cout << "Non-positive: " << val << "\n";
}
// val is not accessible here
■ Always include break in each switch case unless fall-through is intentional. Annotate intentional fall-through
with a comment: // [[fallthrough]]
Page 24
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
<iomanip> Formatting
The <iomanip> header provides manipulators to control cout output format. Most manipulators are
'sticky' — they persist until changed.
#include <iostream>
#include <iomanip>
using namespace std;
double pi = 3.14159265;
// Boolean
cout << boolalpha << true << "\n"; // true (not 1)
cout << noboolalpha; // reset
printf (C-style)
Page 25
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
#include <cstdio>
std::format (C++20)
#include <format> // C++20 only
using namespace std;
String Operations
#include <string>
#include <sstream>
using namespace std;
// Number to string
string s1 = to_string(42);
string s2 = to_string(3.14);
// String to number
int n = stoi("123");
double d = stod("3.14");
long l = stol("123456789");
Page 26
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
§10 Random
<random> header · engines · distributions · seeding · legacy rand()
#include <random>
#include <iostream>
using namespace std;
// 1. Create an engine
mt19937 rng(42); // Mersenne Twister, seed=42
// 2. Create a distribution
uniform_int_distribution<int> die(1, 6); // [1, 6]
uniform_real_distribution<double> prob(0, 1); // [0.0, 1.0)
normal_distribution<double> gauss(170, 10); // mean=170, sd=10
// 3. Generate numbers
for (int i = 0; i < 5; i++)
cout << die(rng) << " ";
cout << "\n";
Non-Deterministic Seed
// random_device gives a truly random seed from the OS
random_device rd;
mt19937 rng(rd()); // different every run
Shuffling
Page 27
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
#include <algorithm>
#include <vector>
using namespace std;
vector<int> deck(52);
iota([Link](), [Link](), 1); // fill 1..52
mt19937 rng(random_device{}());
shuffle([Link](), [Link](), rng);
for (int i=0; i<5; i++) cout << deck[i] << " ";
■ rand() % N is biased when RAND_MAX+1 is not divisible by N. Always prefer std::uniform_int_distribution for
correct results.
Page 28
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
Boolean Zen
bool isValid = true;
// BAD
if (isValid == true) { /* ... */ }
if (found == false) { /* ... */ }
// GOOD
if (isValid) { /* ... */ }
if (!found) { /* ... */ }
// BAD
bool isEven(int n) {
if (n % 2 == 0) return true;
else return false;
}
// GOOD
bool isEven(int n) { return n % 2 == 0; }
assert
#include <cassert>
Fencepost Problem
Page 29
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
Lookahead
vector<int> nums = {1, 2, 2, 3, 4, 4, 4, 5};
DeMorgan's Laws
!(A && B) = !A || !B
!(A || B) = !A && !B
int x = 3, y = 8;
if (!(x > 5 && y < 10)) cout << "DeMorgan 1\n"; // original
if (x <= 5 || y >= 10) cout << "DeMorgan 1\n"; // equivalent
Page 30
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
int main() {
ifstream inFile("[Link]");
if (!inFile) { // check if open succeeded
cerr << "Cannot open file\n";
return 1;
}
// Line-by-line
string line;
while (getline(inFile, line))
cout << line << "\n";
[Link]();
return 0;
}
// Append mode
ofstream logFile("[Link]", ios::app);
logFile << "New entry\n";
Token-Based Reading
Page 31
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
std::filesystem (C++17)
Page 32
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
#include <filesystem>
namespace fs = std::filesystem;
if (fs::exists("[Link]"))
cout << "File size: " << fs::file_size("[Link]") << "\n";
// Iterate directory
for (const auto& entry : fs::directory_iterator("."))
cout << [Link]() << "\n";
Page 33
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
§13 Arrays
C-arrays · std::array · pointer basics · value/reference · 2D arrays
C-Style Arrays
Array
A fixed-size contiguous block of same-type elements. No bounds checking. Decays to a pointer
when passed to functions.
Page 34
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
#include <array>
using namespace std;
// Range-for works
for (int n : primes)
cout << n << " ";
// Sort, find
sort([Link](), [Link]());
auto it = find([Link](), [Link](), 5);
2D Arrays
Page 35
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
■ Prefer std::vector over C-arrays for dynamic or function-passed data, and std::array over C-arrays for
fixed-size data. Both are safer and more idiomatic.
Page 36
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
vector<string> names;
names.push_back("Alice");
names.push_back("Bob");
names.push_back("Carol");
[Link]([Link]()+1, "Dave"); // insert at index 1
// Initializer list
vector<int> nums = {5, 2, 8, 1, 9, 3};
// 2D vector
vector<vector<int>> matrix(3, vector<int>(4, 0));
matrix[1][2] = 7;
Page 37
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
#include <map>
using namespace std;
[Link]("Bob");
cout << [Link]() << "\n"; // 2
Page 38
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
#include <set>
#include <unordered_set>
using namespace std;
vector<int> v = {5,2,8,1,9,3};
Page 39
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
Object
An instance created with the class type (or new for heap allocation).
Member
A field or method belonging to a class.
Access Modifiers
public (accessible anywhere), private (class only), protected (class + subclasses). Default in class:
private; in struct: public.
Page 40
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
class Dog {
public:
// Constructor
Dog(string name, int age, string breed)
: name(name), age(age), breed(breed) { // member initializer list
totalDogs++;
}
// Static method
static int getTotalDogs() { return totalDogs; }
// Operator overloading
bool operator<(const Dog& other) const { return age < [Link]; }
private:
string name;
int age;
string breed;
static int totalDogs; // declaration
};
// Usage
Dog d1("Rex", 3, "German Shepherd");
Dog d2("Fluffy", 1, "Poodle");
cout << [Link]() << "\n"; // Rex says: Woof!
cout << d1 << "\n"; // Dog(Rex, 3yo, German Shepherd)
cout << Dog::getTotalDogs() << "\n"; // 2
cout << (d2 < d1) << "\n"; // 1 (true — d2 younger)
Page 41
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
class Point {
public:
double x, y;
Rule of 3 / 5 / 0
If a class manages a resource (heap memory, file handle), define all of: destructor, copy constructor,
copy assignment. With C++11 move semantics: also move constructor and move assignment (Rule of
5). Or use RAII containers and define none (Rule of 0).
Page 42
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
1. Encapsulation
Encapsulation
Bundling data and methods together, hiding internal state behind an interface using
private/protected. Protects class invariants.
class BankAccount {
string owner;
double balance; // private by default in class
public:
BankAccount(string o, double b=0)
: owner(o), balance(max(0.0, b)) {}
2. Inheritance
Inheritance
A derived class (child) inherits from a base class (parent) using the : syntax. C++ supports multiple
inheritance (inheriting from more than one base).
Page 43
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
class Animal {
protected:
string name;
int age;
public:
Animal(string n, int a) : name(n), age(a) {}
virtual string speak() const { return "..."; } // virtual!
void eat() const { cout << name << " is eating.\n"; }
virtual ~Animal() {} // virtual destructor — essential!
};
Dog d("Rex",3,"Lab");
cout << [Link]() << "\n"; // Woof!
[Link](); // Rex is eating. (inherited)
cout << (d is Animal?) << "\n"; // use dynamic_cast or typeid
3. Polymorphism
Polymorphism
Virtual functions enable runtime polymorphism — the correct function is chosen based on the actual
object type, even through a base-class pointer/reference. Requires the virtual keyword in the base
class.
Page 44
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
4. Abstraction
Abstraction
Hiding implementation details. C++ achieves this with pure virtual functions (= 0) making a class
abstract — it cannot be instantiated directly.
Page 45
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
class Shape {
public:
virtual double area() const = 0; // pure virtual
virtual double perimeter() const = 0;
virtual void describe() const { // concrete
cout << typeid(*this).name()
<< " area=" << fixed << setprecision(2) << area() << "\n";
}
virtual ~Shape() {}
};
// Shape s; ← compile error: abstract class!
vector<unique_ptr<Shape>> shapes;
shapes.push_back(make_unique<Circle>(5));
shapes.push_back(make_unique<Rectangle>(4,6));
for (const auto& s : shapes) s->describe();
Page 46
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
Big O Complexity
O(1) Constant: array index, unordered_map lookup
vector<int> v = {5,2,8,1,9,3};
sort([Link](), [Link]()); // ascending
sort([Link](), [Link](), greater<int>()); // descending
// Sort array
int arr[] = {5,2,8,1,9};
sort(arr, arr+5);
// Sort structs
struct Student { string name; int score; };
vector<Student> sts = {{"Alice",95},{"Bob",82},{"Carol",91}};
sort([Link](), [Link](),
[](const Student& a, const Student& b){ return [Link] > [Link]; });
Page 47
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
// STL
auto it = find([Link](), [Link](), 8);
if (it != [Link]())
cout << "Found at index " << (it - [Link]()) << "\n";
// Manual implementation
int bsearch(const vector<int>& v, int target) {
int lo=0, hi=(int)[Link]()-1;
while (lo <= hi) {
int mid = lo + (hi-lo)/2;
if (v[mid]==target) return mid;
else if (v[mid] < target) lo = mid+1;
else hi = mid-1;
}
return -1;
}
Page 48
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
Page 49
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
§18 Recursion
base case · call stack · memoization · classic problems · tail recursion
What is Recursion?
A function calls itself to solve a smaller version of the same problem. Every recursive solution needs:
Factorial
long long factorial(int n) {
if (n == 0) return 1; // base case
return n * factorial(n - 1); // recursive case
}
cout << factorial(5) << "\n"; // 120
cout << factorial(0) << "\n"; // 1
Page 50
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
// Naive O(2^n)
int fib(int n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
}
for (int i=0; i<10; i++) cout << fibMemo(i) << " ";
// 0 1 1 2 3 5 8 13 21 34
Array Sum
int sum(const vector<int>& v, int i) {
if (i == (int)[Link]()) return 0;
return v[i] + sum(v, i+1);
}
// Call: sum({1,2,3,4,5}, 0) == 15
Tower of Hanoi
Page 51
C++ Comprehensive C++ Programming Guide Sections 1-18 . CS Definitions . Examples
Stack safety Deep C++ recursion causes segfault with no nice error. Use iteration
for depth > ~10k.
End of Guide
You have covered all 18 sections of the Comprehensive C++ Programming Guide.
Happy coding! C++
Page 52