Advanced Programming in C++ Flashcards
Why do we use C/C++?
- Complete, non-simplified
- Efficient
- Can handle high-performance tasks like simulations, real-time processing, and visualization
- Language of choice for embedded systems
- Can be made safe with proper education
What are the main steps in the C++ compilation process?
- Preprocessing
- Compilation (converting source code to object code)
- Linking (combining object code with libraries to create an executable)
What is the purpose of the preprocessing stage in C++ compilation?
Preprocessing handles directives like #include and #define, preparing the code for compilation
What does the term “data type” refer to in C++?
A data type specifies the kind of data a variable can hold, such as int, char, or float.
What are the main categories of primitive data types in C++?
- Integer Numbers (int)
- Character (char)
- Booleans (bool)
- Floating Point (float)
- Void (void)
How do you declare and initialize a variable in C++?
Use the syntax scope type identifier = value;
(e.g., global int x = 5;)
What is a scope in C++?
A scope defines the region of code where a variable is accessible. Variables in a local scope (like inside a function) are not accessible outside of it.
What are the 3 different types of scope?
- Global: Variable is declared outside all functions and is accessible throughout the program, but not from other files
- Local: Variable is only accessible within the function or block where it is declared, only exists during an invocation and ceases to exist at the end of it
- Local Statics: Variable is declared during first invocation and doesn’t cease to exist at the end of it
What is the difference between stack and heap memory?
Stack memory is used for local variables and function calls, while heap memory is for dynamic allocation, managed manually
What are the roles of new and delete in C++?
- new allocates memory on the heap
- delete deallocates memory to avoid memory leaks
What is malloc and how is it used in C++?
- Function in C++ to allocate a block of memory on the heap
- Returns a pointer to the beginning of the block
- Unlike new, malloc does not call constructors and requires explicit casting of the returned pointer to the desired data type
- Memory allocated with malloc should be deallocated using free()
What are the 3 elements of a flowchart?
- Activity (rectangular block, e.g. instructions, subprograms, …)
- Decision (diamond, condition (e.g. Boolean expression) is specified)
- Terminal activity (rounded rectangle)
What is the structured program theorem?
Flow graphs can compute any computable function by composing the following 3 control structures:
- Sequential execution (execute one subprogram and after that another one)
- Conditional branch, selection (execute one of two subprograms according to the value of a Boolean expression)
- Loop (repeat execution of a subprogram as long as a Boolean expression evaluates to true each time before executing the subprogram)
What is a block scope in C++?
- Block scope refers to variables declared within a block of code, enclosed by {}
- These variables are only accessible within that block
How does a switch statement work in C++?
- A switch statement evaluates an expression and executes the corresponding case block
- If no cases match, the default block (if provided) is executed
What is the purpose of the break statement in a switch case?
break exits the switch statement to prevent “fall-through” to the next case (execution continues until break is encountered)