01. C++ Flashcards
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()