01. C++ Flashcards

1
Q

What is a class in C++? Describe what a class is.

A

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.

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

What 2 things do we separate when making our C++ classes?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What do classes contain? What is the difference between “define” and “declare” when it comes to classes?

A

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

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

What are the roles of “inclusion guards” #ifndef?

A

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.

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

What are public class member methods?

A

These are class variables/methods that are accessible from other classes. That means other classes can call these methods if they instantiate this object.

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

What are private class member variables?

A

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.

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

What is the scope resolution operator “::”?

A

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()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Encapsulation and explain in detail?

A

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.

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

What are namespaces in C++, how are they used and what is their benefit?

A

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.

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

When adding header files to a C++ program, what is the difference between when we use quotes “..” vs. angle brackets ?

A

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

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

What are some situations you’d use for private variables/methods? What about public?

A

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

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

What are the 3 types of constructors in C++? Also describe them.

A

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

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

Pointers & References, what are they able to provide us?

A

Pointers and References are able to allow a level of indirection via memory to the data

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

What does providing a “level of indirection via memory” mean?

A

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

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

What is a Reference Variable, describe how it works, and what are the main 3 rules for reference variables?

A

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

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

What are pointers and what are the main characteristics of pointers?

A

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

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

What are the 3 indirection operators and how do each of them work?

A

&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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How does Stack memory work?

A

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:

  1. Never return a pointer to the stack variable
  2. Variables created on the stack will go out of scope and are automatically deallocated
  3. The stack is also faster than the heap
  4. Stacks can eventually overflow, it has a maximum size
  5. 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

19
Q

What is a Stack Frame?

A

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

20
Q

What is heap memory? How does it work and why do you use it? What does “new” do? How do you free heap memory?

A

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

21
Q

What is leaked memory?

A

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.

22
Q

What is the difference between a Deep Copy vs. a Shallow Copy?

A

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.

23
Q

How do we allocate and free arrays in C++?

A

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

24
Q

What are the 3 ways to pass function parameters?

A

Pass-by-Value
Pass-by-Reference
Pass-by-Pointer

25
Q

Explain what does it mean to pass-by-value?

A

When we pass parameters to a function by value, we are simply passing in a copy of that variable, so any modification to that copy doesn’t change the data in the main program, and simply modifiies the copy in the function.

This is safe, but it needs extra memory, which matters when you are copying larget amounts of data into the function’s stack frame.

26
Q

Explain what it means to pass-by-reference?

A

When you pass in a parameter by reference, we are passing an alias to the variable, and this time the modification to that parameter will actually change the data of that object in the main program.

Nothing to be copied when passing by reference, it’s fast, somewhat safe, and a valid object is always passed into the function.

27
Q

Explain what it means to pass-by-pointer?

A

When we pass in a value bu pointer into a fcuntion, it means we are passing a pointer to the original data, and by changing the parameter, we also actually modify the object being passed in.

We do copy 8 bytes (system dependent) for the pointer itself, it’s also fast but unsafe because we don’t really know if a valid object is being passed into the function.

28
Q

Explain the “const” keyword and why/when do we generally use it?

A

The ‘const’ keyword is a way to prevent parameters that are being passed into function to be changed. That means if we pass in a parameter into a function with the ‘const’ keyword and then try changing it in our function, our compiler will throw an error.

We can also use the ‘const’ keyword in member function declarations, it tells us that we are simply returning a member variable and just reading a value.

http://prntscr.com/jfy9lf

29
Q

What are the 3 ways to return from a function?

A

Return-by-Value: object is copied from the function to the caller function

Return-by-Reference: returns the address of object

Return-by-Pointer: returns the address of object

30
Q

What is a copy constructor and what are the 2 different types?

A

A copy constructor copies an object based off of an existing object.

Whenever a non-primitive variable is passed or returned by value from a function (means a copy of that object needs to be made), we need a copy constructor for our class.

There are 2 types of copy constructors:

Automatic Copy Constructor: called if no copy constructor is defined, this is done by default and does a shallow copy because it simple copies member variables for objects, and if copying a pointer, will make a shallow copy by making the variable point to an existing address.

Custom Copy Constructor: declared as any other constructor, supposed to make a deep copy of the object, this is part of the “Big 3” we declare when making our classes.

Here are the differences between both types of constructors

http://prntscr.com/jfy9se

31
Q

What is a destructor? What part of the lifecycle of class is this? What are the types of destructors?

A

The destructor is the last and final member function called in the lifecycle of a class. The destructor frees all the resources of the class (all objects, memory, heap)

There are 2 types of destructors:

Automatic Destructor: automatically gets called if the custom destructor is not defined. Remember that if we have member variables on the heap we need to define a custom constructor

Custom Destructor: will be defined by us, frees heap memory explicitly

Remember that in our program that the destructor is never invoked directly, the delete keyword calls it for us.

32
Q

Explain Overloading Operators in C++

A

C++ allows us to overwrite operators, that means we can change the behavior of every operator. There are 20 operators we can overload for our class.

Example: The bitwise operators in cout work as an input stream.

33
Q

What is the syntax to overload operators?

A

http://prntscr.com/jfy9y6

34
Q

What is a very important operator we must overload in our classes?

A

The Assignment Operator! This operator overloader is very similar to the copy constructor but here is one big difference: it needs to clear all the data from the current data structure before it copies over state data from another object to the current object.

2 Main Steps to Assignment Operators:

Clear memory of the current object (*this)
Copy the state of the assigned object

35
Q

What is the rule of 3?

A

If we define ANY of the following functions from the 3 below, that means we need to define ALL of the following:

Copy Constructor
Assignment Operator
Destructor

36
Q

What are the 3 main design principles of Object-Oriented Design?

A

Abstraction: define interfaces with C++ by class (.h)
Encapsulation
Modularity

37
Q

What is Inheritance, and describe it in your own words?

A

Inheritance is the technique that allows you to design generic classes that can be extended into more specialized classes with derived classes by reusing code from the base class they are extending from.

Basically it refers to using the structure and behavior of a super class in a subclass.

http://prntscr.com/jfyadb

38
Q

What is Polymorphism? How does the concept differ from inheritance?

A

Polymorphism is an OOP concept in which we can declare a single object to take on any of its base types.

http://prntscr.com/jfyah6

39
Q

What keyword allows a derived class to override the behavior of a base class?

A

The virtual keyword allows us to override the behavior of a class by its derived type.

40
Q

Describe 2 cases in which we would call methods for inherited classes?

A

Base class has defined a member function (public) however derived classes does not. In this cases, the base class’s function gets called whenever we instantiate the object.

Both methods of the same name in both classes are defined: In this cases whichever object gets instantiate gets its method called.

41
Q

What is Pure Virtual Method? Also what are the roles of an abstract class?

A

A Pure Virtual Method does not have a definition, and makes a class abstract. You cannot create an object of an abstract class.

Every class that is derived from an abstract class MUST implement the pure virtual method from the base.

Also remember that a derived class has to override a pure virtual method but does not have to override a virtual method alone.

42
Q

What is a template and what does it allow us to do? What would a templated class roughly look like in code?

A

A template is a way for us to write a data structure with generic types.

That means we have to add “template “ in front of every function, look a the example code below.

http: //prntscr.com/jfyaqm
http: //prntscr.com/jfyat3

43
Q

Why would a destructor in a base class need to be virtual?

A

To ensure that the destructor for the most derived class is called.