Introduction to C++
If you have ever wondered how games like GTA V render millions of polygons per second, how trading firms in Mumbai execute thousands of stock orders in microseconds, or how the Mars rover firmware was written, the answer is almost always C++ (or C, its close ancestor). C++ is not a beginner's toy — it is the language professionals reach for when performance, control, and reliability are non-negotiable.
This chapter gives you the full picture: where C++ came from, why it still matters in 2026, how it differs from C, and how a simple program travels from a .cpp text file all the way to a running executable.
A Brief History of C++
In the late 1970s, a Danish computer scientist named Bjarne Stroustrup was working on his PhD at Cambridge. He was using a language called Simula for simulating network behaviour, but he found it too slow for serious systems work. He then turned to C, which was fast but lacked the ability to model real-world entities cleanly.
His solution: take C and add classes — a way to bundle data and behaviour together. The result was initially called "C with Classes," introduced in 1979. In 1983 the name was officially changed to C++, where ++ is the C increment operator — a programmer's way of saying "one step beyond C."
Key milestones in C++ history:
| Year | Milestone |
|---|---|
| 1979 | Bjarne Stroustrup begins "C with Classes" at Bell Labs |
| 1983 | Renamed to C++ |
| 1985 | First commercial release, Cfront 1.0 |
| 1998 | First ISO standard — C++98 |
| 2003 | Bug-fix release — C++03 |
| 2011 | Major modernisation — C++11 (lambdas, auto, smart pointers) |
| 2014 | C++14 (generic lambdas, relaxed constexpr) |
| 2017 | C++17 (structured bindings, std::optional, std::filesystem) |
| 2020 | C++20 (concepts, ranges, coroutines, modules) |
| 2023 | C++23 (further library improvements, std::expected) |
Today, C++ is standardised by ISO/IEC and its compiler implementations — GCC, Clang, and MSVC — are among the most sophisticated pieces of software ever written.
Why C++ Is Still Relevant in 2026
Some students ask: "Python is so easy — why learn C++?" The honest answer is that C++ and Python solve different problems. Here is where C++ reigns:
1. Competitive Programming
Every serious competitive programmer on platforms like Codeforces, LeetCode, and AtCoder uses C++. The reason is speed: C++ programs typically run 10–100× faster than equivalent Python programs. When a problem gives you a time limit of 1 second and your solution must handle 10^6 operations, Python will time out but C++ will not. Topcoders from IITs and NITs who clear ICPC regionals almost universally code in C++.
2. FAANG and Product Company Interviews
At Google India, Microsoft Hyderabad, Amazon Bengaluru, and similar companies, interviewers accept any language — but candidates who write C++ demonstrate low-level understanding that stands out. Many roles in infrastructure, compilers, and machine-learning systems explicitly require C++ proficiency, with compensation packages ranging from ₹30 LPA to ₹1.5 Cr+ for senior engineers.
3. Game Engines
Unreal Engine, the technology behind Fortnite, is written entirely in C++. Unity's core runtime is C/C++. Game studios worldwide hire C++ engineers because real-time rendering at 60+ FPS demands direct hardware control.
4. Systems and Embedded Software
Operating system kernels, device drivers, firmware for routers and IoT devices, and real-time control software for industrial machines are predominantly written in C or C++. Companies like Texas Instruments, Qualcomm, and semiconductor firms in Bengaluru's electronics district actively hire C++ embedded engineers.
5. High-Frequency Trading (HFT) and Finance
Banks and trading desks — from Goldman Sachs to smaller prop-trading firms on Dalal Street — rely on C++ for order-matching engines and risk systems where a microsecond of latency can mean crores of rupees in profit or loss.
6. Machine Learning Infrastructure
While you write model code in Python, the engines underneath — TensorFlow, PyTorch — are C++ with Python wrappers. NVIDIA's CUDA GPU kernels are written in a C++ dialect. If you want to optimise AI systems at the metal, C++ is unavoidable.
C vs C++: What Changed?
C++ was designed to be backward-compatible with C, meaning almost all valid C code also compiles as C++. But C++ adds a great deal on top:
| Feature | C | C++ |
|---|---|---|
| Object-Oriented Programming | No | Yes (classes, inheritance, polymorphism) |
| Function Overloading | No | Yes |
| Templates (generic programming) | No | Yes |
| References | No | Yes |
bool type | No (use int) | Yes |
| Standard library (STL) | Limited (libc) | Rich (vectors, maps, algorithms, …) |
| Exception Handling | No | Yes (try/catch/throw) |
| Namespaces | No | Yes |
new/delete operators | No (use malloc/free) | Yes |
| Smart Pointers | No | Yes (unique_ptr, shared_ptr) |
const correctness | Partial | Fully enforced |
Think of C as a high-performance sports car with manual controls and no safety systems, and C++ as the same engine wrapped in a body that also provides airbags (type safety, RAII) and a better dashboard (STL).
The C++ Compilation Model
Unlike Python, which is interpreted line-by-line at runtime, C++ is a compiled language. Before your program can run, it must be translated into machine code that the processor understands directly. The journey looks like this:
Source Code (.cpp)
|
v
Pre-processor
(handles #include, #define, macros)
|
v
Compiler (e.g., g++)
(translates to assembly / object code)
|
v
Object File (.o)
|
v
Linker
(combines object files + libraries)
|
v
Executable Binary
Pre-processor: Reads your source file and processes all lines that start with #. When it sees #include <iostream> it literally copies the contents of the iostream header file into your source before compilation begins.
Compiler: Reads the pre-processed source, checks for syntax errors, and emits an object file (.o or .obj). The object file contains machine code but is not yet a complete program — it may reference functions defined elsewhere.
Linker: Combines one or more object files with the standard library (and any other libraries you specified) to produce the final executable. On Linux this is an ELF binary; on Windows, a .exe.
This model means that a C++ error is caught before you ever run the program, unlike runtime errors in interpreted languages. In a team setting it also means you can compile each source file independently and link them together — a huge advantage for large codebases.
Your First C++ Program: Hello, World!
Let us write, compile, and run the classic program. Create a file called hello.cpp and type:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Line-by-Line Explanation
#include <iostream>
This is a pre-processor directive. It tells the pre-processor to include the standard input/output stream header, which defines std::cout (console output) and std::cin (console input). Without this line, the compiler would not know what cout is.
int main()
Every C++ program must have exactly one main function. It is the entry point — the first function the operating system calls when you run your program. int means the function returns an integer to the OS.
{ and }
Curly braces delimit a block of code — in this case, the body of the main function. Everything between them belongs to main.
std::cout << "Hello, World!" << std::endl;
std::cout— the standard character output stream (your terminal).<<— the stream insertion operator. Think of it as "send this data into the stream.""Hello, World!"— a string literal. The double quotes mark the start and end of the text.std::endl— end-of-line: prints a newline and flushes the output buffer so the text appears immediately.- The
;at the end is mandatory — every statement in C++ ends with a semicolon.
return 0;
Returns the integer 0 to the operating system, conventionally meaning "the program finished successfully." A non-zero return value signals an error.
Compiling and Running
Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and run:
// This is a terminal command, not C++ code:
// g++ -std=c++17 -o hello hello.cpp
// ./hello
For the actual terminal commands (outside C++ files):
g++ -std=c++17 -o hello hello.cpp
./hello
You should see:
Hello, World!
That is it — you have compiled and run your first C++ program.
Reading Input: A Slightly More Interactive Program
Let us extend the example to read the user's name:
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Hello, " << name << "! Welcome to C++." << std::endl;
return 0;
}
Here we include <string> for the std::string type and use std::cin >> to read a word from the keyboard into the variable name.
The using namespace std Shortcut
You may see tutorials that begin with:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
The line using namespace std; tells the compiler: "Every time I write cout, look for it in the std namespace so I do not have to type std:: repeatedly." This is convenient for small programs and competitive programming, but in large professional codebases it is discouraged because it can cause name clashes. Throughout this tutorial series, we will write std:: explicitly in explanatory examples and mention where using namespace std; is acceptable.
Common Pitfalls
Forgetting the semicolon. Every statement must end with ;. The error message will say something like "expected ';' before '}'" and point to the line after the missing semicolon — which confuses beginners.
Mixing up = and ==. A single = is assignment; == is comparison. Writing if (x = 5) assigns 5 to x and is always true, which is a subtle logic bug. Enable compiler warnings (-Wall) to catch this.
Missing #include. Using std::cout without #include <iostream> produces a cryptic "identifier not found" error. Always check your includes first.
Case sensitivity. C++ is case-sensitive. Main, MAIN, and main are three different names. Only lowercase main is the valid entry point.
Comparing using namespace std; scope. Placing using namespace std; inside a function is safer than at the global (file) level — it limits the scope of the shortcut and reduces collision risk.
Practice Exercises
-
Write a C++ program that prints your full name, your college name, and your target company — each on a separate line.
-
Modify the greeting program so that it asks for the user's first name and last name as two separate inputs, then prints "Welcome, [FirstName] [LastName]!".
-
Look up what the pre-processor directive
#define PI 3.14159does. Add it to a program and print the value ofPIusingstd::cout. -
Compile the Hello World program with the flag
-Walland observe the output. What warnings (if any) appear? Research what-Wallenables. -
Visit Compiler Explorer (godbolt.org) and paste your Hello World program. Change the compiler to GCC 13 with
-O2. Observe the assembly output and notice which instructions correspond to thecoutcall.
Summary
- C++ was created by Bjarne Stroustrup in 1983 by extending C with object-oriented features.
- It remains the language of choice for competitive programming, game engines, embedded systems, HFT, and ML infrastructure.
- C++ adds classes, templates, the STL, references, exceptions, and smart pointers on top of C.
- A C++ program goes through pre-processing → compilation → linking before producing an executable.
- The mandatory entry point is
int main(), which returns0on success. std::couthandles output,std::cinhandles input; both require#include <iostream>.- Always end statements with
;, and use==for comparison, not=.