cmsc 202 exam 2

studied byStudied by 8 people
0.0(0)
learn
LearnA personalized and smart learning plan
exam
Practice TestTake a test on your terms and definitions
spaced repetition
Spaced RepetitionScientifically backed study method
heart puzzle
Matching GameHow quick can you match all your cards?
flashcards
FlashcardsStudy terms and definitions

1 / 67

flashcard set

Earn XP

Description and Tags

Pointers, Vectors, Dynamic Memory, Overloaded Operators, Linked Lists, Object Relationships

68 Terms

1
Parts of a variable
scope, memory address, variable name (identifier), and the value of the variable
New cards
2
Pointer
a type of variable that holds a memory address. Allocates 8 bytes
New cards
3
Initializing pointer
\*ptrVar = &var2; uninitialized pointers may cause issues in code if used. Types must match
New cards
4
Address-of operator
Gets the address of the variable
New cards
5
Deferencing pointers
Use the asterisk again on the variable to get the value of the variable
New cards
6
Pass by value parameters
The inserted argument in the parameter is passed by value, a local copy is created in the function and only changed there.
New cards
7
Pass by reference paramaters
void fxn(int &var); Gives function access to the original variable and changes it permanently. To protect var, use const keyword.
New cards
8
Function overloading
There is more than one function signature with same name but different parameter lists. Allows same tasks to be performed but with different data
New cards
9
Abstraction
Separates code use from implementation. Ex: user can use vectorName.push_back() but does not know how push_back was implemented
New cards
10
Encapsulation
A form of abstraction and information hiding. The data and functions that are located inside a class is considered encapsulation
New cards
11
Structure
collection of values of different types; has member variables and/or functions;
New cards
12
Initialization list
Used to initializing the data members of a class
New cards
13
Vectors
vector name; OR

vector name (size, n)
New cards
14
Disadvantages of vectors
inserting and deleting elements in the middle, sorting, and requires contigous block of memory
New cards
15
Copy constructor of vector
vector name (otherVector), creates a copy of another existing vector when declaring a new oen
New cards
16
Accessing vectors
vector\[index\] = num;

vector.at(index) = num;
New cards
17
At()
does bound checking and will throw an exception when out of bounds. Will cause program to terminate. Is slower than \[\]
New cards
18
Vectors as function parameters
Vectors are passed by value. Can pass by reference by using & in the function prototype
New cards
19
Resize()
Changes the size of the vector

void resize(n, val);

n is the new size, val is optional and used to initialize new elements
New cards
20
Push_Back
void push_back(val);

val is the new element being added to the vector. Must be the same type as the vector.
New cards
21
size()
Returns the number of elements in a vector;

returns an unsigned integer, may need to typecast it
New cards
22
Compile Time/Static Allocation
the memory allocated by the compiler for named variables;

use memory from the stack;

exact size and type of storage must be known at the compile time
New cards
23
Stack
where local variables and pass by value parameters are stored
New cards
24
Dynamic memory allocation
memory is allocated on the fly during runtime; the dynamic memory is placed in the heap;

pointers are necessary for dynamic allocation
New cards
25
Heap
where dynamically allocated memory is stored
New cards
26
Initializing a variable dynamically
\* ptrName = new varName;

\* ptrName = new varName(params);
New cards
27
Deallocating memory
when memory is being allocated dynamically, must delete it. use delete keyword

delete ptrName;

ptrName = nullptr;

For arrays: delete\[\]
New cards
28
Nullptr
indicates the pointer is not pointing to something
New cards
29
Destructor
For every class that dynamically allocates memory, a destructor is necessary. Destructors destroy the object and deallocates memory. Is called when delete keyword is used

\~ClassDestructor();
New cards
30
Memory leaks
occurs when dynamic memory is not allocated. If left unchecked, leak will accumulate (continually using computer resources) and crash program
New cards
31
Valgrind
Is used to check for memory leaks;

valgrind ./executablefile
New cards
32
Linked list
dynamic data structure that uses nodes and allows for easy insertion and deletion
New cards
33
Pros of linked list
changes size easily and constantly, easy insertion and deletion, only one node needs to be contigously stored
New cards
34
Cons of linked list
can’t randomly access data w/o sequential iteration, requires memory management, pointer to next node takes up more memory
New cards
35
Parts of a linked list
Must have a m_head but m_tail is optional. Last node must point to nullptr.
New cards
36
Node
an element of a linked list. Consists of two main parts: the data and the pointer pointing to the next node. Can be a class or struct
New cards
37
Deleting nodes
Can loop through linked list to individually delete each node. Set head, tail, and other node pointers (like temp) to nullptr.
New cards
38
Special cases for linked list
empty linked list, only one element in linked list, and multiple nodes in the linked list.
New cards
39
Linked list class implementation
Constructor, destructor, insert(), remove(), display/printList(), isEmpty()
New cards
40
Traversing through linked list
create node pointer Node\* temp = mhead;

create loop while (temp != mhead)

output data cout << temp→mdata/GetData() << “→”;

move temp temp = temp→mnext
New cards
41
Removing node at X position
check powerpoint lol…
New cards
42
Arrow operator
Deferences a pointer (\* asterisk operator)) and accesses member functions/variables (dot operator). Can be used when inserting nodes
New cards
43
Enumeration
Type of variable that holds a collection of named or unnamed constant integers.
New cards
44
Benefits of enums
allows user to restrict assignments to only valid values;

unnamed types can manage large list of constants but does not prevent invalid values
New cards
45
Enumeration variables
If the integer variable is not assigned a number, it will assign it the number of the previous variable + 1. Ex:

enum Random = {A = 1, B, C = 4, D}

B = 2 and D = 5. If A had no number, A = 0
New cards
46
Overloaded operators
Allows us to use operators that would not regularly work for user-defined items. Create overloaded operators as member functions in a class.
New cards
47
Operators that cannot be overloaded
scope ::, ternary ?:, dot ., asterisk \*
New cards
48
Friend
Friend keyword gives outside operators access to the private members in a class
New cards
49
Code Reuse
important for successful coding, efficient, and error free (code has been tested throughly)
New cards
50
Inheritance (is-a relationship)
When a specialized class (child, derived, subclass) inherits the members from a general class (parent, base, superclass)
New cards
51
How Child class can inherit Parent members
Members of the parent class must be in protected access modifier or use getters and setters if in private
New cards
52
Makefile for parent and child classes
Every child class should have a ParentClass.o so that it checks if the parent class was built first.
New cards
53
Composition (has-a relationship)
When two or more classes are dependent on each other. Ex: Car class has a Chassis object (Chassis is a class) in it. If car object is destroyed, so is chassis object
New cards
54
Aggregation (has-a relationship)
A class has some kind of link to another class through the use of a pointer. Ex: Car class is linked to a Driver object (Driver is a class) by using a pointer
New cards
55
Child’s use, extend, and replace
Use - use parent class behaviors (functions)

Extend - child creates new behaviors (specific for its class)

Replace - child classcan override the parent’s version of the same function
New cards
56
Replacing/Overriding
Uses the same function signature but different implementation. Only possible through inheritance. The child class can call either its version or parent’s version by using scope resolution operator. Ex:

Parent → Vehicle::Drive() Child → Car::Drive()
New cards
57
Overriding vs Overloading
Overriding takes precedence over overloading, so be careful if you want to overload functions within parent and child classes
New cards
58
Initialization lists
Populates variables without creating multiple objects at once. Ex:

Oboe(string name, int numReeds, string type) : Woodwind(name, numReeds) {}

Oboe is child class and Woodwind is parent
New cards
59
Polymorphism
The ability to manipulate objects in a type-independet way. Use a pointer to point at a parent class that will point to an object of a child class. Ex:

Vehicle \*vehiclePtr =&myCar
New cards
60
Limitation of polymorphism
Parent classes do not inherit the child class’ members so the pointer cannot access the child’s members. Use a virtual function
New cards
61
Virtual functions
Virtual functions allow the child class’ function to be called instead of the parent class’ in polymorphism. Use virtual keyword in the parent class’ prototype (in header file).

virtual void Drive();
New cards
62
Late binding
How virtual functions are implemented in C++. Late binding is determined at run time. The program will run but will not use the parent version’s of the class but instead the child class’ version.
New cards
63
Virtual functions and polymorphism
The program will run but will not implement the parent class’ version of a function but instead the child’s version
New cards
64
Purely virtual functions
virtual void Drive() = 0;

Any class that has a purely virtual function becomes an abstract class,

No implementation of the function in parent class but must implement the function in child class
New cards
65
Abstract class
A class that has a purely virtual function. Cannot create any objects of the class.
New cards
66
Virtual function tables
Compiler uses these tables whenever polymorphism is used. These tables are created for classes with virtual functions and their child classes
New cards
67
Virtual destructor
Must be declared for any class that has virtual functions. If not declared, non-virtual destructors only invoke the base class’ destructor.
New cards
68
Virtual constructor
No need for them. Constructors require complete information at the time of being called but virtual functions do not have the complete information at the time.
New cards

Explore top notes

note Note
studied byStudied by 14 people
1005 days ago
4.0(1)
note Note
studied byStudied by 162 people
624 days ago
5.0(1)
note Note
studied byStudied by 16 people
122 days ago
5.0(1)
note Note
studied byStudied by 22 people
743 days ago
5.0(1)
note Note
studied byStudied by 61 people
882 days ago
4.0(1)
note Note
studied byStudied by 8 people
176 days ago
5.0(1)
note Note
studied byStudied by 10 people
898 days ago
5.0(1)
note Note
studied byStudied by 255 people
686 days ago
4.8(9)

Explore top flashcards

flashcards Flashcard (127)
studied byStudied by 31 people
911 days ago
5.0(1)
flashcards Flashcard (20)
studied byStudied by 19 people
266 days ago
5.0(1)
flashcards Flashcard (20)
studied byStudied by 8 people
784 days ago
5.0(1)
flashcards Flashcard (28)
studied byStudied by 29 people
737 days ago
5.0(2)
flashcards Flashcard (67)
studied byStudied by 9 people
837 days ago
5.0(1)
flashcards Flashcard (315)
studied byStudied by 51 people
763 days ago
5.0(4)
flashcards Flashcard (29)
studied byStudied by 15 people
379 days ago
5.0(1)
flashcards Flashcard (26)
studied byStudied by 84 people
17 days ago
5.0(1)
robot