CS2210 C++ Key Concepts: Data Types, Functions, Arrays, I/O, and Compilation

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/39

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

40 Terms

1
New cards

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.

2
New cards

sizeof() Operator

A compile-time operator that returns the size of a data type or variable in bytes.

3
New cards

Size of char

The size of char is always guaranteed to be 1 byte.

4
New cards

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).

5
New cards

Type Modifiers

unsigned: For non-negative values only.

6
New cards

const

A promise that a variable's value will not be changed after it is initialized. The initialization can happen at runtime.

7
New cards

constexpr

A stricter constant that must be known and evaluated at compile time. It can be used for things like array size declarations.

8
New cards

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).

9
New cards

Prefix Increment

Prefix (++x): The variable is incremented first, and the new value is used in the expression.

10
New cards

Postfix Increment

Postfix (x++): The original value of the variable is used in the expression first, and then the variable is incremented.

11
New cards

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.

12
New cards

Short-Circuit Evaluation (AND)

&& (AND): If the first operand is false, the second is never evaluated.

13
New cards

Short-Circuit Evaluation (OR)

|| (OR): If the first operand is true, the second is never evaluated.

14
New cards

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.

15
New cards

Function Prototypes

A function prototype is a declaration of a function that specifies its name, return type, and parameter types.

16
New cards

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.

17
New cards

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++.

18
New cards

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.

19
New cards

const reference

Pass large objects that you don't intend to modify as a const reference (e.g., void func(const string &s)).

20
New cards

C-Style Arrays

Have a fixed size determined at compile time.

21
New cards

C-Style Arrays Size Tracking

Do not have a .length or .size() property; the size must be tracked and passed to functions separately.

22
New cards

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.

23
New cards

Array Decay

When a C-style array is passed to a function, it 'decays' to a pointer to its first element.

24
New cards

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.

25
New cards

std::vector

The modern C++ alternative for dynamic arrays (similar to Java's ArrayList).

26
New cards

std::vector Memory Management

Automatically manages memory and can grow or shrink in size.

27
New cards

std::vector Size Tracking

Tracks its own size via the .size() method.

28
New cards

std::vector Access Methods

Provides bounds-checked access with the .at() method and unchecked access with [].

29
New cards

std::array

A modern C++ wrapper for fixed-size arrays.

30
New cards

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.

31
New cards

Input/Output (I/O) Streams

C++ uses streams like cin (standard input), cout (standard output), and cerr (standard error).

32
New cards

Extraction Operator

The extraction operator (>>) reads input from a stream (e.g., cin >> myVar;).

33
New cards

Insertion Operator

The insertion operator (<<) writes output to a stream (e.g., cout << 'Hello';).

34
New cards

Reading Input with Spaces

The >> operator stops reading at the first whitespace character (space, tab, newline).

35
New cards

getline() Function

To read an entire line of text, including spaces, use the getline() function: getline(cin, myString);.

36
New cards

EOF-Controlled Loop

A common pattern to read an unknown amount of data is to loop until the End-Of-File (EOF) is reached.

37
New cards

C++ Exception Handling

A primary difference from Java is that C++ has no concept of checked exceptions.

38
New cards

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.

39
New cards

C++ Compilation Process

C++ code is compiled directly into native machine code for a specific platform (OS and CPU).

40
New cards

Java Compilation Process

Java code is first compiled into platform-independent bytecode (.class files).