Installing a C++ Compiler
You need a C++ compiler like g++ (GCC), clang++, or MSVC. Most systems can install one easily.
Example
# Check if g++ is installed
g++ --version
# macOS (includes clang++)
xcode-select --install
# Ubuntu/Debian
sudo apt install g++
# Windows: Install MinGW-w64 or use Visual Studio
# Compile and run
g++ -o program main.cpp
./program
# With C++17 standard
g++ -std=c++17 -o program main.cpp Program Structure
Every C++ program follows a standard structure with preprocessor directives, namespaces, and the main function.
Example
#include <iostream> // preprocessor directive
#include <string>
using namespace std; // use standard library without std::
// Function declaration
void greet(string name);
// Main function — entry point
int main() {
greet("Alice");
return 0; // 0 means success
}
// Function definition
void greet(string name) {
cout << "Hello, " << name << "!" << endl;
} 