CPSC 1020 Exam 2 - Zybooks (8-10)

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/63

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

64 Terms

1
New cards

What happens in a shallow copy of an object with dynamic memory?

Pointer address is copied, but not actual memory contents

2
New cards

Why is a deep copy needed for classes with dynamically allocated memory?

To ensure each object has its own separate copy of data

3
New cards

Main issue with default copy constructor for an object having a pointer?

Could lead to double delete error by creating a shallow copy

4
New cards

If not explicitly defined, the copy constructor does ____ by default?

Copies all member variables and pointers (using memory address)

5
New cards

Common problem caused by shallow copies?

Double deletion of dynamically allocated memory

6
New cards

Correctly implement a deep copy?

Allocate new memory + copy contents of original obje

7
New cards

Best way to prevent shallow copy problems in a dynamic memory class

Implement a custom copy constructor that performs a deep copy!

8
New cards

If an object uses dynam mem, what happens when the destructor is missing

Memory leaks occur, memory has not been freed!!

9
New cards

Rule of 3?

For any class using dynam mem, it should have: destructor, copy constructor, and copy assignment operator.

10
New cards

Safest way to handle copying if a class manages a resource using dynam mem/files?

Define copy constructor AND assignment operator for deep copying

11
New cards

Destructor example?

~LinkedList();

12
New cards

Memory leak

Happens whenever there is no DELETE

13
New cards

Destructor purpose?

Deallocate dynam alloc data and clean up object

14
New cards

Equivalent statement to: (*sample1).ShowData()

sample1->ShowData();

15
New cards

Dynamic array size vs capacity

equivalent, fixed

16
New cards

Declare unique_ptr of type Food

unique_ptr<Food> f(new Food());

17
New cards

A date class destructor example

Date::~Date() {…}

18
New cards

Replace “usa” with “USA“

strncpy(stringPosition, “USA”, 3);

19
New cards

Declare a copy assignment operator

PatientDetails& operator=(const PatientDetails& inVal);

20
New cards

NEW operator calls constructor…

AFTER dynamically allocating memory

21
New cards

Not in Rule of 3

default constructor

22
New cards

Delete a dynamic array y

delete[] y;

23
New cards

Static arrays dont need…

pointers

24
New cards

Herp memory

Dynamic memory, using new and delete

25
New cards

Stack memory

Automatic, whenever declaring variables as normal

26
New cards

(T/F) the program can adjust the array’s size anytime.

F

27
New cards

strrchr():

returns a pointer to the last occurrence of the character

28
New cards

Code memory region

Program instructions are stored

29
New cards

Garbage collection

Automatic freeing of unreachable memory

30
New cards

Stream error check

if (!inFS.fail()) {…}

31
New cards

Check if file is open

inFS.isopen(“data.txt”);

32
New cards

Extraction operator

>>

33
New cards

Manipulators are defined in

<iomanip> <iostream

34
New cards

Output of: cout « setprecision(3) << scientific << 3.14

3.140e+00

35
New cards

Has-a relationship example

School-Teacher, meaning one class owns another class as part of data members (in private?)

36
New cards

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

37
New cards

Is-a

Meaning a derived class inherits from base class (Dog is-a animal)

38
New cards

Inheritance operator

“:”

39
New cards

(T/F) A class can serve as a base class for multiple derived classes

True

40
New cards

Compile time polymorphism (earlier)

Compiler knows which function to call at compile time, used with function/operator overloading

41
New cards

Run-time polymorphism (later)

Used with virtual functions, only knows which function to call at run-time, used with pointers to inherited objects

42
New cards

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)

43
New cards

Pointers + Inheritance

Needed for polymorphism

44
New cards

Stack vs Heap

Stack for automatic processes,

heap for dynamic processes (new/delete)

45
New cards

Constructor Order

Base class run first, then derived class

46
New cards

Destructor order

derived class run first, then base class!!!

47
New cards

Virtual function (for run-time polymorphism!)

function in base class that can is “overridden” in derived class

48
New cards

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

49
New cards

friend function

non-member function that has access to private data of a class

50
New cards

constructor

special function used to initialize objects when they are created

51
New cards

Destructor

cleans up resources when object is destroyed (called when object out of scope)

52
New cards

stream

bytes flowing between a program and data source/destination

53
New cards

input stream

reads data from keyboard/file into program

54
New cards

output stream

sends data from a program to screen or file

55
New cards

istream

data flowing into programo

56
New cards

ostream

data flowing out of program

57
New cards

Insertion operator

<<

58
New cards

Extraction operator

>>

59
New cards

stream manipulators

functions like setw/setfill/setprecision that format output

60
New cards

string stream

treats a string as input/output like a file stream

61
New cards

is_open()/fail()

functions used to check if file stream opened/closed

62
New cards

dangling pointer

pointer that references deallocated memory

63
New cards

Concrete class

class that is not abstract

64
New cards

unique_ptr<T>

smart pointer that owns dynamically allocated object of type T, automatically deletes when object goes out of scope