Ultimate C++ Course Notes

Jul 9, 2024

Ultimate C++ Course

Introduction

Course Overview

  • Instructor: Mosh Hamedani, 20+ years experience, taught millions to code.
  • Course Objective: Learn C++ from basics to advanced concepts, enabling confident coding by course end.
  • Target Audience: Beginners with no prior knowledge of C++ or programming; comprehensive and organized.
  • Relevance: Widely used in performance-critical applications by companies like Adobe, Google, Microsoft, NASA.
  • Salary: Average C++ programmer salary in the US ~$170,000/year.

Mastering C++

Key Learning Areas

  • Language Syntax: Grammar and rules of C++.
  • Standard Library (STL): Pre-written C++ code for data structures (lists, maps) and algorithms (searching, sorting).
  • Versioning: A new version every three years; latest is C++20.

Tools and Environment

  • IDEs: Visual Studio (Windows), Xcode (Mac), CLion (Windows, Mac, Linux).
  • Setup Recommendations: Use CLion for ease; download from jetbrains.com, ensure correct version for macOS (Intel vs Apple Silicon).

Getting Started with C++

Writing the First Program

  • Structure: Main function as the entry point (int main() {}); returns 0 for success.
  • Libraries: #include <iostream> for printing to the screen using std::cout.
  • Compilation and Execution: Compiling into machine code; running using IDE's play button.
  • Common Errors: Missing semicolons causing compilation failures.

Coding Best Practices

  • Formatting: Consistent use of spaces, braces style, and code readability.
  • Comments: Use for explaining 'why' and 'how', not 'what'. Avoid overuse.

C++ Basics

Variables and Data Types

  • Variable Declaration: Type followed by name, e.g., int fileSize = 100;
  • Types: int, double, float, char, bool
  • Best Practices: Meaningful variable names, initialization at declaration.

Constants

  • Usage: Prevent value changes using const keyword, e.g., const double PI = 3.14;

Naming Conventions

  • Styles: Snake case (file_size), Pascal case (FileSize), Camel case (fileSize). Use Camel case for variables and Pascal case for classes.

Mathematical Expressions

  • Operators: Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%), Increment (++), Decrement (--).
  • Precedence: Multiplication/Division before Addition/Subtraction; use parentheses to alter precedence.
  • Usage Examples: Basic arithmetic, swapping variables, handling floating-point division.

Input/Output

  • Output: std::cout for writing to console.
  • Input: std::cin for reading from the console.
  • Projects Example: State and county tax calculation, temperature conversion from Fahrenheit to Celsius.

Standard Library Functions

  • Math Functions: cmath library for functions like floor, pow.
  • Usage: Generating random numbers using rand, seeding random generator with srand.

Advanced Topics

Seeding Random Numbers

  • Purpose: Use current time to seed for true randomness; srand(time(nullptr)).
  • Example: Rolling a dice, bounded random number using modulus operator.

Fundamental Data Types

  • Overview: int, short, long, float, double, char, bool
  • Memory Use: 2-4 bytes for integer types, float/double for differing precision.
  • Signed vs. Unsigned: Avoid unsigned due to potential overflow issues.

Brace Initialization

  • Usage: Prevents narrowing conversions; initializes variables to zero by default.
  • Example: int number{1.2}; causes an error, int number{}; initializes to zero.

Number Systems

  • Types: Decimal (base 10), Binary (base 2), Hexadecimal (base 16).
  • Usage in C++: Representation with prefixes (0b for binary, 0x for hexadecimal).

Conclusion

  • Further Learning: Complete C++ series covers basics to advanced concepts like classes, exceptions, templates.
  • Career Path: Understanding C++ opens opportunities in various fields including gaming (Unreal Engine).
  • Additional Resources: Download cheat sheet and summary notes provided in the course.