Lesson 1 • Beginner
Introduction to C++
By the end of this lesson you'll be able to write, compile, and run a complete C++ program that prints text to the screen and reads input from the keyboard — the foundation of every C++ program you'll ever build.
What You'll Learn
- What C++ is and where it's used (games, systems, high performance)
- How compiling turns your code into a runnable program
- The anatomy of a program: #include, int main(), and return 0
- Printing text with std::cout and ending lines with std::endl
- Reading keyboard input with std::cin
- Why namespaces (std::), comments, and semicolons matter
💡 Real-World Analogy
C++ is like a manual race car. Languages such as Python are automatics — easy to drive, but the engine makes decisions for you. C++ hands you the gearstick: you control memory and squeeze out maximum speed, which is exactly why it powers games, operating systems, and anything where every millisecond counts. The trade-off is that you learn how the engine works — and this lesson is your first lap around the track.
1. What Is C++ (and Why Learn It)?
C++ is a compiled, high-performance language created by Bjarne Stroustrup in 1979. "Compiled" means your code is translated into raw machine instructions before it runs, so it executes extremely fast — far faster than languages that interpret code line by line. That speed and low-level control are why C++ sits underneath a huge amount of the software you use every day.
Where C++ runs the show
- Games & engines: Unreal Engine, most AAA titles, console software
- Operating systems: parts of Windows, macOS, and the Linux kernel
- Browsers: the core of Chrome, Firefox, and Edge
- Performance-critical systems: databases, trading platforms, robotics, embedded devices
2. How Compiling Works
A C++ file (ending in .cpp) is just text — your CPU can't run it directly. A compiler translates that text into a machine-code executable you can run. The most common free compiler is g++. On your own machine you'd save your code as main.cpp and run two commands in a terminal:
Compile, then run
g++ main.cpp -o app # compile main.cpp into a program named "app" ./app # run the program you just built
The -o app part names the output file. If you leave it off, g++ creates a default file called a.out. The good news: the editor below compiles and runs for you, so you can focus on the code itself.
3. Your First Program, Line by Line
Every C++ program has the same skeleton. #include <iostream> pulls in the input/output library so you can print and read text. int main() is the entry point — the place execution begins. Inside it, std::cout << sends text to the screen and std::endl ends the line. Read every comment in the worked example below, then run it.
Worked example: Hello, World!
Read every comment, run it, and check the output matches.
// Every C++ program needs this line to print to the screen.
#include <iostream> // gives you std::cout (output) and std::cin (input)
// main() is the ENTRY POINT — execution always starts here.
int main() {
// std::cout is the "character output" stream.
// The << operator pushes text INTO that stream, left to right.
std::cout << "Hello, World!" << std::endl; // prints: Hello, World!
// std::endl ends the line (like pressing Enter).
std::cout << "C++ is fast and powerfu
...4. Comments, Namespaces & Semicolons
Three small things carry a lot of weight. A comment (// for one line, /* ... */ for several) is a note the compiler ignores — use it to explain why. The std:: prefix is a namespace: it says "find cout inside the standard library", which keeps names from clashing in big programs. And the semicolon ends every statement, because C++ ignores line breaks and needs a marker for where one instruction stops.
Worked example: comments & std::
See single-line and multi-line comments, plus the std:: prefix in action.
#include <iostream>
int main() {
// This is a single-line comment — everything after // is ignored.
std::cout << "Comments are notes for humans." << std::endl;
/* This is a multi-line comment.
It can span as many lines as you like.
The compiler skips all of it. */
std::cout << "The compiler ignores comments." << std::endl;
// std:: is a NAMESPACE prefix. cout lives inside the "std" namespace
// (the C++ standard library), so its full name is std::cout.
...Your turn. The program below is almost complete — fill in the three blanks marked ___ using the hints in the comments, then run it.
🎯 Your turn: print two lines
Fill in the ___ blanks, then check your output against the expected lines.
#include <iostream>
int main() {
// 🎯 YOUR TURN — replace each ___ then press "Try it Yourself".
// 1) Print your name on its own line.
std::cout << ___ << std::endl; // 👉 put your name in "double quotes"
// 2) Print a second line that says you are learning C++.
std::cout << ___ << std::endl; // 👉 e.g. "I am learning C++!"
// 3) Always finish main() by returning 0.
return ___; // 👉 the success code is a single digit
}
// ✅ Expected ou
...5. Reading Input with std::cin
Output sends data out; input brings data in. std::cin reads what the user types, using the >> operator. Notice the arrows point opposite ways: std::cout << pushes text out to the screen, while std::cin >> pulls a typed value into a variable. Run the worked example, then complete the guided one.
Worked example: cin reads keyboard input
Type a name and an age when prompted, then watch the output.
#include <iostream>
#include <string> // needed to use the std::string type
int main() {
// Declare variables to hold what the user types.
std::string name;
int age;
// Prompt, then READ input with std::cin and the >> operator.
// >> points the OTHER way to << : data flows FROM cin INTO the variable.
std::cout << "Enter your name: ";
std::cin >> name; // reads one word into 'name'
std::cout << "Enter your age: ";
std::cin >> age; // reads a whole
...Now you try. Fill in the two blanks so the program reads a city and greets it.
🎯 Your turn: read and use input
Pick the right operator, then print the value back out.
#include <iostream>
#include <string>
int main() {
// 🎯 YOUR TURN — finish the two blanks.
std::string city;
std::cout << "Enter a city: ";
// 1) Read the typed word into 'city'.
std::cin ___ city; // 👉 which operator sends input INTO a variable?
// 2) Print a friendly message that includes the city.
std::cout << "Greetings from " << ___ << "!" << std::endl; // 👉 the variable name
return 0;
}
// ✅ Example run (you type London):
// Enter a cit
...Common Errors (and the fix)
- "expected ';' before ..." — you forgot a semicolon at the end of a statement. Every line of code ends with
;. Add the missing one on the line the error points to. - "'cout' was not declared in this scope" — you used
coutwithout#include <iostream>at the top. Add that include line so the compiler knows whatcoutis. - "'cout' was not declared" even with the include — you wrote
coutinstead ofstd::cout. Withoutusing namespace std;you must prefix it withstd::. - Program builds but the OS reports a non-zero exit — you forgot
return 0;at the end ofmain(). Add it so you explicitly report success. - Wrong operator: using
=instead of<<. Output uses the insertion operator:std::cout << "text", notstd::cout = "text".
Pro Tips
- 💡 Compile early and often. Write a few lines, run them, repeat. Don't write 100 lines before your first compile.
- 💡 Fix the first error first. Compiler errors list a line number; later errors are often caused by the earliest one.
- 💡 Prefer
'\n'overstd::endlwhen you don't need to flush the buffer — it's slightly faster in tight loops.
📋 Quick Reference
| Task | Code |
|---|---|
| Include I/O library | #include <iostream> |
| Entry point | int main() { ... return 0; } |
| Print a line | std::cout << "text" << std::endl; |
| Read input | std::cin >> variable; |
| Single-line comment | // note for humans |
| Multi-line comment | /* ... */ |
| Compile & run | g++ main.cpp -o app && ./app |
Frequently Asked Questions
Q: Do I really need to write std:: everywhere?
std:: tells the compiler that cout, cin and endl live in the standard library's namespace. Many tutorials add 'using namespace std;' so they can drop the prefix, but writing std:: in full is the safer habit — it avoids name clashes in larger programs, which is why professional codebases prefer it.
Q: What does compiling actually do?
Your .cpp file is plain text that the CPU can't run. A compiler such as g++ translates it into a machine-code executable. The command 'g++ main.cpp -o app' reads main.cpp and produces a program called app, which you then run with ./app.
Q: What is the difference between cout and cin?
std::cout sends data OUT to the screen using the << operator. std::cin reads data IN from the keyboard using the >> operator. The arrows point in the direction the data flows: << pushes text out, >> pulls input in.
Q: Why does every line end with a semicolon?
In C++ the semicolon marks the end of a statement, the way a full stop ends a sentence. C++ ignores line breaks, so the semicolon is how the compiler knows one instruction has finished and the next begins. Forgetting one is the most common beginner error.
Q: What does 'return 0;' mean at the end of main?
main() hands a number back to the operating system when the program ends. By convention 0 means 'finished successfully' and any other number signals an error. If you forget it, modern compilers assume return 0, but writing it makes your intent clear.
Mini-Challenge: Greeting Card
No blanks this time — just a brief and a starter outline. Build it, run it, and check your output against the example in the comments. This is exactly the kind of small program everything bigger is made of.
🎯 Mini-Challenge: build a greeting card
Declare the variables, read input with cin, and print two lines.
#include <iostream>
#include <string>
int main() {
// 🎯 MINI-CHALLENGE: Tiny greeting card
// 1. Declare a std::string 'name' and an int 'year'.
// 2. Prompt for and read each one with std::cin.
// 3. Print TWO lines:
// "Hello, <name>!"
// "You were born in <year>."
// Remember: every statement ends with a semicolon, and finish with return 0;
//
// ✅ Example run (you type Maria and 1998):
// Hello, Maria!
// You were born in 199
...🎉 Lesson Complete
- ✅ C++ is a fast, compiled language behind games, operating systems, and browsers
- ✅
g++ main.cpp -o appcompiles your code;./appruns it - ✅ Every program needs
#include <iostream>and anint main()entry point - ✅
std::cout <<prints output;std::endlends the line - ✅
std::cin >>reads keyboard input into a variable - ✅
std::is the standard-library namespace; every statement ends with; - ✅ Next lesson: Variables & Data Types — store and manipulate numbers, text, and more
Sign up for free to track which lessons you've completed and get learning reminders.