Theory Exam 1 Flashcards
Public members functions
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
Private helper functions
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
Private variables
Private variables of a class in C++ are private because they contain data about a specific object
Constructors
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
Automatic Default Constructor
If there is no constructor defined, then C++ will define an automatic default constructor for our class.
Custom Default Constructor
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
Custom Non-Default Constructor
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
Encapsulation
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.
Namespaces in C++
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?
Variables
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
Primitive vs. User-defined Variables
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
Indirection in C++
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
2 Rules of Indirection **
Multiple variables can refer to the same piece of memory
Pointers and References don’t create their own objects
Pointers and References in context of indirection
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++
Reference variables
What is a Reference Variable?
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.
3 Rules of Reference Variables
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
Pointers, How big are pointers and what are they exacty?
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.
Do pointers have their own memory? And how is that relevant to References?
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”
** 3 Rules of Pointers to REMEMBER **
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
Indirection Operators
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()
What is stack memory and how does it start?
Stack memory starts at vary large values and grows towards 0.
How is data read on the stack and the process?
Memory is allocated first, then data is written. Data is read upwards in a stack.
By default. all variables are in stack memory
What is a stack frame?
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.
2 Rules on Stack Frames
Stack frames are created when a function is called
They are marked free when a function returns