C++ Examples

These C++ examples put the whole tutorial into practice with small, complete programs. Read each one, predict its output, then compile it and try the suggested changes.

FizzBuzz

#include <iostream>

int main() {
    for (int i = 1; i <= 15; ++i) {
        if (i % 15 == 0)      std::cout << "FizzBuzz";
        else if (i % 3 == 0)  std::cout << "Fizz";
        else if (i % 5 == 0)  std::cout << "Buzz";
        else                  std::cout << i;
        std::cout << ' ';
    }
    std::cout << '\n';
}
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 

Word Frequency Counter

#include <iostream>
#include <map>
#include <sstream>
#include <string>

int main() {
    std::string text = "the cat and the hat and the bat";
    std::map<std::string, int> freq;
    std::istringstream in(text);
    for (std::string w; in >> w; ) ++freq[w];

    for (const auto& [word, count] : freq)
        std::cout << word << ": " << count << '\n';
}
and: 2
bat: 1
cat: 1
hat: 1
the: 3

Sorting Students by Score

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

class Student {
public:
    std::string name;
    int score;
    Student(std::string n, int s) : name(std::move(n)), score(s) {}
};

int main() {
    std::vector<Student> cls{{"Hina", 88}, {"Usman", 95}, {"Zara", 72}};
    std::ranges::sort(cls, std::greater{}, &Student::score);
    for (const auto& s : cls) std::cout << s.name << ' ' << s.score << '\n';
}
Usman 95
Hina 88
Zara 72

Simple Inventory with Polymorphism

#include <iostream>
#include <memory>
#include <vector>

struct Item {
    virtual double price() const = 0;
    virtual ~Item() = default;
};
struct Book : Item { double price() const override { return 12.5; } };
struct Pen  : Item { double price() const override { return 1.5; } };

int main() {
    std::vector<std::unique_ptr<Item>> cart;
    cart.push_back(std::make_unique<Book>());
    cart.push_back(std::make_unique<Pen>());
    cart.push_back(std::make_unique<Pen>());

    double total = 0;
    for (const auto& it : cart) total += it->price();
    std::cout << "Cart total: " << total << '\n';
}
Cart total: 15.5

Exercises to Try Next

  • Extend the word counter to ignore case and punctuation.
  • Add a removeStudent function and a class average to the student program.
  • Build a to-do list app that saves tasks to a file with std::ofstream.
  • Write a template Stack<T> class backed by std::vector.

Frequently asked questions

What are good C++ projects for beginners?

A console calculator, a bank account system with classes, a tic-tac-toe game, a contact manager with file saving, or a simple text adventure.

How long does it take to learn C++?

You can learn the fundamentals in a few weeks of steady practice. Becoming comfortable with the full language and its library takes months of building real projects.

What should I learn after this C++ tutorial?

Data structures and algorithms, CMake for building projects, multithreading with std::thread, and a domain such as game development with Unreal or SFML.