Skip to content

Your First Program

You are going to build and run a real program from this repository, understand every line of it, and then deliberately break it. The last part is the important one: an error you caused on purpose is an error you will recognise later.

Where this file lives

The program below is the repository's starter file, examples/playground/hello.cpp. You can also find it under examples/ in the repository alongside its captured output. Work on your own copy — for example in scratch/, which is excluded from version control — so the reference copy stays exactly as the course describes it.


Step 1 — The source

// examples/playground/hello.cpp
//
// Build:  g++ -std=c++17 -Wall -Wextra hello.cpp -o hello
// Run:    ./hello        (Linux/macOS)   or   hello.exe   (Windows)

#include <iostream>
#include <string>

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

int main() {
    const std::string name = "Object-Oriented Programming";

    std::cout << greeting(name) << '\n';
    std::cout << "You are running a C++17 program built from source.\n";

    return 0;
}

Step 2 — Compile it

Save the code as hello.cpp, open a terminal in that folder, and run:

g++ -std=c++17 -Wall -Wextra hello.cpp -o hello

There is no output if it worked. That is normal: a successful compile is silent. On Windows with MSYS2 or WSL, the same command produces hello.exe.

Step 3 — Run it

./hello

On Windows, if ./hello does not work, try ./hello.exe or hello.exe.

Expected output:

Hello, Object-Oriented Programming!
You are running a C++17 program built from source.

If you see exactly that, your toolchain is working and you have built the course's first program from source. Do not move on until this works — everything else depends on it.


Step 4 — Understand every line

Read the program with the comments below. Nothing here is magic; each line has a precise job.

#include <iostream>   // brings in std::cout, the standard output stream
#include <string>     // brings in std::string, the standard text type

std::string greeting(const std::string& name) {
    // Returns a new string built by joining the pieces.
    // `const std::string&` means: refer to the caller's string without copying
    // it, and promise not to modify it. Copying would work too, but this is the
    // habit the course expects for anything larger than a few bytes.
    return "Hello, " + name + "!";
}

int main() {          // execution starts here — every C++ program has one main
    const std::string name = "Object-Oriented Programming";
    // `const` says this value will never change after initialisation. The
    // compiler will refuse any later attempt to modify it, which turns a whole
    // class of mistakes into compile-time errors instead of bugs.

    std::cout << greeting(name) << '\n';
    // `<<` pushes things into the output stream, left to right:
    //   greeting(name)  the string returned by the function
    //   '\n'            a newline character  (print "\n" or std::endl too —
    //                   they differ in performance, not in the visible result)

    std::cout << "You are running a C++17 program built from source.\n";

    return 0;         // 0 means "the program ended successfully"
}

Three ideas in this twenty-line program already shape the whole course:

  1. Data and behaviour are separated here — and Module 2 will fix that. greeting is a free function that takes a string. Once you can write classes, a person object will be able to greet itself, and the data will stop being passed around separately from the code that operates on it.
  2. const and & are not decoration. They express intent the compiler enforces. Both are used from the first week of this course, not saved for the end.
  3. std:: is a namespace. The standard library lives in a namespace called std to avoid colliding with names you write yourself. Module 1 explains why, and why using namespace std; is discouraged in headers.

Step 5 — Break it on purpose

Now the useful part. Make three deliberate mistakes, one at a time, and look at what the compiler says. Fix each one before trying the next.

Messages vary, structure does not

The exact wording and column numbers differ between GCC, Clang and MSVC, and between versions of each. The structure below — file, line, error:, and a plain-language explanation — is what every compiler gives you.

Break 1 — remove a semicolon

Delete the ; at the end of the line:

std::cout << "You are running a C++17 program built from source.\n"

You will get something like:

hello.cpp: In function 'int main()':
hello.cpp:19:5: error: expected ';' before 'return'

Two things to notice. The message names the file, line, and column — and it points at line 19, the next statement, not at line 17 where the semicolon is missing. The compiler only discovered the problem when it reached the following token. When an error's location looks strange, check the line above it.

Break 2 — misspell a name

Change std::cout (line 16) to std::cour:

hello.cpp:16:10: error: 'cour' is not a member of 'std'

The compiler is telling you that std is fine but cour is not something it knows about — a typo, in almost every case.

Break 3 — forget a header

Delete the line #include <iostream> (line 6):

hello.cpp:16:10: error: 'cout' was not declared in this scope
hello.cpp:16:10: note: 'std::cout' is defined in header '<iostream>'; did you mean to '#include <iostream>'?

The stream is not missing from the language; it is simply not visible until you include the header that declares it. Notice that recent compilers do not merely complain — they tell you the fix. Remember this one: it is the single most common first-week error.

Rule of thumb

The compiler's wording is often unhelpful, but its location and order are reliable. Fix the first error, rebuild, and ignore the rest: one genuine mistake frequently produces a cascade of follow-on messages.

Full catalogue of the errors you will meet: Reading Compiler Errors.


Step 6 — Experiment

Change one thing at a time and predict the result before you compile. Being wrong is the point.

  • Change the name to your own.
  • Change const std::string& to std::string (remove both const and &) and confirm the program still works — then find out what it costs, by reading the const-reference discussion in Module 2.
  • Add a second call to greeting() and print both results.
  • Remove const from name, then try to modify name later; observe the error.
  • Remove return 0; from main and see whether it still compiles. (It does — main is special — but now you know why.)

Next: Reading Compiler Errors · Then: Study Plan · Or begin: Module 1