EECS 280 –Final Cheat Sheet
Const Keyword (Read Inside-Out):
Const pointer: const int *ptr = &x;
Pointer-to-const: int * const ptr = &x;
double perimeter const {}, where const means the function will not modify
...
EECS 280 –Final Cheat Sheet
Const Keyword (Read Inside-Out):
Const pointer: const int *ptr = &x;
Pointer-to-const: int * const ptr = &x;
double perimeter const {}, where const means the function will not modify any member variables
Arrays:
arr[i] is equivalent to *(arr + i)
Traversal by pointer: for (int *ptr = arr; ptr < arr + SIZE; ++ptr) { }
Container ADTs:
A container is an ADT whose purpose is to hold other objects. (ex. arrays, vectors)
Representation Invariants - express the conditions for a valid compound object.
Static keyword - data member with this modifier is shared between all instances of the class
o Lives throughout the program, but within the class's scope.
size_t (unsigned integer) - guaranteed to hold members large enough to represent the largest possible size of an object or a container
Operator Overloading - overloading of operators to tell them what to do (help handle non-primitive types)
o std::ostream & operator<<(std::ostream &os, const T &item) // Insertion Operator
o value_t & operator[](std::size_t index) // Subscript Operator
o T & operator=(const T &other) // Assignment Operator
o bool operator< (const Iterator &lhs, const Iterator &rhs) // Less than Operator
Templates:
The compiler can see which kinds of the class you use and instantiates (produces) a version of the code for each different type T.
template
using Set = UnsortedSet; // Type Aliasing
template
void fillFromArray(Set &set, const T *arr, int n); // Template Parameter List
Template instantiation is done during compilation proper, which is before the linking phase.
Managing Dynamic Memory (Requires BIG THREE):
When a class-type object is created, its constructor runs. (top-down behavior)
o For a local object, when its declaration runs.
o For a dynamic object, when it is created with new.
When a class-type object dies, its destructor runs. (bottom-up behavior)
o For a local object, when it goes out of scope.
o For a dynamic object, when it is destroyed with delete.
The lifetimes of the members of a class-type object are tied to the lifetime of the whole object itself.
o After its body executes, the destructor of a class-type object automatically runs the destructors of its members that are of class type.
Memory Leaks - You’re not using dynamic memory, but you never free up the space for it.
Orphaned Memory - You lose the address of a heap object, meaning it will inevitably be leaked.
Double Free - You try to free heap memory too many times.
Bad Delete - Use delete with a pointer to a non-heap object.
Dangling Pointers - You keep around the address of a dead heap object.
Special syntax for deleting arrays: delete[] arr; // Deletes array, not the pointer. Other methods won’t work.
Deep Copies and The Big Three (Destructor, Copy Constructor, Assignment Operator):
Destructors – deallocate dynamically allocated memory // Implicit – basic clearing of local variables
o If there are subtypes, ALWAYS make the destructor virtual.
Copy Constructor – initializes a compound from another (makes a deep copy) // Implicit – shallow copy of other
o Also used when an object is passed by value in parameter passing
[Show More]