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:
- A C++20 compatible compiler (GCC 12+ or Clang 15+)
- CMake 3.20+
- A terminal and text editor of your choice
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:
- We use
auto main() -> int(trailing return type) as the course style auto constfor local variables where possiblestd::string{"..."}brace initialisation rather than=\ninstead ofstd::endl(avoids unnecessary flush)
Compiling and running
g++ -std=c++20 -Wall -Wextra -Werror -o hello hello.cpp
./hello
You should see:
Hello, COMP6771!
The flags we use:
-std=c++20enables the C++20 standard-Wall -Wextraturns on most useful warnings-Werrortreats warnings as errors (get used to this!)
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:
- Value semantics and the type system
- STL containers and iterators
- Move semantics and smart pointers
- Templates and generic programming
- Custom iterators and ranges
See you in Tutorial 1!