1/63
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What happens in a shallow copy of an object with dynamic memory?
Pointer address is copied, but not actual memory contents
Why is a deep copy needed for classes with dynamically allocated memory?
To ensure each object has its own separate copy of data
Main issue with default copy constructor for an object having a pointer?
Could lead to double delete error by creating a shallow copy
If not explicitly defined, the copy constructor does ____ by default?
Copies all member variables and pointers (using memory address)
Common problem caused by shallow copies?
Double deletion of dynamically allocated memory
Correctly implement a deep copy?
Allocate new memory + copy contents of original obje
Best way to prevent shallow copy problems in a dynamic memory class
Implement a custom copy constructor that performs a deep copy!
If an object uses dynam mem, what happens when the destructor is missing
Memory leaks occur, memory has not been freed!!
Rule of 3?
For any class using dynam mem, it should have: destructor, copy constructor, and copy assignment operator.
Safest way to handle copying if a class manages a resource using dynam mem/files?
Define copy constructor AND assignment operator for deep copying
Destructor example?
~LinkedList();
Memory leak
Happens whenever there is no DELETE
Destructor purpose?
Deallocate dynam alloc data and clean up object
Equivalent statement to: (*sample1).ShowData()
sample1->ShowData();
Dynamic array size vs capacity
equivalent, fixed
Declare unique_ptr of type Food
unique_ptr<Food> f(new Food());
A date class destructor example
Date::~Date() {…}
Replace “usa” with “USA“
strncpy(stringPosition, “USA”, 3);
Declare a copy assignment operator
PatientDetails& operator=(const PatientDetails& inVal);
NEW operator calls constructor…
AFTER dynamically allocating memory
Not in Rule of 3
default constructor
Delete a dynamic array y
delete[] y;
Static arrays dont need…
pointers
Herp memory
Dynamic memory, using new and delete
Stack memory
Automatic, whenever declaring variables as normal
(T/F) the program can adjust the array’s size anytime.
F
strrchr():
returns a pointer to the last occurrence of the character
Code memory region
Program instructions are stored
Garbage collection
Automatic freeing of unreachable memory
Stream error check
if (!inFS.fail()) {…}
Check if file is open
inFS.isopen(“data.txt”);
Extraction operator
>>
Manipulators are defined in
<iomanip> <iostream
Output of: cout « setprecision(3) << scientific << 3.14
3.140e+00
Has-a relationship example
School-Teacher, meaning one class owns another class as part of data members (in private?)
a derived class with a member function of same name/parameters/return type as base class, that member function is said to ____ the base class’s function
OVERRIDE
Is-a
Meaning a derived class inherits from base class (Dog is-a animal)
Inheritance operator
“:”
(T/F) A class can serve as a base class for multiple derived classes
True
Compile time polymorphism (earlier)
Compiler knows which function to call at compile time, used with function/operator overloading
Run-time polymorphism (later)
Used with virtual functions, only knows which function to call at run-time, used with pointers to inherited objects
Abstract classes
Class that guides the design of subclasses, no objects can be created and must have at least one pure virtual function (usually function = 0)
Pointers + Inheritance
Needed for polymorphism
Stack vs Heap
Stack for automatic processes,
heap for dynamic processes (new/delete)
Constructor Order
Base class run first, then derived class
Destructor order
derived class run first, then base class!!!
Virtual function (for run-time polymorphism!)
function in base class that can is “overridden” in derived class
Pure virtual function (for run-time polymorphism!)
virtual function, not implemented in base class so it forces derived classes to override it with implementations! Declared “= 0“, cannot be instantiated
friend function
non-member function that has access to private data of a class
constructor
special function used to initialize objects when they are created
Destructor
cleans up resources when object is destroyed (called when object out of scope)
stream
bytes flowing between a program and data source/destination
input stream
reads data from keyboard/file into program
output stream
sends data from a program to screen or file
istream
data flowing into programo
ostream
data flowing out of program
Insertion operator
<<
Extraction operator
>>
stream manipulators
functions like setw/setfill/setprecision that format output
string stream
treats a string as input/output like a file stream
is_open()/fail()
functions used to check if file stream opened/closed
dangling pointer
pointer that references deallocated memory
Concrete class
class that is not abstract
unique_ptr<T>
smart pointer that owns dynamically allocated object of type T, automatically deletes when object goes out of scope