Why C++, and Your First Program
Why C++ is still everywhere, how to compile, and your first program with cout and cin.
Why learn C++?
C++ sits at a sweet spot that no other language quite matches: it gives you the low-level control and raw speed of C, plus high-level features like classes and a huge ready-made library. That combination is why it powers the things where performance is non-negotiable — game engines like Unreal, web browsers, databases, operating-system components, financial trading systems, and embedded devices. It's also the dominant language of competitive programming and a favourite for college placements, thanks to its speed and its brilliant Standard Template Library.
If you've done our C course, C++ will feel familiar — it grew out of C and keeps almost all of it. But C++ adds object-oriented programming and a toolbox of data structures and algorithms that would take hundreds of lines to build yourself in C. Learning C++ means learning both how computers work and how to build large, organised programs efficiently.
Compiling, the C++ way
Like C, C++ is compiled — a compiler translates your whole program into machine code before it runs, which is why C++ programs are so fast. The common compiler is g++ (part of GCC). The workflow is always: write a .cpp file, compile it into an executable, run it.
g++ hello.cpp -o hello # compile hello.cpp into a program called "hello"
./hello # run it (on Windows: hello.exe)
The playground in this lesson compiles for you, so you can run every example instantly without installing anything. When you're ready to build locally, install g++ (via MinGW on Windows, preinstalled on most Linux systems).
Your first program, line by line
Here's the classic first C++ program. Each line earns its place:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" << endl;
return 0;
}
#include <iostream> pulls in the input/output library — that's what gives us cout and cin. using namespace std; lets us write cout instead of the fuller std::cout (more on namespaces in a moment). int main() is the entry point where every C++ program begins. cout << "Hello, world!" prints text — the << arrows "stream" data to the output. endl moves to a new line. And return 0; tells the operating system the program finished successfully.
cout and cin: output and input
C++ handles input and output with streams, which read much more naturally than C's printf/scanf. You stream out with cout << and stream in with cin >>:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Next year you'll be " << age + 1 << endl;
return 0;
}
Notice you can chain several things in one cout with multiple << — text, variables, and expressions all flow together. No format specifiers like %d to remember; C++ figures out the type automatically. The arrows point the way the data moves: << sends to output, >> pulls from input.
What is a namespace?
That using namespace std; line deserves a quick explanation. As programs grow, two libraries might both define something called, say, list. A namespace prevents clashes by grouping names under a label. The standard library lives in the std namespace, so its full names are std::cout, std::cin, std::string. Writing using namespace std; says "I'll be using the std namespace, so let me skip the prefix."
For learning and small programs, using namespace std; is convenient and you'll see it everywhere. (In large professional codebases people often write std:: explicitly to avoid any ambiguity — but don't worry about that yet.) Now that you can compile, print, and read input, the next chapter covers how C++ stores data in its typed variables.
Finished "Why C++, and Your First Program"?
Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.
Was this chapter clear?
