Skip to content
Zhewen Shen
Go back

[COMP6771][Tutorial 0] Hello World

Table of contents

Open Table of contents

Welcome

Welcome to COMP6771 Advanced C++ Programming! This tutorial will help you set up your environment and write your first C++ program.

Setting up your environment

You will need:

On CSE machines, these are already available. To check your compiler version:

g++ --version

Your first C++ program

Create a file called hello.cpp:

#include <iostream>
#include <string>

auto main() -> int {
    auto const name = std::string{"COMP6771"};
    std::cout << "Hello, " << name << "!\n";
    return 0;
}

A few things to note:

Compiling and running

g++ -std=c++20 -Wall -Wextra -Werror -o hello hello.cpp
./hello

You should see:

Hello, COMP6771!

The flags we use:

Building with CMake

In this course, we use CMake for building. A minimal CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(hello)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(hello hello.cpp)

Then build with:

cmake -B build
cmake --build build
./build/hello

A taste of modern C++

C++ has evolved significantly. Here is a preview of some features we will cover throughout the course:

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

auto main() -> int {
    auto const numbers = std::vector{3, 1, 4, 1, 5, 9, 2, 6};

    // Ranges: filter and transform without manual loops
    auto evens_doubled = numbers
        | std::views::filter([](int n) { return n % 2 == 0; })
        | std::views::transform([](int n) { return n * 2; });

    for (auto const val : evens_doubled) {
        std::cout << val << " ";
    }
    std::cout << "\n";
    // Output: 8 4 12
}

Don’t worry if this looks unfamiliar. By the end of the course, this will be second nature.

What’s next

In the upcoming tutorials, we will cover:

See you in Tutorial 1!


Share this post on: