Hello World
#include <iostream>
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
Compile and run:
g++ main.cpp -o main
./main
This is the smallest C++ program shape: main starts the program, and std::cout prints to the terminal.
Variables
#include <string>
std::string name = "Dan";
int count = 1;
bool active = true;
auto language = "C++";
This shows a few common C++ value types and auto for type inference when the initializer already makes the type obvious.
Functions
#include <string>
std::string greet(const std::string& name)
{
return "Hello, " + name + "!";
}
This defines a function that takes a name and returns a greeting string. It is the usual pattern for reusable logic in C++.