C++ Flashcards
What does the C++ compiler do when it encounters the “#include” preprocessor directive?
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)
Purpose of “#ifndef”
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
Referring to the location in which a particular function is stored
‘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
Passing something by value versus Passing something by reference
Object Slicing Problem
-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
“virtual” reserved word
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
Arguments
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]
Parameters
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]
Pure Virtual Functions
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
Static Allocation
Performed at compile time
Indirect Addressing
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.
Addresses in memory
Every byte is counted. It has it’s own number that represents it. That number is it’s address
Pointer
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.
Using a star * before the name of your integer variable
Treats the value as a POINTER. You are directing the machine the memory address associated with that particular number
Using an ampersand & before the name of your integer variable
Advantage of using pointers
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
Using an arrow in a code -> (with respect to pointers)
This means that you are dereferencing the pointer
Dynamic Allocation
Allocation of memory space at run time
The Heap (Free store)
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
Dynamic allocation Operator: “new”
“New” allocates memory to hold an int and stores the address into the variable
“The Stack”
Memory Leak
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
Garbage
Memory locations that can no longer be accessed.
This is created when you update what a pointer is pointing toward
Inaccessible object
A dynamic variable on the free store without any pointer pointing to it
Dangling Pointer
A pointer that points to a variable that has been deallocated
deallocation operator: “delete”
Stack overflow v. Memory Leak
Robustness
The ability of a program to recover following an error
Four Types of Errors
- A user may accidently or deliberately (hackers) enter incorrect inputs
- 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)
- Hardware devices may fail or becomes inaccessible
- Software components may contain defects (bugs)
Six options for handling errors
- Assume errors will not occur (not an option)
- Print a descriptive error message
- Return an unusual value to indicate an error has occurred
- Alter a status variable’s value
- Use assertions to block further execution
- Use exception handlers
Exception
Basically an anomaly.
An unusual event, detectable by software or hardware, that requires special processing
Exception Handler
A section of program code that is executed when a particular exception occurs