Theory Exam 1 Flashcards

1
Q

Public members functions

A

A public member function in a Class in C++ would be a function that an external class can call because it is a public function

This would include functionality an external class could use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Private helper functions

A
Private helper functions are functions that are used internaly in lets say a Sphere class
We will see this in the class we come up with that talks about this
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Private variables

A

Private variables of a class in C++ are private because they contain data about a specific object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Constructors

A

Every class in C++ has a constructor, even if you didn’t define one

3 Types of them

Automatic Default
Custom Default
Non-Custom Default

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Automatic Default Constructor

A

If there is no constructor defined, then C++ will define an automatic default constructor for our class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Custom Default Constructor

A

This is a constructor we define ourselves, however it will have 0 parameters when called, and will initialized all the member variables of your object to default values that you programmed in this default constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Custom Non-Default Constructor

A

This would be a construtor that you would define yourself, however you actually have the constructor take in parameters so the object is filled in with initial default values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Encapsulation

A

Classes provide the programmer to encapsulate all the behavior to a class, sort of like creating a certain type of object that has state variable and methods, usually the methods are private to manipulate the objects data, and of course we have constructors as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Namespaces in C++

A

Creating a class that is part of a namespace (eg: Sphere is part of the cs225 namespace)

Using a class from a namespace (eg: cs225::Sphere)

using namespace cs225

Purpose and usefulness of namespaces

What is the purpose of a namespace?

What is the usefulness of a namespace?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Variables

A

Four properties: name, type, location (in memory), and value

Name of variable: This would just be the name of the variable
Type of Variable: Type would either be primitive (int, double, float, char, etc.), or user-defined which means we are instantiating a class which would make a certain type of variable
Location (in memory): Stack if not declared with the “new” keyword, othersewise if “new” is used, the variable will be on the heap
Value: the value of the variable would be whatever we asign it

Primitive vs User-Defined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Primitive vs. User-defined Variables

A

A Primitive Variable: A primtive variable include, int, double, char, string (if header is in there), float, etc. etc.
User Defined Variable: These are variable that a user defines with a class, and can use a typedef here

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Indirection in C++

A

What is indirection mean in C++?

Basically what we are doing when creating pointers and references is that we are able to create multiple variables that refer to the same piece of memory
int i = 7;
int &y = i; // y is an alias of i, which means if we modify y, then we will be modifying i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

2 Rules of Indirection **

A

Multiple variables can refer to the same piece of memory

Pointers and References don’t create their own objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Pointers and References in context of indirection

A

We use Pointers and References in C++, these are a level of indirection via memory to the data.
The indirection operator is a * in C++

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Reference variables

What is a Reference Variable?

A

A reference variable is an alias to an existing variable. That means that modifying the reference variable actually modifies the variables that is being aliased. So then we may ask, what is going on internally?
Internally, a reference variable maps to the same memory as the variable being aliased.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

3 Rules of Reference Variables

A

A reference variable MUST ALWAYS alias another variable, it cannot be NULL

A reference variable must be initialized when it is created, it cannot be alone.

int z = 1
int &x = z

17
Q

Pointers, How big are pointers and what are they exacty?

A

Just a memory address, that points to another piece of memory

Alright, another way we can access data is through pointers, and we know that with pointers they are 8 bytes long, and point to a piece of memory, wether that memory is in the heap or stack.

18
Q

Do pointers have their own memory? And how is that relevant to References?

A

KEY**: Pointers are variables with their OWN memory, really bring it home that a reference variable is basically a mask/alias for another variable, it does NOT have it’s own memory, while a pointer does have it’s own memory, one of that of allocating the pointer itself.

Pointers store the memory address of the contents they’re “pointing to”

19
Q

** 3 Rules of Pointers to REMEMBER **

A

Pointers are a variable and a type (it has it’s own memory)
Pointers store a memory address that’s it, but the way we visualize it is to draw diagrams of a memory cell pointing to another cell
Pointers may point to nothing (NULL), pointers are allowed to store the 0x0 memory address, as opposed to reference variables that cannot alias a NULL

20
Q

Indirection Operators

A

Indirection operators are used in code, there are 3, make sure you know what they return

&v : &v returns the memory address that’s storing v’s data

*v : *v returns the data at the memory address contained in v, basically returns data that v is pointing to

v-> : v-> exact same as a dereference, accesses member of an object pointer

(*v).getRadius() === v->getRadius()

21
Q

What is stack memory and how does it start?

A

Stack memory starts at vary large values and grows towards 0.

22
Q

How is data read on the stack and the process?

A

Memory is allocated first, then data is written. Data is read upwards in a stack.

By default. all variables are in stack memory

23
Q

What is a stack frame?

A

A stack frame is a reference to all variables including the parameters to a function that are part of a function are part of a function’s stack frame.

24
Q

2 Rules on Stack Frames

A

Stack frames are created when a function is called

They are marked free when a function returns

25
Q

Big RULE/MISTACK on Stack Memory?

A

Never return a reference to stack memory in a function. Never, that means a function should never return &n

26
Q

What is heap memory, and why is it used?

A

Used because:

We can use heap memory in cases where the lifecycle of the variable exceeds the lifecycle of a function

27
Q

How to create heap memory?

A

Key #1: The only way to create heap memory is to use the “new” keyword. Using “new” will do these 3 things you need to memorize:

28
Q

3 Rules of the “new” keyword used to allocate memory on the heap

A

Using “new” will do these 3 things you need to memorize:

  1. Allocate heap memory
  2. call the object’s constructor if needed
  3. Return a pointer to memory
29
Q

Where does heap memory start and how does it grow?

A

Heap memory starts from a low address and grows upward to infinite (until the machine runs out of storage)

30
Q

How do you “free” up heap memory?

A

Use the “delete” keyword which will do the 2 following things:

  1. Call the object’s destructor
  2. Mark the memory as free
31
Q

What is leaked memory?

A

Key #3: The memory is never automatically reclaimed, even if it goes out of scope. Any memory lost, but not freed, is considered to be “leaked memory”
For each “new” there must be a delete, or else you’ll have leaked memory.

32
Q

What are the 3 keys to Heap memory?

A

Key #1: The only way to create heap memory is to use the “new” keyword. Using “new” will do these 3 things you need to memorize:

Key #2 :The only way to “free” heap memory is to use the delete keyword which will do 2 things, when using delete:

Key #3: The memory is never automatically reclaimed, even if it goes out of scope. Any memory lost, but not freed, is considered to be “leaked memory”

33
Q

Functions: Calling and Returning

A

What are the 3 types of args you can pass into a function?

Pass by value, pointer, or reference(alias)

34
Q

What is Pass by Value refer to?
What is exactly copied when the function is invoked?
Does the modification of the passed in object modify the caller’s object?
Is there always a valid object passed into the function?
Speed?
Safety?
By default what are the 2 things that happen when you pass something in by value?

A

What is exactly copied when the function is invoked?
The entire object gets copied when you pass an object by value to a function

Does the modification of the passed in object modify the caller’s object?
No

Is there always a valid object passed into the function?
Yes

Speed?
Might be slow because we are copying data

Safety?
Safe

By default what are the 2 things that happen when you pass something in by value?
Variables are coped to the function’s stack frame
Modifications don’t change the callers data, just changes the data of the copy

35
Q

What is Pass by Reference refer to?
What is exactly copied when the function is invoked?
Does the modification of the passed in object modify the caller’s object?
Is there always a valid object passed into the function?
Speed?
Safety?

A

What is exactyle copied when the function is invoked?
Nothing gets copied

Does the modification of the passed in object modift the caller’s object?
yes

Is there always a valid object passed into the function?
yes

Speed?
Fast

Safety?
Somewhat safe

36
Q

What is Pass by Pointer refer to?
What is exactly copied when the function is invoked?
Does the modification of the passed in object modify the caller’s object?
Is there always a valid object passed into the function?
Speed?
Safety?

A

What is exactyle copied when the function is invoked?
1 pointer which is 8 bytes, which is a memory address that points to a piece of memory

Does the modification of the passed in object modift the caller’s object?
Yes

Is there always a valid object passed into the function?
No

Speed?
Fast

Safety?
Unsafe