Introduction to C++

Reviewed & published by Brayan K

C++ is a high-performance, compiled programming language that extends C with object-oriented features, giving programmers low-level control over memory and hardware for systems software, games, and performance-critical applications.

Part of the free C++ course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

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

💡 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

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.

// 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 powerful." << std::endl; // prints: C++ is fast and powerful.

    // return 0; tells the operating system "I finished successfully".
    return 0;   // 0 means "no error"
}

// ✅ Expected output:
//    Hello, World!
//    C++ is fast and powerful.

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.

#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.
    int maxLives = 3;   // good comments explain WHY, not WHAT
    std::cout << "Lives: " << maxLives << std::endl;   // prints: Lives: 3

    return 0;
}

// ✅ Expected output:
//    Comments are notes for humans.
//    The compiler ignores comments.
//    Lives: 3

Your turn. The program below is almost complete — fill in the three blanks marked ___ using the hints in the comments, then run it.

#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 output (example):
//    Sam
//    I am learning C++!

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.

#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 number into 'age'

    // Build one output line by chaining several << operators.
    std::cout << "Hi " << name << ", next year you'll be "
              << age + 1 << "!" << std::endl;

    return 0;
}

// ✅ Example run (you type Alice and 24):
//    Enter your name: Alice
//    Enter your age: 24
//    Hi Alice, next year you'll be 25!

Now you try. Fill in the two blanks so the program reads a city and greets it.

#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 city: London
//    Greetings from London!

Common Errors (and the fix)

Pro Tips

📋 Quick Reference

TaskCode
Include I/O library#include <iostream>
Entry pointint main() { ... return 0; }
Print a linestd::cout << "text" << std::endl;
Read inputstd::cin >> variable;
Single-line comment// note for humans
Multi-line comment/* ... */
Compile & rung++ main.cpp -o app && ./app

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.

#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 1998.

    // your code here

    return 0;
}

🎉 Lesson Complete

Practice quiz

Which header must you #include to use std::cout and std::cin?

  • <string>
  • <iostream>
  • <stdio>
  • <output>

Answer: <iostream>. <iostream> provides the input/output streams std::cout and std::cin.

Where does execution of a C++ program begin?

  • The first #include
  • The first function alphabetically
  • int main()
  • The last line of the file

Answer: int main(). int main() is the entry point — execution always starts there.

Which operator sends text to the screen with std::cout?

  • << (insertion)
  • >> (extraction)
  • = (assignment)
  • -> (arrow)

Answer: << (insertion). std::cout uses the << insertion operator to push text out to the screen.

Which operator reads keyboard input into a variable with std::cin?

  • <<
  • =
  • >>
  • &&

Answer: >>. std::cin uses the >> extraction operator to pull typed input into a variable.

What does 'return 0;' at the end of main() conventionally mean?

  • The program failed
  • The program finished successfully
  • Print zero to screen
  • Loop back to the start

Answer: The program finished successfully. By convention 0 means the program finished successfully; non-zero signals an error.

What does std::endl do?

  • Ends the program
  • Deletes the last character
  • Ends the current line (like pressing Enter)
  • Empties the variable

Answer: Ends the current line (like pressing Enter). std::endl ends the current line, like pressing Enter.

Why must you write std:: before cout (without 'using namespace std;')?

  • std:: makes it run faster
  • cout lives in the std namespace
  • It is required punctuation for every word
  • It changes cout to uppercase

Answer: cout lives in the std namespace. cout lives inside the standard library's std namespace, so its full name is std::cout.

What marks the end of a statement in C++?

  • A new line
  • A semicolon ;
  • A period .
  • A closing brace }

Answer: A semicolon ;. C++ ignores line breaks; the semicolon marks where one statement ends.

Which is a correct single-line comment in C++?

  • # a note
  • // a note
  • <!-- a note -->
  • ** a note

Answer: // a note. // begins a single-line comment; everything after it on the line is ignored.

What command compiles main.cpp into a program called app with g++?

  • g++ main.cpp -o app
  • run main.cpp app
  • g++ app -o main.cpp
  • compile main.cpp app

Answer: g++ main.cpp -o app. g++ main.cpp -o app compiles main.cpp; the -o flag names the output 'app'.

Continue this course

Frequently asked questions

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.

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.

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.

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.

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.