Getting Started with C++
Write, compile, and run your very first program
- explain the compile-then-run model,
- recognise
#include <iostream>,int main(),std::cout,;andreturn 0;, - write and read a complete Hello World program and know its output.
1. The compile-then-run model
Unlike some languages that run your text directly, C++ works in two stages. First you write source code (a text file, usually ending in .cpp). Then a compiler reads that text and translates it into a program the computer understands. Only after that can you run the program to see its result.
If your code has a spelling or grammar mistake, the compiler will stop and show an error instead of a program. That is normal and helpful: fixing those messages is a big part of learning C++.
2. Your first program: Hello, World!
Here is the smallest useful C++ program. Type it exactly as shown:
#include <iostream>
int main() {
std::cout << "Hello, World!";
return 0;
}
Expected output:
Hello, World!
That is it! Four short lines print a message to the screen. Next we will look at what each line does.