← Back to Tutorials

C++ Programming Tutorial: Complete Guide with Code Examples

A beginner-friendly, end-to-end C++ tutorial. Learn syntax, control flow, functions, classes, pointers, and the STL — and run every example in our online C++ compiler without installing anything.

Why Learn C++?

C++ powers operating systems, game engines (Unreal, Unity's native layer), high-frequency trading systems, embedded firmware, browsers (Chrome, Firefox), and most performance-critical software you use every day. Learning C++ teaches you how computers actually work — memory, pointers, and how high-level abstractions map to machine code.

<div><span>Industry standard for performance-critical software</span></div><div><span>Deep understanding of memory and computer architecture</span></div><div><span>Required for game development, embedded systems, and systems programming</span></div><div><span>Strong foundation for learning Rust, Go, Java, and C#</span></div>
<div><div><div id="hello">1. Your First C++ Program</div></div> <div><p>Every C++ program starts with a main() function. iostream gives you cout for printing to the console.</p> <div><pre>#include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; return 0; }</pre></div> <span><a>Try this example</a></span></div></div><div><div><div id="variables">2. Variables and Data Types</div></div> <div><p>C++ is statically typed — you declare the type up front. The core types you'll use every day are int, double, char, bool, and std::string.</p> <div><pre>#include <iostream> #include <string> using namespace std; int main() { int age = 25; double price = 19.99; char grade = 'A'; bool isStudent = true; string name = "Alice"; cout << name << " is " << age << " years old" << endl; cout << "Grade: " << grade << ", Price:
quot; << price << endl; return 0; }</pre></div> <span><a>Try this example</a></span></div></div><div><div><div id="conditionals">3. Conditional Statements</div></div> <div><p>Use if / else if / else to branch on a condition. Braces are optional for single statements but recommended for clarity.</p> <div><pre>#include <iostream> using namespace std; int main() { int score = 85; if (score >= 90) cout << "Grade: A" << endl; else if (score >= 80) cout << "Grade: B" << endl; else if (score >= 70) cout << "Grade: C" << endl; else cout << "Grade: F" << endl; return 0; }</pre></div> <span><a>Try this example</a></span></div></div><div><div><div id="loops">4. Loops</div></div> <div><p>for loops are perfect when you know the count up front; while loops run as long as a condition holds.</p> <div><pre>#include <iostream> using namespace std; int main() { // for loop for (int i = 1; i <= 5; i++) { cout << "Count: " << i << endl; } // while loop int n = 0; while (n < 3) { cout << "n = " << n << endl; n++; } return 0; }</pre></div> <span><a>Try this example</a></span></div></div><div><div><div id="functions">5. Functions</div></div> <div><p>Functions package reusable logic. Declare the return type, the name, and the parameter list.</p> <div><pre>#include <iostream> using namespace std; int add(int a, int b) { return a + b; } double average(double x, double y) { return (x + y) / 2.0; } int main() { cout << "Sum: " << add(10, 5) << endl; cout << "Average: " << average(8.0, 12.0) << endl; return 0; }</pre></div> <span><a>Try this example</a></span></div></div><div><div><div id="arrays-vectors">6. Arrays and Vectors</div></div> <div><p>C-style arrays have a fixed size. std::vector is the modern, resizable container you should reach for in most code.</p> <div><pre>#include <iostream> #include <vector> using namespace std; int main() { // C-style array int nums[5] = {10, 20, 30, 40, 50}; cout << "First: " << nums[0] << endl; // Modern C++ vector vector<string> fruits = {"apple", "banana", "cherry"}; fruits.push_back("orange"); for (const string& f: fruits) { cout << f << endl; } return 0; }</pre></div> <span><a>Try this example</a></span></div></div><div><div><div id="classes">7. Classes and Objects</div></div> <div><p>C++ supports object-oriented programming. A class bundles data (members) and behaviour (methods) together.</p> <div><pre>#include <iostream> #include <string> using namespace std; class Person { public: string name; int age; Person(string n, int a): name(n), age(a) {} void greet() const { cout << "Hi, I'm " << name << " and I'm " << age << endl; } }; int main() { Person p("Alice", 30); p.greet(); return 0; }</pre></div> <span><a>Try this example</a></span></div></div><div><div><div id="pointers">8. Pointers and References</div></div> <div><p>A pointer holds a memory address. Use & to take an address and * to dereference. Modern C++ also offers references and smart pointers (unique_ptr, shared_ptr) to manage memory safely.</p> <div><pre>#include <iostream> using namespace std; int main() { int value = 42; int* ptr = &value; // pointer stores the address cout << "Value: " << value << endl; cout << "Address: " << ptr << endl; cout << "Deref: " << *ptr << endl; *ptr = 100; // modify via pointer cout << "Updated: " << value << endl; return 0; }</pre></div> <span><a>Try this example</a></span></div></div>
Next Steps in Your C++ Journey
<div><span>Smart pointers (unique_ptr, shared_ptr) and RAII</span></div><div><span>Templates and generic programming</span></div><div><span>STL algorithms: sort, find, transform, accumulate</span></div><div><span>Move semantics and rvalue references</span></div><div><span>Multithreading with std::thread and std::async</span></div><div><span>Modern C++20/23 features: concepts, ranges, modules</span></div>

Practice C++ Online

Ready to write your own C++ code? Open the online C++ compiler, paste any example from this tutorial, and run it instantly — no setup, no toolchain, no installation.