zyBooks
C++ Fundamentals
Introduction to C++
- A program involves input, process, and output.
- Programs use variables to store data (e.g., x).
- Algorithm: A sequence of instructions to solve a problem.
- Computational Thinking: Creating instruction sequences for problem-solving.
Programming Basics
- Program execution starts in
main(). Statements execute sequentially. - Statements usually end with a semicolon (;).
- Code: Textual representation of a program.
cin >> x; gets input and puts it into variable x.cout is used for output.- String literals are enclosed in double quotes (" ").
cout << endl; or \n creates a new line.
- Console: Text-based interface for program I/O.
- Prompt: A message indicating expected user input.
- Comment: Text ignored by the compiler, for human understanding.
- Single-line comment:
// comment - Multi-line comment:
/* comment */ - Whitespace: Spaces, tabs, and newlines; mostly irrelevant to the compiler.
Errors and Warnings
- Syntax Error: Violation of language rules, detected by the compiler (compile-time error).
- Logic Error (Bug): Error during program execution.
- Warning: Indicates a potential logic error but doesn't prevent compilation.
Computers and Programs
- Bits: 0s and 1s (binary digits).
- Processors: Execute calculations.
- Memory: Stores 0s and 1s.
- Machine Instructions: Instructions as 0s and 1s.
- Executable Program: Sequence of machine instructions.
- Compilers: Translate high-level language to executable programs.
Integrated Development Environment (IDE)
- IDE: Software integrating a text editor and a compiler.
- Features: Syntax highlighting, automatic delimiter completion, automatic indentation.
- Console/Terminal: Text-based interface for running programs.
- Command-Line Interface (CLI): User enters commands to run programs.
- Command-Line Argument: Value entered after the program name from the command line.
Computer Tour
- Screen: Displays output.
- Keyboard: Provides input.
- Solid-State Drive (SSD): Flash memory for storing files.
- RAM (Random-Access Memory): Temporarily holds data.
- Byte: 8 bits.
- Processor: Executes programs.
- Operating System: Manages programs and peripherals.
- Cache: Small, fast RAM on the processor.
- Clock: Governs processor instruction execution rate.
- Transistors: Small switches integrated onto a chip.
- Moore's Law: Doubling of IC capacity roughly every 18 months.
Language History
- C: Developed at AT&T Bell Labs, described in 1978, a high-level language.
- C++: Developed by Bjarne Stroustrup in 1985, adding object-oriented programming support to C.
Problem Solving
- Programming is about problem-solving.
- Computational Thinking: Building logical, precise programs.
Why Programming
- Computational thinking benefits extend beyond programming.
Why Whitespace Matters
- Whitespace is any blank space or newline.
- Attention to detail is important for correctness.
Variables / Assignments
- Variable: A named item (e.g.,
x, numPeople) holding a value. - Assignment: Assigning a value to a variable (e.g.,
x = 5). - Incrementing: Increasing a variable's value by 1 (e.g.,
x = x + 1).
Variables (int)
- Variable Declaration: Specifies variable's name and type.
- Allocation: Process of determining memory location.
- Assignment Statement: Assigns value on the right to the variable on the left.
- Expression: Number, variable name, or calculation (e.g.,
80, numApples, numApples + 1). - Integer Literal: An integer in an expression (e.g.,
80).
Identifiers
- Identifier: Name created by a programmer for a variable or function.
- Case-sensitive.
- Reserved Word (Keyword): Part of the language (e.g.,
int, short, double). - Lower Camel Case:
numApples or peopleOnBus.
Arithmetic Expressions (General)
- Expression: Combination of variables, literals, operators, and parentheses evaluating to a value.
- Literal: Specific value in code (e.g.,
2). - Operator: Symbol performing a built-in calculation (e.g.,
+). - Operators:
+ (addition), - (subtraction), * (multiplication), / (division). - Precedence Rules: Order of mathematical operations.
Arithmetic Expressions (int)
- Unary Minus: Minus sign used as negative.
- Compound Operators: Shorthand for updating a variable (e.g.,
+=, -=, *=, /=, %=). Example: userAge += 1 is equivalent to userAge = userAge + 1
Example: Health Data
- Incremental Development: Writing, compiling, and testing small code increments.
Floating-Point Numbers (double)
- Floating-Point Number: Real number with a decimal point (e.g.,
98.6, 0.0001). double: Variable type for storing floating-point numbers.- Floating-Point Literal: Number with a fractional part (e.g.,
1.0, 0.0). - Not a Number (NaN): Unrepresentable or undefined value.
Scientific Notation for Floating-Point Literals
- Scientific Notation: Using
e to represent power-of-10 exponent (e.g., 6.02e23 for 6.02 \times 10^{23}).
Constant Variables
- Constant Variable: Initialized variable whose value cannot change.
The #define Directive
#define MACROIDENTIFIER replacement: Replaces MACROIDENTIFIER with replacement in code.- Also called a macro.
Compiling and Running a Program Using a Command-Line Interface
- Command-Line Interface (CLI): Text commands input to OS.
- Source File: Code to be compiled.
g++: CLI command to compile a C++ source file.a.out: Default name for executable program file.-o: Flag to assign a filename to the executable file (e.g., g++ carCounter.cpp -o CarCounter).
Using Math Functions
- Math Library: Contains math operations (functions).
- Function/Function Call: List of statements executed by invoking the function's name.
- Arguments: Input values to a function.
Integer Division and Modulo
- Divide-by-Zero Error: Occurs at runtime if divisor is 0.
- Runtime Error: Severe error causing program termination.
- Modulo Operator (%): Returns remainder of integer division (e.g.,
23 % 10 is 3).
Type Conversions
- Type Conversion: Conversion from one data type to another (e.g.,
int to double). - Implicit Conversion: Automatic conversion by the compiler.
- Type Cast: Explicitly converts a value from one type to another using
static_cast<type>(expression).
Binary
- Binary Number: Base-2 representation using 0s and 1s.
- Decimal Number: Base-10 representation using 0-9.
Characters
char: Stores a single character (e.g., char myChar;).- Character Literal: Surrounded by single quotes (e.g.,
myChar = 'm';). - ASCII: Standard for encoding characters as numbers.
- Escape Sequence: Two-character sequence starting with
\ representing a special character.
Strings
- String: Sequence of characters.
- String Literal: Character sequence with double quotes (e.g.,
"Hello"). - Whitespace Character: Represents spaces, tabs, and newlines.
getline(cin, stringVar): Gets all text up to the next newline character.
Integer Overflow
- Overflow: Occurs when assigned value exceeds maximum variable value.
- Compiler may provide a warning.
Numeric Data Types
long long: Used for integers exceeding about 2 billion.- Overflow: Occurs when assigned value exceeds maximum variable value.
Unsigned
- Used to store only positive values.
Random Numbers
rand(): Returns a random integer between 0 and RAND_MAX.- Seed: Initial value for random number generation.
time(): Returns seconds since Jan 1, 1970.
Debugging
- Debugging: Process of finding and fixing the cause of a problem.
- Troubleshooting: Another word for debugging.
Auto (since C++11)
auto: Keyword telling the compiler to determine variable type.typeid: Operator reports a variable's type.
Style Guidelines
- Style guidelines for code formatting.
Branches
- Branch: Sequence of statements executed under a condition.
if: Executes statements if an expression is true.if-else: Executes one branch if true, another if false.
Loops
- Loop: Program construct repeating statements while an expression is true.
- Iteration: Each time through a loop's statements.
- Sentinel Value: Special Value Idicating the end of a list.