1/39
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Primitive Types vs. Objects
Unlike Java where everything is an object or can be auto-boxed, C++ has primitive types that are not objects. This improves performance but requires more careful handling.
sizeof() Operator
A compile-time operator that returns the size of a data type or variable in bytes.
Size of char
The size of char is always guaranteed to be 1 byte.
Size Hierarchy
Sizes of other types (int, long, etc.) can vary by platform, but a general hierarchy is guaranteed: sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long).
Type Modifiers
unsigned: For non-negative values only.
const
A promise that a variable's value will not be changed after it is initialized. The initialization can happen at runtime.
constexpr
A stricter constant that must be known and evaluated at compile time. It can be used for things like array size declarations.
Integer Division
When dividing two integers, C++ performs integer division and truncates (discards) the remainder. To get a floating-point result, at least one of the operands must be a floating-point type (e.g., 5.0 / 2).
Prefix Increment
Prefix (++x): The variable is incremented first, and the new value is used in the expression.
Postfix Increment
Postfix (x++): The original value of the variable is used in the expression first, and then the variable is incremented.
Short-Circuit Evaluation
Logical expressions are evaluated from left to right, and evaluation stops as soon as the outcome is certain. This is guaranteed by the language standard.
Short-Circuit Evaluation (AND)
&& (AND): If the first operand is false, the second is never evaluated.
Short-Circuit Evaluation (OR)
|| (OR): If the first operand is true, the second is never evaluated.
Free Functions
A major difference from Java is that C++ functions can exist outside of a class. These are called 'free functions'. Java requires all functions (methods) to be within a class.
Function Prototypes
A function prototype is a declaration of a function that specifies its name, return type, and parameter types.
The Call Stack
Each time a function is called, a new 'stack frame' is created in memory to hold its local variables and parameters. When the function returns, this memory is automatically cleaned up.
Pass-by-Value
The function receives a copy of the argument. Any modifications to the parameter inside the function affect only the local copy and do not change the original variable. This is the default behavior in C++.
Pass-by-Reference
Achieved by using an ampersand (&) in the parameter declaration (e.g., void func(int &x)). The parameter becomes an alias for the original variable; no copy is made. Modifications to the parameter inside the function directly change the original variable.
const reference
Pass large objects that you don't intend to modify as a const reference (e.g., void func(const string &s)).
C-Style Arrays
Have a fixed size determined at compile time.
C-Style Arrays Size Tracking
Do not have a .length or .size() property; the size must be tracked and passed to functions separately.
C-Style Arrays Bounds Checking
C++ performs no automatic bounds checking, so accessing an index like arr[10] in a 5-element array leads to undefined behavior.
Array Decay
When a C-style array is passed to a function, it 'decays' to a pointer to its first element.
sizeof in Functions
Because of decay, using sizeof(arr) inside a function will return the size of a pointer (e.g., 4 or 8 bytes), not the size of the original array.
std::vector
The modern C++ alternative for dynamic arrays (similar to Java's ArrayList).
std::vector Memory Management
Automatically manages memory and can grow or shrink in size.
std::vector Size Tracking
Tracks its own size via the .size() method.
std::vector Access Methods
Provides bounds-checked access with the .at() method and unchecked access with [].
std::array
A modern C++ wrapper for fixed-size arrays.
std::array Size Knowledge
Unlike C-style arrays, it knows its own size (.size()) and does not decay to a pointer when passed to functions.
Input/Output (I/O) Streams
C++ uses streams like cin (standard input), cout (standard output), and cerr (standard error).
Extraction Operator
The extraction operator (>>) reads input from a stream (e.g., cin >> myVar;).
Insertion Operator
The insertion operator (<<) writes output to a stream (e.g., cout << 'Hello';).
Reading Input with Spaces
The >> operator stops reading at the first whitespace character (space, tab, newline).
getline() Function
To read an entire line of text, including spaces, use the getline() function: getline(cin, myString);.
EOF-Controlled Loop
A common pattern to read an unknown amount of data is to loop until the End-Of-File (EOF) is reached.
C++ Exception Handling
A primary difference from Java is that C++ has no concept of checked exceptions.
RAII (Resource Acquisition Is Initialization)
A core C++ pattern where resource management (like closing files or freeing memory) is tied to an object's lifetime.
C++ Compilation Process
C++ code is compiled directly into native machine code for a specific platform (OS and CPU).
Java Compilation Process
Java code is first compiled into platform-independent bytecode (.class files).