Advanced Programming in C++ Flashcards
(40 cards)
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)
What is a function in C++?
- A function is a block of code designed to perform a specific task
- It can accept inputs (parameters) and return a result
How can the parameters given to a function be changed?
If given as a reference (e.g. int& a), the variable can be changed in the function and persists after, not just as local variable
What is a const reference?
- The variable data is not copied and given to the function and it is not editable
- Safe and easy
- E.g. void func(const a& data)
- Faster than function call by value
What is a function prototype in C++ and why is it used?
- A function prototype declares a function’s return type, name, and parameters before its definition
- It is used to inform the compiler about the function’s existence
How do you define a function in C++?
A function is defined with a return type, name, parameter list, and body:
return_type function_name(parameters) { body }
What is recursion in C++?
Recursion occurs when a function calls itself to solve a smaller instance of the same problem, often used in algorithms like factorial calculation
What is a for loop and when would you use it?
- A for loop is used for iterating a fixed number of times
- It consists of initialization, condition, and increment/decrement expressions
How does a while loop differ from a do-while loop in C++?
- while loop checks the condition before executing the loop body
- do-while loop checks the condition after, ensuring the body executes at least once