C++: Introduction
Learn what C++ is, where it came from, and how to write and run your first program.
What is C++ and its History
C++ is a general-purpose, compiled programming language created by Bjarne Stroustrup at Bell Labs in 1979 as an extension of C. It introduced object-oriented features while keeping C's low-level performance, making it one of the most widely used languages today.
- Originally called "C with Classes", renamed C++ in 1983
- Standardized by ISO: C++98, C++11, C++14, C++17, C++20, C++23
- Combines low-level memory control with high-level abstractions
C++
A statically typed, compiled language that supports procedural, object-oriented, and generic programming.
- Compiled: source code is translated to machine code before running
- Statically typed: variable types are known at compile time
- Multi-paradigm: supports OOP, procedural, and functional styles
- Standard library provides containers, algorithms, and I/O utilities
C++ vs C vs Other Languages
C++ extends C by adding classes, templates, exceptions, and the Standard Template Library (STL). Compared to higher-level languages like Python or Java, C++ gives you direct memory control at the cost of more complexity.
Language Comparison
Choose the right tool: C++ gives maximum performance and control; higher-level languages trade performance for simplicity.
- C vs C++: C++ adds OOP and STL on top of C
- C++ vs Java: C++ compiles to native code; Java runs on a virtual machine
- C++ vs Python: C++ is faster but requires explicit memory management
- C++ is preferred when performance and hardware access matter most
| Feature | C | C++ | Python |
|---|---|---|---|
| OOP Support | No | Yes | Yes |
| Memory Control | Manual | Manual | Automatic (GC) |
| Execution Speed | Very Fast | Very Fast | Slower |
| Learning Curve | Moderate | Steep | Gentle |
| Use Case | OS, Embedded | Games, Systems | Scripting, AI |
Applications of C++
C++ powers software where performance and hardware proximity are critical, from operating system kernels to AAA game engines.
- Operating systems and kernels (Windows, parts of Linux)
- Game engines (Unreal Engine, id Tech)
- Embedded systems and firmware (microcontrollers, IoT devices)
- Browsers and runtimes (Chrome V8, Firefox SpiderMonkey)
- High-frequency trading and financial systems
- Compilers and interpreters (Clang, Python's CPython internals)
Where C++ Excels
Use C++ when your program needs to run fast, use little memory, or interact directly with hardware.
- Systems programming: OS, drivers, firmware
- Game development: real-time rendering, physics engines
- Embedded: bare-metal microcontroller code
- Performance-critical apps: databases, browsers, compilers
Setting Up Your Development Environment
You need a text editor or IDE and a C++ compiler. The most beginner-friendly options are VS Code with the C++ extension or Code::Blocks with a bundled compiler.
IDE Options
Pick an IDE that installs or integrates a compiler so you can compile and run programs immediately.
- VS Code: lightweight editor, add the C/C++ extension by Microsoft
- Code::Blocks: beginner-friendly IDE with GCC bundled on Windows
- Visual Studio: full-featured IDE with MSVC compiler (Windows)
- CLion: powerful JetBrains IDE with CMake integration
- On Linux/Mac, GCC or Clang is usually pre-installed or available via package manager
Compilers: GCC, Clang, MSVC
A compiler translates your C++ source code into a native executable. The three major compilers are GCC, Clang, and MSVC, all follow the C++ standard but have different strengths.
C++ Compilers
A compiler checks your code for errors and converts it to machine code your CPU can execute directly.
- GCC (g++): default on Linux, highly standard-compliant, open source
- Clang (clang++): fast compilation, excellent error messages, macOS default
- MSVC (cl.exe): Microsoft compiler, ships with Visual Studio
- All three are free; GCC and Clang run on all major platforms
Writing Your First Program
Every C++ program starts with a main() function. The program below prints a greeting to the console using cout.
Program Structure
A minimal C++ program needs one header, a namespace declaration, and a main() function that returns 0.
- #include <iostream>: includes input/output stream support
- using namespace std; lets you write cout instead of std::cout
- int main(): entry point; the OS calls this when the program starts
- return 0; signals successful execution to the operating system
Hello, World!
C++The minimal C++ program: include a header, write to cout, return 0.
Basic Input and Output
Use cout to print output and cin to read input from the user. Both live in the <iostream> header.
cout and cin
cout streams data to standard output; cin reads data from standard input (the keyboard).
- cout << value: outputs value to the terminal
- cin >> variable: reads the next whitespace-delimited token into variable
- endl: flushes the buffer and moves to the next line
- Use getline(cin, str) to read a full line including spaces
Greeting the User
C++Read a name from stdin and print a personalized greeting.
The Compilation Process
Turning source code into a runnable program happens in three stages: preprocessing, compilation, and linking.
Compilation Stages
Understanding each stage helps you read error messages and know where a problem occurred.
- Preprocessor: handles #include and #define, produces a single translation unit
- Compiler: checks syntax and semantics, outputs an object file (.o / .obj)
- Linker: combines object files and libraries into the final executable
- Compile-time errors come from the compiler; linker errors come from the linker
| Stage | Tool | Input | Output |
|---|---|---|---|
| Preprocessing | cpp / compiler front-end | .cpp source | Expanded source |
| Compilation | g++ / clang++ | Expanded source | .o object file |
| Linking | ld / linker | .o files + libs | Executable |
Understanding Errors
Errors fall into two main categories: compile-time errors (caught before the program runs) and runtime errors (occur while the program is executing). Learning to read error messages is a core skill.
Error Types
Compile-time errors prevent the executable from being created; runtime errors crash or misbehave during execution.
- Syntax error: code violates grammar rules (missing semicolon, mismatched braces)
- Type error: incompatible types used together (assign string to int)
- Linker error: function declared but never defined
- Runtime error: divide by zero, null pointer dereference, out-of-bounds access
- Read the error line number and message first before changing code at random
Avoiding Common Errors
C++Guard against both compile-time mistakes and runtime pitfalls.
Knowledge Check
1. Who created C++?
2. Which of the following is a C++ compiler?
3. What does the preprocessor do during compilation?
4. Which error occurs while the program is running?
5. What is the entry point of every C++ program?
6. Which header is required to use cout and cin?