Basics & Setup

Introduction to C++ Programming

DIFFICULTY: Beginner READ TIME: 10 mins

Hey! Welcome to the visual guide. C++ is basically a beast of a general-purpose programming language. It was kicked off by Bjarne Stroustrup back in 1979 at Bell Labs. You can think of it as the parent language for a ton of high-performance modern tech you use daily.

Sometimes people call it a “superset of C”, which basically means it takes C and gives it OOP superpowers. If you need absolute performance and control over computer memory—like in modern 3D games, robotics, operating systems, or high-frequency trading—C++ is your go-to.

History and Evolution of C++

C++ didn’t start with the name C++. Originally, Bjarne named it “C with Classes”. The name tells you exactly what it did: it took the classic, fast, low-level C language and added Object-Oriented Programming (OOP) concepts like classes and inheritance to it.

Over the last few decades, it has evolved a lot. Here are some of the key milestones in its timeline:

  • 1979: Bjarne Stroustrup, a computer scientist from Denmark, started working on “C with Classes”.
  • 1982: The language officially got its name C++ (incrementing C) and introduced virtual functions, overloading, and references.
  • 1984: The first stream input/output library was introduced.
  • 1985: Bjarne published the first edition of The C++ Programming Language.
  • 1989: C++ 2.0 launched, bringing multiple inheritance and abstract classes to the mix.
  • 1998: Standardized internationally as C++98.
  • 2020: C++20 was finalized, introducing modules, concepts, and coroutines.
  • 2024: C++23 was officially published, further refining the standard library and syntax.

Key Features of C++

C++ lives in the sweet spot between low-level control and high-level abstractions, making it a powerful middle-level language. Here are the features you’ll love:

  • Object-Oriented Programming (OOP): Let’s organize code using classes, objects, inheritance, polymorphism, and encapsulation.
  • Standard Template Library (STL): A huge collection of pre-built data structures (vectors, maps, lists) and algorithms that saves you from writing sorting algorithms from scratch.
  • Cross-Platform: Write your code once, compile it, and run it on Windows, macOS, or Linux.
  • Manual Memory Control: Direct control over memory allocation and deallocation (essential for performance critical tasks).
  • Speed: C++ compiles straight down to machine code, making it incredibly fast.

Applications of C++

Because it is so fast and efficient, C++ is the foundation for systems where performance is non-negotiable:

  • Operating Systems: Windows, macOS, and Linux have large parts written in C++.
  • Game Development: Major game engines like Unreal Engine are built entirely on C++.
  • Embedded Systems & Robotics: Smart devices, autonomous cars, and humanoid robotics run on C++.
  • Web Browsers: Engines behind Google Chrome (Blink) and Mozilla Firefox (Gecko) use C++.
  • Databases: High-performance storage engines like MySQL and MongoDB are written in C++.

Comparison with Other Languages

Here is a quick look at how C++ stacks up against Python and Java:

FeatureC++PythonJava
Execution SpeedVery FastSlower (Interpreted)Moderate (JIT/JVM)
CompilationRequired (Direct to Native)InterpretedCompiled to Bytecode
Memory ManagementManual (High Control)Automatic (Garbage Collected)Automatic (Garbage Collected)

Why Learn C++?

Learning C++ helps you solve complex, resource-limited problems. Here is why it’s worth it:

  • Low-Level Control: You get to understand how computer memory, pointers, and memory addresses actually work under the hood.
  • Evergreen Demand: Even with new languages appearing constantly, C++ remains critical for core system infrastructures.
  • Deep Mental Models: Understanding C++‘s compilation stages, pointers, and stack vs. heap allocation makes you a better developer in any language.

Setting Up Your Environment

To run C++ programs locally, you need a compiler and a text editor/IDE. Here are your options:

Option 1: Run C++ Online

If you want to test your first program instantly without installing anything, use an online compiler:

Option 2: Install C++ Locally

For a professional development setup, follow these steps:

  1. Text Editor: Download and install VS Code.
  2. Compiler (MinGW for Windows):
    • Download the MinGW installer.
    • Check/select the gcc-g++ package (this compiles your C++ files).
    • Add the MinGW bin directory path to your system Environment Variables (PATH).
  3. C++ Extensions: Open VS Code, head over to the Extensions tab (Ctrl+Shift+X), and install the C/C++ extension pack by Microsoft.

Once you have installed the compiler and IDE, you are ready to write code!

Your First C++ Program

Let’s write a simple program to print “Hello, striverA2Z!” to your terminal.

  1. Open VS Code.
  2. Create a new file and save it as hello.cpp.
  3. Copy and paste the following code block:
#include <iostream>

int main() {
    std::cout << "Hello, striverA2Z!" << std::endl;
    return 0;
}

Compiling and Running

To compile and run your code, open your terminal in the folder where you saved the file and execute:

# Compile hello.cpp into an executable binary named hello
g++ hello.cpp -o hello

# Run the executable
./hello

Expected Output

Once run, you will see the output printed:

Hello, striverA2Z!

Basic Program Structure

Let’s dissect the simple code you wrote above. A typical C++ program is split into two parts:

1. Headers & Libraries

Headers let you include external libraries. For instance, #include <iostream> tells the compiler to import the standard Input/Output Stream library so we can write to the terminal.

#include <iostream>

We use angle brackets < > for standard libraries, and double quotes " " for custom local header files.

2. The Main Function

The compiler starts executing your program inside the main function. It’s the entry point of your code:

int main() {
    // Code to run goes here
    return 0;
}

The return 0; at the end tells the operating system that your program finished executing successfully with no errors.

How the C++ Compiler Works

When you compile your code, it doesn’t just happen in one single leap. The compiler toolchain processes your code in stages before producing the final runner. Here is the process:

flowchart TD
    Source[Source Code: hello.cpp] --> Preprocessor[Preprocessor]
    Preprocessor --> Expanded[Expanded Code]
    Expanded --> Compiler[Compiler]
    Compiler --> Object[Object Code: hello.o]
    Object --> Linker[Linker]
    Libs[Standard Libraries] --> Linker
    Linker --> Executable[Executable: hello.exe]
    
    style Source fill:#e5eeff,stroke:#0058be,stroke-width:2px
    style Object fill:#d3e4fe,stroke:#0058be,stroke-width:2px
    style Executable fill:#d4e3ff,stroke:#0060ac,stroke-width:3px

Let’s break down exactly what happens in each compiler phase:

Preprocessor

The preprocessor scans your code first. It looks for lines starting with a # symbol (like #include and #define). If it finds #include <iostream>, it goes and fetches the library’s contents and pastes them directly into a temporary file.

Compiler

The compiler takes the expanded code and translates it into Assembly/Object code (saved as .o or .obj files). This is machine code, but it isn’t executable yet because it contains placeholder tags for external library code that it doesn’t know about.

Linker

The linker resolves those placeholders. It takes all your .o files and stitches them together with the binary library code (like iostream) to assemble a single, runnable binary executable (e.g. hello.exe on Windows).

Future of C++

C++ isn’t going anywhere. Because of the explosive growth in self-driving vehicles, high-end game development, and high-performance computing, the demand for developers who can manage resources manually is extremely high. Mastering C++ will make you a highly sought-after systems architect.

Frequently Asked Questions

Who developed C++, and what was its original name?

Bjarne Stroustrup developed it in 1979 at Bell Labs. Its original name was “C with Classes” before it was renamed C++ in 1982.

Why is C++ called a middle-level language?

Because it gives you low-level access (like pointer arithmetic and manual memory addresses) alongside high-level abstractions (like Object-Oriented Classes and Templates).

How do I verify if my C++ compiler was installed correctly?

Open your terminal and run g++ --version (or gcc --version). If it prints details about the GCC version installed, you’re all set!

quiz Test Your Understanding

Who developed the C++ programming language?