Before you can write C++ code, you need a way to compile and run it. Here is what you need:
A compiler โ the tool that turns your code into an executable program. The most popular one is g++ (part of the GCC compiler collection). On Windows, you can install MinGW or use Visual Studio. On Linux, g++ is usually a single command away. On macOS, you can install Xcode Command Line Tools.
An IDE โ an editor with C++ support makes life easier. Visual Studio, Code::Blocks, CLion, or even VS Code with the right extensions all work great. But honestly, you can start with any text editor and the command line.
Your First C++ Program
Let us start with a simple program. Create a file called hello.cpp and
write this:
#include <iostream>
int main() {
std::cout << "Welcome to C++!";
return 0;
}
To compile this on the command line, run: g++ hello.cpp -o hello.
Then run the executable: ./hello (or hello.exe on Windows).
You should see "Welcome to C++!" printed out.
Program Structure
Every C++ program follows this basic structure. The #include lines bring in
libraries. The main() function is where execution begins. Statements end
with semicolons. It is simple once you get used to it.
#include <iostream>
int main() {
std::cout << "C++ is pretty cool, right?";
std::cout << "Keep going, you will love it.";
return 0;
}