Chapter 1: Introduction to C++

Welcome to the world of C++. If C is the foundation of modern computing, C++ is its powerful, evolved successor. Developed by Bjarne Stroustrup in 1979 as an extension of C, C++ introduces the paradigm of Object-Oriented Programming (OOP). While it retains the speed and low-level memory access of C, it provides higher-level abstractions that make building complex, large-scale software much more manageable.

Why C++?

C++ is the powerhouse of the technology industry. It is the language behind high-performance gaming engines like Unreal Engine, desktop applications like Adobe Photoshop, web browsers like Chrome, and operating systems. Learning C++ gives you a unique advantage: you can write code as efficient as C, but with the modularity and safety of object-oriented design.

The Transition: C vs. C++

You might wonder, "If I already know C, why learn C++?" The answer lies in the ecosystem. C++ includes the Standard Template Library (STL), a collection of pre-written, highly optimized data structures and algorithms. Furthermore, it introduces classes, inheritance, and polymorphism, which allow you to model real-world concepts directly in your code.

Your First C++ Program

Even the syntax for "Hello, World!" shows the evolution. In C++, we use iostream for input/output and the std (standard) namespace.

#include <iostream>

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

Key differences to notice:

Crucial Concepts

C++ is a superset of C. This means almost all valid C code is also valid C++ code. However, you should aim to use C++ features, such as std::string instead of C-style character arrays, as they are safer and easier to use.

Try It Yourself

  1. Install a C++ compiler (like G++ or Clang).
  2. Create a file named main.cpp and run the "Hello, World!" example.
  3. Modify the program to output your name and your favorite programming language on two separate lines.

C++ vs C: What's Different?

C++ was created by Bjarne Stroustrup in 1985 as "C with Classes," adding object-oriented programming on top of C's procedural foundation. This means C++ supports everything C does, plus classes, objects, inheritance, and polymorphism. Modern C++ is used in game engines (Unreal Engine), high-frequency trading systems, and performance-critical software where both speed and structure matter.

Namespaces and std::

You'll often see std:: before names like cout and cin in C++ code. This is because they belong to the std (standard) namespace, which prevents naming conflicts between the standard library and your own code.

#include <iostream>
using namespace std;

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

Using using namespace std; lets you skip typing std:: every time, which is common in small learning projects but often avoided in large production codebases to prevent naming collisions.

A Note on Semicolons

Every statement in C++ must end with a semicolon — forgetting one is probably the single most common beginner mistake and will cause a compiler error pointing at the following line rather than the actual mistake, which can be confusing at first.