Overview
This lecture covers the core concepts needed to get started in C++, from setting up your environment to basic syntax, variables, operators, control structures, arrays, functions, object-oriented programming, and more.
Why Learn C++ & Setup
- C++ is a fast, middle-level language used in graphics, embedded systems, and video game development.
- C++ balances human-readable code and machine-level control.
- You need a text editor (e.g., VS Code) and a compiler (e.g., GCC for Windows/Linux, Clang for Mac).
- IDEs like VS Code and Code Blocks provide helpful developer tools.
Writing Your First C++ Program
- Include header files with
#include <iostream> for input/output operations.
- The program starts at the
int main() function.
- Use
std::cout to print output, ending statements with a semicolon.
- Use
std::endl or \n for new line output.
- Comments start with
// for single line, /* ... */ for multiline.
Variables & Data Types
- Variables represent data; declare with a data type (
int, double, char, bool, string).
- Assignment can be combined with declaration:
int x = 5;
int stores whole numbers, double stores decimals, char stores single characters, bool is true/false, string stores text.
- Use
std::cin to accept user input; use std::getline for input with spaces.
Constants & Namespaces
const keyword makes variables read-only.
- Naming CONVENTION: constants are uppercase.
- Namespaces group entities and avoid naming conflicts; access with
namespace::entity.
Typedefs & Type Aliases
typedef and using create type aliases for improved readability.
Operators & Expressions
- Arithmetic operators:
+, -, *, /, % (modulus).
- Use
++ and -- for increment/decrement, +=, -= for shorthand updates.
- Operator precedence: parentheses > multiplication/division > addition/subtraction.
- Type conversion (implicit/explicit) changes data types as needed.*
Control Structures
if, else if, else for decision making.
switch is efficient for multiple discrete cases, use break to exit cases.
- Ternary operator:
condition ? expr1 : expr2;
- Logical operators:
&& (and), || (or), ! (not).
Loops
while and do...while for repeating code under a condition.
for loop for a set number of iterations.
break exits a loop; continue skips an iteration.
- Nested loops allow for multi-level iteration (e.g., grids).
Arrays & Strings
- Arrays store multiple values of the same type, indexed from 0.
- Use
sizeof(array)/sizeof(array[0]) to get array length.
- Iterate arrays with loops (
for or range-based for).
- 2D arrays represent grids or matrices.
- Common string methods:
.length(), .empty(), .clear(), .append(), .at(), .insert(), .find(), .erase().
- Use
std::fill() to initialize array values.
Functions
- Functions group reusable code, may take parameters and return values.
- Overloaded functions share the same name but have different parameters.
- Pass by value copies arguments; pass by reference modifies originals.
- Use
const for read-only parameters.
Pointers & Dynamic Memory
- Pointer is a variable that stores a memory address; use
* for declaration and dereferencing.
- Null pointers (
nullptr) indicate no assigned address.
- Use
new to allocate dynamic memory and delete to free it.*
Object-Oriented Programming
- Classes define objects with attributes and methods.
- Constructors initialize object attributes, support overloading.
- Access modifiers (
public, private) control visibility.
- Use getters and setters for encapsulation.
- Inheritance allows child classes to receive attributes/methods from parent classes.
Structs & Enums
struct groups related variables (members) of different types.
enum creates a set of named integer constants for fixed choices.
Other Concepts
- Random numbers:
rand(), seeded with srand().
- Templates generalize functions for multiple data types.
- Search and sort algorithms can be implemented with arrays.
- Recursion is when a function calls itself for repetitive tasks.
Key Terms & Definitions
- Compiler — software that converts code to machine instructions.
- Namespace — a scope for identifiers to avoid naming conflicts.
- Pointer — a variable storing the address of another variable.
- Class — a blueprint for creating objects with attributes and methods.
- Constructor — special class function to initialize new objects.
- Getter/Setter — methods for reading/updating private attributes.
- Inheritance — a mechanism where one class derives from another.
Action Items / Next Steps
- Practice by writing simple programs using variables, loops, and functions.
- Create a class with a constructor, getter, and setter.
- Try implementing array search and sort functions.
- Review any assigned homework or exercises from the lecture.