MyWhiteBoard TrainingHome

PRACTICAL C++ COURSE

C++ Foundations to Modern Practice

Build the habits needed to read, write, test, and improve C++ programs. Each module pairs a focused concept with code you can run in Open Editor.

What you will learn

  • Compile the structure of a C++ program: headers, main, statements, and return values.
  • Use types, conditions, loops, functions, and standard input/output with confidence.
  • Model data with classes while protecting object invariants through encapsulation.
  • Prefer RAII, standard containers, and smart pointers over manual memory management.

How to use this course: read a module, type the example rather than pasting it, then change one line and predict the new result before running it.

1. Core language basics

Program shape and output

C++ begins execution at main. Include only the headers you use; <iostream> provides std::cout for output.

#include <iostream>

int main() {
    const int lessons = 6;
    std::cout << "C++ lessons: " << lessons << '\n';
    return 0;
}

Expected output: C++ lessons: 6.

Values, decisions, and loops

Use a meaningful type for each value. Prefer const when a value should not change, and keep conditions direct.

#include <iostream>

int main() {
    for (int number = 1; number <= 5; ++number) {
        if (number % 2 == 0) {
            std::cout << number << " is even\n";
        }
    }
}

2. Functions and classes

Functions express one job

Pass read-only objects by const reference when copying would be unnecessary. Return a value when the caller needs a result.

#include <iostream>
#include <string>

std::string greeting(const std::string& name) {
    return "Hello, " + name + "!";
}

int main() {
    std::cout << greeting("Learner") << '\n';
}

Classes keep data valid

Keep representation details private and expose operations that preserve rules. This is more durable than exposing fields for any code to modify.

#include <iostream>

class Counter {
public:
    void increment() { ++value_; }
    int value() const { return value_; }

private:
    int value_ = 0;
};

int main() {
    Counter counter;
    counter.increment();
    std::cout << counter.value() << '\n';
}

3. Resource safety and modern C++

C++ objects should manage their own resources. This principle is called RAII: acquire a resource during initialization, release it automatically when the owning object leaves scope.

#include <iostream>
#include <memory>

int main() {
    auto score = std::make_unique<int>(100);
    std::cout << *score << '\n';
} // score is released automatically here

Rule of thumb: use local objects and std::vector first. Use std::unique_ptr for exclusive dynamic ownership, std::shared_ptr only when ownership is truly shared, and avoid raw new/delete in new code.

4. Standard library containers and algorithms

The Standard Template Library provides well-tested containers and algorithms. Prefer them to hand-written arrays and loops where they make intent clearer.

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

int main() {
    std::vector<int> scores {72, 95, 81, 88};
    std::sort(scores.begin(), scores.end());

    for (int score : scores) {
        std::cout << score << ' ';
    }
    std::cout << '\n';
}

Expected output: 72 81 88 95. Learn vector, string, map, and algorithms such as sort, find, and count_if before building custom equivalents.

Practice plan

Exercise 1: Temperature report

Store five temperatures in a std::vector, print each value, and calculate the average. Then reject an empty vector before division.

Exercise 2: Library book

Create a Book class with title, author, and checked-out status. Add methods to check out and return a book without exposing the status field.

Exercise 3: Score finder

Use std::find to determine whether a target score exists in a vector. Print a clear message for both outcomes.

Run this program in Open Editor

Before you move on

  • I can compile the basic structure of a program.
  • I use const for values that do not change.
  • I can explain when to pass by value or const reference.
  • I can create a class with private data and public behavior.
  • I choose standard containers before manual dynamic arrays.
  • I avoid manually pairing new and delete.

Continue with the existing advanced reference material for encapsulation, templates, containers, smart pointers, and operator overloading.

More lessons are being prepared: topic links in the course contents currently lead here until each dedicated lesson is published.