01. C++ Flashcards
(43 cards)
What is a class in C++? Describe what a class is.
We use classes in C++ to define new variable types. We can refer to these types of variables as “complex” variables, which have state (object private variables), and a set of methods associated with the object.
What 2 things do we separate when making our C++ classes?
When writing our classes, we separate the API (.h) file and the implementation (.cpp) file. When we create a class in C++ we separate our class in two different files.
API (.h): This is defines what the class is supposed to do (defines the class) Implementation (.cpp): This file implements and defines how the class is supposed to do the defined behavior in the API
What do classes contain? What is the difference between “define” and “declare” when it comes to classes?
Classes are like containers that hold (1) variables (state), and (2) methods (behaviors).
In the .h file, a class is defined while the components of that class are declared which are the variables and methods.
Class -> defined
Variables/Methods -> declared
Implementation of variables and methods in .cpp file is -> defining
What are the roles of “inclusion guards” #ifndef?
This is an if condition for the compiler to check to see if the class has already been included in a .cpp file, if it hasn’t been defined yet, then it will include it.
What are public class member methods?
These are class variables/methods that are accessible from other classes. That means other classes can call these methods if they instantiate this object.
What are private class member variables?
These are private variables that are not accessible directly, and instead we have to create getter() and setter() methods that need to be public in order to access/modify these private variables.
What is the scope resolution operator “::”?
The scope resolution operator says a method() belongs to the class which is specified before the double column.
double Sphere::getRadius() {…}
// Class: Sphere // Method: getRadius()
What is Encapsulation and explain in detail?
Encapsulation is the process of combining data and functions into a single unit called a class. In encapsulation a class’s data is not accessed directly, it’s accessed through defined public functions (getters and setters) that are provided to manipulate these attributes. Encapsulation makes the concept of “data-hiding” possible.
What are namespaces in C++, how are they used and what is their benefit?
Namespaces in C++ allows us to organize a bunch of classes into packages (namespace), the benefits being that it large projects it helps from name collisions between classes.
For example, “sort()” is a common method name for a class that can conflict with the STL, so having a class in a anamespace can help the compiler call the right sort() function.
In namespaces you CANNOT have 2 classes with the same name, remember that.
When adding header files to a C++ program, what is the difference between when we use quotes “..” vs. angle brackets ?
Quotes: (#include “cube.h”) tells the compiler to look for the file in our current directory Angle Brackets: (#include ) tells the compiler to look at system headers
What are some situations you’d use for private variables/methods? What about public?
Here are a few situations I’d use private protection levels for my class
A helper function to use internally that supports some public function (Ex: _clear() for ~Sphere()) State variable data for our class Here is a situation where I'd use a public protection level
I would have key functionality public so I can have client code interact with the API
What are the 3 types of constructors in C++? Also describe them.
Automatic Default Constructor: This is called when no constructor for a class is defined, it is gone when we define a custom constructor
Custom Default Constructor: This is a 0 parameter constructor we define, it will initialize an object to its default values
Custom Non-Default Constructor: This is a constructor we define in our classes that require parameters that will be assigned to objects member variables
Pointers & References, what are they able to provide us?
Pointers and References are able to allow a level of indirection via memory to the data
What does providing a “level of indirection via memory” mean?
Basically what it means is that in our program, we can have:
Multiple variables that refer to the same piece of memory
Pointers/References do not create their own memory objects
Sphere s1; // Variable of type Sphere
Sphere *s2; // Pointer that points to Sphere
Sphere %s3; // Reference variable of type Sphere
What is a Reference Variable, describe how it works, and what are the main 3 rules for reference variables?
A reference variable is an alias to an existing variable. That means modifying the reference variable actually modifies the variable the reference was refering to. A reference variable simple maps to the same memory as the variable being aliased.
3 Rules for Reference Variables:
R.V. must ALWAYS alias another variable, it cannot be set to NULL
R.V. must always be initialized, and cannot be changed
R.V. do NOT create their own memory
http://prntscr.com/jfy1r6
What are pointers and what are the main characteristics of pointers?
Pointers store the memory address of the contents they are pointinter to. (8 bytes generally but remember it is SYSTEM DEPENDENT, so 8 bytes on a 64-bit)
Here are some of the characteristics of pointers:
Pointers do have their own memory as opposed to reference variables. This memory stores the memory address of what they are pointer to, remember that memory gets allocated for a pointer, and this stores a memory address.
Pointers don’t have to be initialized immediately, AND can be set to NULL. When a pointer is set to NULL, it simply stores the 0x0 memory address, whereas reference variable cannot alias “NULL”
Diagram: https://eli.thegreenplace.net/images/2009/10/ptr_place.png
What are the 3 indirection operators and how do each of them work?
&v = returns the memory address that’s storing v’s data
*v = returns the data that is located at the memory address contained in v
v-> = returns the member of an object (v->getRadius()), and is the same as (*v).getRadius()
How does Stack memory work?
Stack memory is the memory that is set aside as scratch space for a thread of execution. So when a function is called, a stack frame is created for that function for it to be able to execute, and remeber that this stack frame is automatically marked free (not actually freed) so it can later on be overwritten.
Here are some rules to follow regarding stack memory to remember:
- Never return a pointer to the stack variable
- Variables created on the stack will go out of scope and are automatically deallocated
- The stack is also faster than the heap
- Stacks can eventually overflow, it has a maximum size
- You should use stacks when you know exactly how much data you need to allocate and it’s not too big
Stack memory start at a very large value and grows downwards to 0. Memory is allocated first, then data is written.
http://prntscr.com/jfy28i
What is a Stack Frame?
All programs are organized into stack frames
A stack frame is created whenever a function is called
A stack frame is marked free when a function returns (not actually freed), so now that a “marked free” memory can be overwritten
Never return a &n for a function
What is heap memory? How does it work and why do you use it? What does “new” do? How do you free heap memory?
Heap memory is memory that is used on demand because we explicitly write code to allocate memory on the heap. We use it in cases where the lifecycle of the variable exeeds the lifecycle of the function.
The way you allocate heap memory is by using the new keyword which does 3 main things:
Allocate heap memory
Call the object’s constructor
Returns the pointer to memory
The way to free heap memory is by calling the delete keyword that does 2 things:
Calls the object’s destructor
Marks the memory as free
What is leaked memory?
Remember that in heap memory, memory is never automatically reclaimed even if it goes out of scope, it will still be lingering on the heap until we explicitly free that memory with delete or delete[] keyword.
That means for each “new” there must be a “delete”. Programs that are forget to reclaim memory will run into problems, lets say you are running server code that is leaking memory that is constantly running 24/7, that program is eventually going to run into a problem when you run out of space because there had been memory leaks on the server the whole time.
What is the difference between a Deep Copy vs. a Shallow Copy?
A deep copy actually copies over all the data from one data structure to another. A shallow copy copies simply the reference.
Shallow Copy
So for example, let’s say we have 2 arrays A and B, this is how a Shallow Copy would look like:
http://prntscr.com/jfy92l
Shallow Copy Explanation: Variables A and B refer/point to 2 different areas in memory, when B is assigned to A, the 2 variable now refer to the same area in memory. That means from this point forwards, any modifications to this area will reflect in both A and B, so if you were to make a change such as changing the 2nd element of B to K after the shallow copy, then A[1] would return K as well because they are the same array. In the shallow copy above, B is simply assigned to A.
Deep Copy
This is how a Deep Copy would look like with the 2 arrays:
http://prntscr.com/jfy96x
Deep Copy Explanation: This time the elements in array A get copied over to array B. That means that if we were to modify any two of these arrays, they will reflect changes to just the arrays they are pointing to as opposed to a shallow copy that has changes reflecting in both arrays due to a pointer being moved. So in a deep copy, remember that actual data is copied over as opposed to just copying the reference.
How do we allocate and free arrays in C++?
Simply use the new[size] (size is a parameter to pass in in) keyword to allocate an array onto the heap. Make sure to use the delete[] keyword to free all the memory of the array.
http://prntscr.com/jfy9ap
What are the 3 ways to pass function parameters?
Pass-by-Value
Pass-by-Reference
Pass-by-Pointer