Week 1: Starting Your C++ Adventure

Dive into the fundamentals of C++ and set up your development environment.

Explore Chapter 1

Chapter 1: Introduction to C++ and Environment Setup

What is C++? History and Applications.

Welcome to C++! C++ is a powerful, general-purpose programming language known for its performance, efficiency, and control over system resources. It's an extension of the C language, adding object-oriented features and other enhancements.

A Brief History

C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs as an enhancement to the C language. It was initially named "C with Classes" and was renamed C++ in 1983. It has evolved significantly over the decades through standardization (C++98, C++03, C++11, C++14, C++17, C++20, etc.).

Key Characteristics

  • Compiled: C++ code is typically compiled directly into machine code, resulting in high execution speed.
  • Statically Typed: Variable types must be declared and are checked at compile time.
  • Multi-Paradigm: Supports procedural, object-oriented, and generic programming.
  • Performance-Oriented: Offers low-level memory manipulation capabilities.
  • Portable: C++ compilers exist for many platforms.

Applications

C++ is used extensively where performance is critical:

  • Game Development (Engines like Unreal Engine)
  • Operating Systems & System Software
  • High-Performance Computing (HPC)
  • Embedded Systems & IoT
  • Financial Trading Systems
  • Web Browsers (e.g., Chrome, Firefox)
  • Database Software

Why Learn C++?

Learning C++ can be challenging but incredibly rewarding:

  • Performance: C++ allows for highly optimized code that runs very fast.
  • Control: Provides fine-grained control over hardware and memory.
  • Foundation: Understanding C++ provides deep insights into how computers work and makes learning other languages easier.
  • Wide Applicability: Used in many performance-critical domains where other languages might not suffice.
  • Large Codebases: Many existing large-scale systems are built with C++.
  • Game Development Standard: A dominant language in the professional game development industry.

Setting up Your Development Environment.

To write and run C++ code, you need a compiler and usually a code editor or an Integrated Development Environment (IDE).

1. Choosing a Compiler

A compiler translates your C++ source code into executable machine code.

  • GCC (g++): Part of the GNU Compiler Collection, widely available on Linux and macOS (often via Xcode Command Line Tools), available on Windows (e.g., via MinGW or Cygwin).
  • Clang: Another popular open-source compiler, known for fast compilation and helpful error messages. Often used alongside GCC or as part of LLVM.
  • MSVC (Microsoft Visual C++): The compiler included with Visual Studio on Windows.

Installation varies by OS. On Linux, you can often install `g++` via your package manager (e.g., `sudo apt install build-essential` on Debian/Ubuntu). On macOS, installing Xcode Command Line Tools (`xcode-select --install` in Terminal) typically provides Clang/g++. On Windows, installing Visual Studio Community (with C++ workload) or setting up MinGW/MSYS2 are common choices.

2. Choosing a Code Editor/IDE

An IDE bundles an editor, compiler, debugger, and other tools.

  • Visual Studio Code (VS Code): Free, popular, highly extensible. Requires installing C++ extensions (like Microsoft's C/C++ extension) and configuring it to use your chosen compiler.
  • Visual Studio (Community Edition): Free for individuals and small teams on Windows. A full-featured IDE with excellent C++ support (uses MSVC compiler).
  • CLion: Powerful cross-platform C++ IDE by JetBrains (paid, but often free for students).
  • Code::Blocks: Free, open-source, cross-platform IDE.
  • Simple Text Editors (like Sublime Text, Atom, Notepad++) + Manual Compilation: Possible, but less convenient for larger projects.

For beginners, VS Code (with configuration) or Visual Studio Community (on Windows) are often good starting points.

Your First C++ Program: Hello, World!

Let's write the traditional first program.

  1. Create a File: Open your code editor/IDE and create a new file named `hello.cpp`.
  2. Write the Code:
    #include <iostream> // Include the Input/Output Stream library
    
    int main() { // main function: program execution starts here
        std::cout << "Hello, C++!" << std::endl; // Output the string to the console
        return 0; // Indicate successful execution
    }
                            
  3. Compile the Code: Open a terminal or command prompt, navigate to the directory where you saved `hello.cpp`, and use your compiler.
    • Using g++: `g++ hello.cpp -o hello`
    • Using Clang: `clang++ hello.cpp -o hello`
    • Using MSVC (from Developer Command Prompt): `cl hello.cpp`
    This creates an executable file (e.g., `hello.exe` on Windows, `hello` on Linux/macOS).
  4. Run the Executable:
    • Windows: `.\hello.exe` or `hello.exe`
    • Linux/macOS: `./hello`
  5. See the Output: You should see `Hello, C++!` printed in your terminal.

Congratulations! You've compiled and run your first C++ program.

Basic Syntax, Comments, and Statements.

Understanding the basic rules of C++ syntax is crucial.

  • Statements: Instructions in C++ typically end with a semicolon (`;`).
    int score = 100;
    std::cout << "Score:" << score;
  • Blocks: Code blocks are enclosed in curly braces `{}`. They define scope (e.g., for functions, loops, conditionals).
    if (score > 50) {
        std::cout << "Pass!";
        // More code inside the block
    }
  • Case Sensitivity: C++ is case-sensitive (`myVar` and `myvar` are different).
  • Whitespace: C++ largely ignores extra spaces, tabs, and newlines between statements, used for readability.
  • Comments: Ignored by the compiler, used for explanations.
    • Single-line comments: Start with `//`.
      // This is a single-line comment
      int x = 10; // Assign 10 to x
    • Multi-line comments: Start with `/*` and end with `*/`.
      /* This is a
         multi-line comment.
         It spans multiple lines. */
      int y = 20;
  • Preprocessor Directives: Lines starting with `#` (like `#include`) are processed before compilation. `#include` is used to bring in code from header files (libraries).
  • `main()` function: Every executable C++ program must have a `main` function. It's the starting point of execution. `int main()` indicates it returns an integer status code (0 typically means success).
  • Namespaces (`std::`): C++ uses namespaces to organize code and prevent naming conflicts. `std::` is the standard namespace where features like `cout`, `cin`, and `endl` are defined. We'll discuss `using namespace std;` later.

These fundamental rules form the basis for writing C++ code.

Syllabus