Introduction to C++ · Lesson 2 of 33
Your First C++ Program: iostream and Namespaces
This lesson sets up a compiler, writes your first C++ program and explains every line. You'll print with std::cout, read input with std::cin, and learn what std:: and namespaces are – including the honest truth about using namespace std;.
In this lesson
- Installing and running g++ or clang++
- Hello World explained
- Output with
coutand input withcin - Namespaces and
using
Set up a compiler
| System | How |
|---|---|
| Windows | MSYS2 (pacman -S mingw-w64-ucrt-x86_64-gcc), or WSL with Ubuntu |
| macOS | xcode-select --install (clang++), or brew install gcc for g++ |
| Ubuntu / Debian | sudo apt install build-essential |
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello
./hello
Hello, World!
#include <iostream>
int main() {
std::cout << "Hello, World!\n";
std::cout << "C++ version: " << __cplusplus << '\n';
return 0;
}
Hello, World! C++ version: 202002
Line by line
- #include <iostream>the input/output stream library (no .h in C++ headers)
- int main()where the program starts; returning 0 means success
- std::coutthe standard output stream; std:: is its namespace
- <<"insert into the stream" – chain as many as you like
- __cplusplusa number telling you which standard you're compiling with (202002 = C++20)
Output with cout
#include <iomanip>
#include <iostream>
int main() {
int qty = 3;
double price = 249.5;
std::cout << "Qty: " << qty << ", price: " << price << '\n';
std::cout << std::fixed << std::setprecision(2); // 2 decimals from now on
std::cout << "Total: " << qty * price << '\n';
std::cout << std::setw(8) << "Item" << std::setw(8) << "Cost" << '\n';
std::cout << std::setw(8) << "Pen" << std::setw(8) << 20.0 << '\n';
std::cout << std::boolalpha << (qty > 2) << '\n'; // prints true instead of 1
return 0;
}
Qty: 3, price: 249.5
Total: 748.50
Item Cost
Pen 20.00
truecout knows the type of each value, so there are no format specifiers to get wrong (unlike printf). Use '\n' for new lines; std::endl also flushes the output, which is slower in loops.
Input with cin
#include <iostream>
#include <string>
int main() {
std::string name;
int age;
std::cout << "First name: ";
std::cin >> name; // reads one word
std::cout << "Age: ";
std::cin >> age;
std::cin.ignore(); // drop the newline left after the number
std::string city;
std::cout << "City (can have spaces): ";
std::getline(std::cin, city); // reads a whole line
std::cout << name << " (" << age << ") lives in " << city << '\n';
return 0;
}
First name: Asha Age: 21 City (can have spaces): New Delhi Asha (21) lives in New Delhi
cin >> then getline
cin >> age leaves the Enter key's newline in the input. A following getline reads that empty remainder and returns immediately. Call std::cin.ignore() (or std::cin >> std::ws) first.
Namespaces
A namespace groups names so that libraries don't clash: the standard library lives in std. You can write your own:
#include <iostream>
namespace physics {
double speed(double distance, double time) { return distance / time; }
}
namespace finance {
double speed(double money, double days) { return money / days; } // same name, no clash
}
int main() {
std::cout << physics::speed(100, 9.58) << " m/s\n";
std::cout << finance::speed(3000, 30) << " rupees/day\n";
using std::cout; // bring in one name
cout << "only cout was imported\n";
return 0;
}
10.4384 m/s 100 rupees/day only cout was imported
About using namespace std;
It imports every name from std – hundreds of them – into your code, which can clash with your own names (count, distance, max…). It's fine in a short contest solution or a single .cpp file, but never put it in a header file. This course writes std::; you'll see using namespace std; in competitive-programming examples, where speed of typing matters.
Practice
Try it yourself
Read two numbers and print their sum, difference, product and average (2 decimals).
Show solution
#include <iomanip> #include <iostream> int main() { int a, b; std::cout << "Two numbers: "; std::cin >> a >> b; std::cout << "sum " << a + b << ", difference " << a - b << ", product " << a * b << '\n'; std::cout << std::fixed << std::setprecision(2) << "average " << (a + b) / 2.0 << '\n'; return 0; }Two numbers: 17 8 sum 25, difference 9, product 136 average 12.50
Quick check
What does std:: mean?
The name belongs to the std namespace – the standard library.
How do you read a full line containing spaces?
std::getline(std::cin, s).
'\n' vs std::endl?
Both end the line; endl also flushes the stream, which is slower.
Summary
- Compile with
g++ -std=c++20 -Wall -Wextra file.cpp -o name. - Print with
std::cout <<, read withstd::cin >>andstd::getline. - Namespaces prevent name clashes; avoid
using namespace std;outside small programs.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.