💻

C++ Programming Overview

Jul 13, 2025

Overview

This lecture is an in-depth introduction to C++ programming, covering core language fundamentals, data structures, control flow, functions, object-oriented programming, memory management, and practical coding exercises.

Introduction to C++ & Setup

  • C++ is a fast, middle-level programming language widely used for graphics, embedded systems, and games.
  • Requires a text editor (e.g., VS Code) and a compiler (GCC for Windows/Linux, Clang for Mac).
  • Installation steps provided for editors and compilers on all platforms.

Writing Your First Program

  • C++ programs start with #include <iostream> for input/output operations.
  • The entry point is the int main() function; code is written within curly braces.
  • return 0; signals successful program completion.
  • std::cout is used for output, ending statements with semicolons.
  • Output can be formatted with std::endl or escape sequences like \n.
  • Comments use // for single lines and /* ... */ for blocks.

Variables & Data Types

  • Variables must be declared with a data type: int (whole numbers), double (decimals), char (single character), bool (true/false), and std::string (text).
  • Declaration and assignment can be separate or combined (e.g., int x = 5;).
  • Use const for constants that should not be modified.

Namespaces, Typedefs, and Type Aliases

  • Namespaces prevent name conflicts; use the :: scope operator to specify.
  • typedef or using creates type aliases for easier code readability.

Arithmetic, Type Conversion, & Input

  • Arithmetic operators: +, -, *, /, %.
  • Operator precedence: parentheses, then multiplication/division, then addition/subtraction.
  • Type conversion can be implicit (automatic) or explicit (via casting).
  • User input uses std::cin; use getline for strings with spaces.*

Control Flow: If, Switch, Loops

  • if, else if, and else provide conditional logic.
  • switch is more efficient when matching one variable to many cases.
  • Loops:
    • while and do-while repeat while a condition is true.
    • for loop repeats a specific number of times.
    • break exits a loop; continue skips to the next iteration.
    • Nested loops allow complex iteration (e.g., printing rectangles).

Math Functions & Projects

  • Use <cmath> for functions like pow, sqrt, abs, round, ceil, floor.
  • Practice project: calculating the hypotenuse using user input and math functions.

Logical & Ternary Operators

  • Logical operators: && (and), || (or), ! (not).
  • Ternary operator (? :) provides a quick if-else in one line.

Arrays & Iteration

  • Arrays store multiple values of the same type, accessed by index starting at 0.
  • Use loops to iterate through arrays, and the sizeof operator to find array size.

Functions

  • Functions are blocks of reusable code; call with parentheses and pass arguments.
  • Pass by value creates a copy; pass by reference (using &) modifies the original.
  • The return keyword sends a value back to the caller.
  • Function overloading allows multiple functions with the same name but different parameters.
  • Use templates to create generic functions.

Structs, Enums, and Classes

  • Structs group different data types as a single unit (members accessed via .member).
  • Enums are user-defined types of named constants.
  • Classes are blueprints for objects combining data (attributes) and methods (functions); use constructors to initialize.

Object-Oriented Programming Concepts

  • Inheritance allows child classes to inherit attributes and methods from parent classes.
  • Use getters/setters to control access to private attributes (abstraction).

Pointers, Memory, and Dynamic Allocation

  • Pointers store memory addresses; use * for declaration/dereferencing and & for address-of.
  • Null pointers (nullptr) indicate no address assigned.
  • Dynamic memory uses new and delete (arrays: delete[]).*

Recursion

  • Recursive functions call themselves, breaking problems into smaller subproblems.
  • Require base cases to prevent infinite recursion.

Practical Projects & Exercises

  • Projects include banking, calculator, rock-paper-scissors, tic-tac-toe, quiz game, sorting/searching arrays, and credit card validation.

Key Terms & Definitions

  • Compiler — Translates source code into machine instructions.
  • Namespace — Unique naming scope to avoid name conflicts.
  • Const — Keyword marking a variable as read-only.
  • Array — A collection of elements of the same type, accessed by index.
  • Pointer — A variable storing a memory address.
  • Struct — User-defined type grouping multiple variables.
  • Class — User-defined type combining attributes and methods.
  • Inheritance — Mechanism where one class derives from another.
  • Dynamic memory — Memory allocated at runtime using new.

Action Items / Next Steps

  • Practice by coding small programs for each topic (loops, arrays, structs, classes).
  • Complete all assigned coding exercises and post your code as specified.
  • Review additional readings on recursion, object-oriented design, and memory management.