C++ Flashcards

1
Q

What does the C++ compiler do when it encounters the “#include” preprocessor directive?

A

It literally copies and pasted the contents of the included file into the program (this process is called “macro expansion”)
-The file contents are pasted at the location of the corresponding “#include”
-That’s why you only need to include a file ONCE in your set of source code files

The compiler searches for the specified file in a predefined set of directories, including the standard system directories and any additional directories specified by the user or the build system (you can even build custom search paths)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Purpose of “#ifndef”

A

Can use #ifndef and #define to make sure that the code is only included one time. The #ifdef guard is used to prevent the machine from unnecessarily using memory for redundent copies of the same code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Referring to the location in which a particular function is stored

A

‘std’ is a popular location (it refers to the “standard library of C++
The location can also be used to identify whether you are talking about data/operations stored in the base class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Passing something by value versus Passing something by reference

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Object Slicing Problem

A

-You wish to pass an object of a derived class by value to a function
-The function parameter data type is that of the base class (not the derived class)
-Since the base class does not have the extra members that were added to the derived class, only the subject of derived class memeber shared with the base class are passed
THIS CAN BE AVOIDED BY PASSING BY REFERENCE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

“virtual” reserved word

A

We can use a virtual function to make write() a polymorphic operation and ensure that the correct version of the function is invoked
-Using “virtual” with a function from the base class guarantees dynamic binding, meaning that the decision for which function (base or derived) is invoked is delayed until run time
-At runtime, the data type of the argument determined which version of the function is invoked
-If virtual appears in the function prototype, it does not appear in the heading of the function definition
-by declaring a member function of the base class as virtual, any redefined versions of the function in derived classes are also virtual

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Arguments

A

Arguments are needed when the function is CALLED. They represent actual variable names so that you can pass actual values to the function. They do not require datatypes
[when the function is virtual, the datatype of the arguments will determine whether the base class’ function or the derived class’ function]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Parameters

A

Parameters are needed in the function header. It is a DECLARATION of variables that you can expect to find in that function. They require datatypes just like any declaration
[when the function is NOT virtual, the data type of the parameter will determine whether the base class’ function or the derived class’ function]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Pure Virtual Functions

A

Functions are made pure virtual by typing “virtual” before the function AND typing “=0;” at the end
At least one of these must be included in each abstract class.
The derived class MUST overwrite this function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Static Allocation

A

Performed at compile time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Indirect Addressing

A

Accessing a variable in two-steps by first using a pointer that gives the location memory (the address of the variable.
Means you are accessing the variable using a pointer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Addresses in memory

A

Every byte is counted. It has it’s own number that represents it. That number is it’s address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Pointer

A

A variable which contains the address or location of other variables.
Pointers use INDIRECT addressing.
The * operator declares the variable a pointer.
If you use a pointer, you can represent any.
ALWAYS an integer value (4 bytes), no matter the datatype of the variable you are pointing to: because it is referencing the ADDRESS of that variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Using a star * before the name of your integer variable

A

Treats the value as a POINTER. You are directing the machine the memory address associated with that particular number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Using an ampersand & before the name of your integer variable

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Advantage of using pointers

A

If you use a pointer (which is always 4 bytes) you can reduce the amount of memory needed to have an array of values
-An array of 20 different 4 byte pointers is better than an array of 20 different 24 byte structures

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Using an arrow in a code -> (with respect to pointers)

A

This means that you are dereferencing the pointer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Dynamic Allocation

A

Allocation of memory space at run time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

The Heap (Free store)

A

Basically sandbox-mode memory, with a limited
Pool of memory locations reserved for allocation and de-allocation of dynamic.
The data created heap cannot be deleted automatically; you have to actually create a command to delete the data you no longer need

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Dynamic allocation Operator: “new”

A

“New” allocates memory to hold an int and stores the address into the variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

“The Stack”

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Memory Leak

A

Too much “garbage” taking up space in the Heap until you run out of memory
Loss of available memory space when that memory is dynamically allocated but not deallocated.
THIS IS WHY IT IS SO IMPORTANT TO DELETE ALL THE VARIABLES THAT YOU ARE NOT USING

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Garbage

A

Memory locations that can no longer be accessed.
This is created when you update what a pointer is pointing toward

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Inaccessible object

A

A dynamic variable on the free store without any pointer pointing to it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Dangling Pointer

A

A pointer that points to a variable that has been deallocated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

deallocation operator: “delete”

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Stack overflow v. Memory Leak

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Robustness

A

The ability of a program to recover following an error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Four Types of Errors

A
  1. A user may accidently or deliberately (hackers) enter incorrect inputs
  2. Hardware does not actually have the resources that you need to actually execute what you need to execute (disk drives and random access memory have size limits)
  3. Hardware devices may fail or becomes inaccessible
  4. Software components may contain defects (bugs)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Six options for handling errors

A
  1. Assume errors will not occur (not an option)
  2. Print a descriptive error message
  3. Return an unusual value to indicate an error has occurred
  4. Alter a status variable’s value
  5. Use assertions to block further execution
  6. Use exception handlers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Exception

A

Basically an anomaly.
An unusual event, detectable by software or hardware, that requires special processing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Exception Handler

A

A section of program code that is executed when a particular exception occurs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

try-throw-catch

A

Used for testing for an exception
Try command is used to run statements that could cause an exception.
The catch command is used to contain “exception handler” statements. THE CATCH IS ONLY EXECUTED IF THE EXCEPTION IS DETECTED

34
Q

What happens if your catch statement is passed the wrong datatype?

A

the compiler will generate an error message

35
Q

Difference between a string and a character array in C++

A
36
Q

What happens what something that is thrown is never caught

A

It gets passed back up the call stack to see if you

37
Q

Why exception handling is important

A
38
Q

“bad_alloc”

A

An exception from the ____
Means “Insufficient memory”

39
Q

Instantiating datatypes with dynamic allocation

A

means that “new” needs to be inside of a try block

40
Q

Purpose of gcov

A

Tool used for code coverage analysis in C and C++ programs. Code coverage analysis is a technique to determine which parts of the code are executed during program execution, helping developers assess the thoroughness of their testing.
Particularly useful for understanding how much of the code is being exercised by test cases.

41
Q

gcov for Code Coverage Analysis

A

The primary purpose of gcov is to provide information on the percentage of code that has been executed during testing. It generates a detailed report indicating which lines of code have been executed and how many times.

42
Q

gcov for Identifying untested code

A

gcov helps in identifying areas of the code that have not been covered by test cases, allowing developers to focus on writing additional tests to achieve higher code coverage.

43
Q

gcov for Improving Testing Strategies

A

By visualizing the parts of the code that are exercised by the tests, developers can refine their testing strategies to ensure more comprehensive coverage, leading to more robust and reliable software.

44
Q

gcov for integration with build systems

A

is often integrated into the build process, allowing automated code coverage analysis as part of the build or testing workflow. It can be invoked alongside a test suite to generate coverage reports.

45
Q

gcov for testing frameworks

A

gcov is used in conjunction with testing frameworks (e.g., Google Test, Catch2) to assess the coverage of the test cases and to guide the creation of additional tests for untested parts of the code.

46
Q

gcov for assessing quality and maintenance

A

Code coverage analysis is a useful metric for evaluating the quality of the software and estimating its maintainability. High code coverage indicates that a substantial portion of the code has been tested.

47
Q

The rule for 100% statement coverage

A

100% Statement Coverage Testing is necessary but not sufficient to ensure a high-quality software product

48
Q

Command Line Arguments

A

Parameters provided to a program when it is invoked from the command line or terminal. - Allow users to pass input values or options to the program, influencing its behavior or providing necessary data for processing.
- Essential for creating flexible and interactive applications.

49
Q

‘struct’ in C++

A

A datatype that represents a record of data
Each new piece of information in a struct is called a member.

50
Q

Selecting a particular member in a struct

A

[struct_name].[member name]

51
Q

Symbol for a line comment in C++

A

//this text is commented out

EXAMPLE:
std::cout &laquo_space;“Hello World!\n”; //say hello

52
Q

Symbol for a block comment in C++

A

/* this text is commented out*/

EXAMPLE:
std::cout &laquo_space;“Hello World!\n”;
/* the machine should print the phrase “Hello World”.
This is a standard introductory program in any programming language */

53
Q

Static Variable v. Automatic Variable

A
54
Q

Purpose of a Header Guard

A

Helps make sure that you are only doing you “macro expansion” once for each of the files that you need to execute your program

EXAMPLE:
#ifdef header_file
#define header_file
/Contents of the header file/
#endif

55
Q

Purpose of the #include preprocessor directive

A

Allows you to add the contents of another file to your source file
#include is always followed by the name of the file that must be included

56
Q

Purpose of #ifndef (with the ‘n’)

A

Means “If Not Defined”:

  • # ifndef is used to check if a specified identifier has not been defined previously in the code.
  • It evaluates to true if the specified identifier is not defined, allowing the subsequent code to be included.
  • It’s commonly used for include guards to prevent a header file from being included multiple times.

EXAMPLE:
#ifndef MY_HEADER_H
#define MY_HEADER_H
// Content of the header file
#endif // MY_HEADER_H

57
Q

Purpose of #ifdef (without the ‘n’)

A

Means “If Defined”

  • # ifdef is used to check if a specified identifier has been defined previously in the code.
  • It evaluates to true if the specified identifier is defined, allowing the subsequent code to be included.
  • It’s used when conditional compilation is needed based on whether a specific feature or macro is defined.

EXAMPLE:
#ifdef debugging_file
//debugging code
#endif

58
Q

What does the compiler do when it encounters #ifndef?

A

The compiler begins a “conditional compilation” process:
1) The compiler check to see if the mentioned marco/symbol has already been defined in the code
2) If it has NOT been defined, the preprocessor includes the code located between the #ifndef and #endif

59
Q

Macros in C++

A

A macro is a fragment of code in a program that has been given a name. It’s a mechanism to perform textual replacement in the source code. Macros provide a way to perform text substitution in the source code before the actual compilation takes place.
- Macros are typically defined using #define preprocessor directive and are often used to define constants or perform simple operations.
EXAMPLE (PI and SQUARE are macros that get replaced with their respective values or code fragments during preprocessing):
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
int radius = 5;
double area = PI * SQUARE(radius);

60
Q

Symbols in C++

A

A symbol is a name that represents a particular piece of data or a memory location in a program. It can be a variable, function, type, or any other named entity in the program.
- Symbols are used throughout the code to refer to specific elements such as variables, functions, types, etc.

EXAMPLE (num and foo are symbols representing an integer variable and a function, respectively):

61
Q

Macros v. Symbols in C++

A
  • A macro is a mechanism to perform textual replacement of code, while a symbol represents a named entity in the program.
  • Macros are often defined using #define and are used for simple text substitutions or operations, while symbols represent various program elements like variables, functions, types, etc.
  • Macros are usually used for constants or simple operations, while symbols encompass a broader range of programming constructs.
62
Q

Six purposes of #define preprocessor directive?

A

Used to define “macros”. By using the #define directive, programmers can create more maintainable and flexible code by centralizing definitions and providing meaningful names to values and code fragments. However, it’s important to use macros judiciously, considering potential pitfalls like macro-related errors and side effects.

Six main uses:
1) Defining constants: One of the primary purposes of #define is to define constants, allowing you to assign a meaningful name to a value and use it throughout the code. This improves code readability and makes it easier to update values in one central place:
#define PI 3.14159
#define MAX_VALUE 100

2) Defining Expressions: Macros can define expressions, making it easier to handle complex or repetitive calculations or operations:
#define SQUARE(x) ((x) * (x))

3) Creating Code Fragments: Macros can define code fragments or functions, making it easier to reuse blocks of code:
#define PRINT_HELLO() std::cout &laquo_space;“Hello, world!” &laquo_space;std::endl;

4) Conditional Compilation: Macros are often used to conditionally include or exclude sections of code, providing a way to create feature flags or customize program behavior at compile time.
#define DEBUG_MODE
#ifdef DEBUG_MODE
// Debugging code
#endif

5) Enhancing Code Reliability: Macros can enhance code readability by giving meaningful names to numeric or cryptic values, improving the understanding of the code.
#define SPEED_LIMIT_KMH 120

6) Avoiding Magic Numbers: Macros help avoid the use of “magic numbers” (unexplained constants) in the code by providing named constants.

63
Q

Why you should minimize your use of Macros in C++

A

To mitigate these eight pitfalls, it’s advisable to minimize the use of macros and prefer alternatives like const variables, enum, inline functions, and template functions, which provide type safety, better maintainability, and improved code readability. When using macros, follow best practices such as proper naming conventions, parentheses for safe macro expansion, and avoiding complex expressions within macros.

1) Symbol Clashes and Unexpected Behavior: Macros replace text globally, which can lead to unintended replacements and unexpected behavior if the macro name coincides with a variable, function, or other identifier in the code.

2) Lack of Type Checking: Macros do not undergo type checking since they are simply text substitutions. This can result in type-related errors that are hard to detect and debug.

3) Evaluation of Arguments Multiple Times: Macro arguments are often evaluated multiple times, which can lead to unintended side effects, especially when arguments have side effects.

4) Difficulty in Debugging: Debugging code that heavily relies on macros can be challenging, as macros obscure the actual code and can expand into complex and verbose expressions.

5) Readability and Maintainability: Overuse of macros can lead to reduced code readability and maintainability, as macros can hide the true intention and logic of the code.

6) Macros with Multiple Statements: Macros containing multiple statements without proper grouping can lead to unexpected behavior and errors.

7) Scope Issues: Macros are expanded globally, potentially causing unintended replacements across different parts of the code.

8) Non-Standard Behavior: Some compilers may implement macros differently or have extensions that behave in a non-standard way, leading to portability issues.

64
Q

Language and structure in C++ v. Python

A

C++: C++ is a statically typed, compiled language with a C-style syntax that requires explicit variable declarations, types, and memory management.
Python: Python is a dynamically typed, interpreted language with a clean and concise syntax that emphasizes readability and reduces the cost of program maintenance.

65
Q

“Typing” in C++ v. Python

A

C++: C++ uses static typing, where variable types are determined at compile time and type checking is performed during compilation.
Python: Python uses dynamic typing, allowing variables to hold values of any type, and type checking is performed at runtime.

66
Q

Memory Management in C++ v. Python

A

C++: In C++, memory management is explicit and requires developers to handle memory allocation and deallocation (e.g., using new and delete).
Python: Python employs automatic memory management and a garbage collector to handle memory allocation and deallocation, reducing the risk of memory leaks.

67
Q

Paradigms in C++ v. Python

A

C++: C++ supports both procedural and object-oriented programming paradigms and allows low-level memory manipulation.
Python: Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming, emphasizing code simplicity and high-level abstractions.

68
Q

Libraries and Ecosystems in C++ v. Python

A

C++: C++ has a rich set of libraries and frameworks, often focused on system-level programming, game development, and performance optimization.
Python: Python boasts a vast ecosystem of libraries and frameworks for a wide range of applications, including web development, data science, artificial intelligence, machine learning, and more.

69
Q

Compilation and Interpretation in C++ v. Python

A

C++: C++ code is compiled into machine-level instructions using a compiler, resulting in an executable file.
Python: Python code is interpreted and executed by the Python interpreter, without the need for compilation, resulting in .pyc (compiled bytecode) files.

70
Q

Development in C++ v. Development in Python

A

C++: C++ often requires more lines of code to achieve the same functionality, making development comparatively slower.
Python: Python’s concise syntax allows for faster development and easier prototyping of ideas

71
Q

Error Handling in C++ v. Python

A

C++: Error handling in C++ typically involves using return values, exceptions, or error codes.
Python: Python emphasizes exception handling as a standard way to deal with errors, making error handling more consistent and expressive.

72
Q

C++ Use Cases v. Python Use Cases

A

C++: C++ is commonly used for systems programming, embedded systems, high-performance applications, game development, and performance-critical applications.
Python: Python is widely used for web development, data analysis, machine learning, artificial intelligence, automation, scientific computing, and prototyping.

73
Q

Language and structure in C++ v. Java

A

C++: C++ is a statically typed, compiled language with a C-style syntax, requiring explicit variable declarations, types, and memory management.
Java: Java is a statically typed, compiled (to bytecode) language with a C-style syntax, emphasizing object-oriented programming and platform independence.

74
Q

Memory Management in C++ v. Java

A

C++: In C++, memory management is explicit, and developers are responsible for manual memory allocation and deallocation using new and delete.
Java: Java uses automatic memory management with garbage collection, relieving developers from explicit memory management concerns.

75
Q

Platform Independence in C++ v. Java

A

C++: C++ code is compiled to machine-specific binaries, making it platform-dependent and requiring recompilation for different platforms.
Java: Java code is compiled to platform-independent bytecode, which can run on any device with a Java Virtual Machine (JVM), enabling platform independence.

76
Q

Garbage Collection in C++ v. Java

A

C++: C++ does not have built-in garbage collection. Memory deallocation is performed explicitly by the programmer.
Java: Java has automatic garbage collection, freeing developers from managing memory deallocation manually.

77
Q

Pointers and References in C++ v. Java

A

C++: C++ allows the use of pointers, enabling direct memory manipulation and efficient low-level operations.
Java: Java does not have explicit pointers and does not allow direct memory manipulation for safety and security reasons.

78
Q

Multiple Inheritance in C++ v. Java

A

C++: C++ supports multiple inheritance, allowing a class to inherit from more than one base class.
Java: Java does not support multiple inheritance through classes, but it allows multiple inheritance of interfaces.

79
Q

Exception Handling in C++ v. Java

A

C++: C++ uses try-catch blocks for exception handling.
Java: Java also uses try-catch blocks for exception handling, and it enforces exception checking at compile time.

80
Q

Standard Libraries in C++ v. Java

A

C++: C++ has a rich set of standard libraries, known as the Standard Template Library (STL), providing data structures and algorithms for various tasks.
Java: Java’s standard library offers a comprehensive set of classes and methods, providing a wide range of functionalities for various programming needs.

81
Q

Use Cases for C++ v. Use Cases for Java

A

C++: C++ is commonly used for systems programming, game development, high-performance applications, and performance-critical applications.
Java: Java is widely used for web and mobile application development, enterprise applications, Android app development, backend development, and large-scale applications.