C++ Flashcards

1
Q

What are the 5 properties of OOP languages such as C++? Define each…

A

Abstraction -> Group associated features into on place for use.
Polymorphism -> Objects of the same type can take different forms.
Encapsulation -> All implementations are encapsulated for security, accessibility,
Inheritance -> Allows for diverse implementations of a single interface and documentation.
Extensibility -> Enables classes to extend functionality of other classes.

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

What header is needed for basic IO commands? What are the commands used for character input and output?

A

include <iostream></iostream>

std::cout -> Writes character stream to console.
std::cin -> Reads character stream from keyboard.

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

What do constructors and destructors return?

A

Nothing.

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

Explain when and how to use the ‘this’ keyword…

A

this is a pointer to the immediate scope of the class. For example, if used in a constructor, it will point to the variable outside of the constructor scope.

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

How do we differentiate the public, protected and private members of a class?

A

Public:
Protected:
Private:

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

In regards to dynamic memory allocation to pointers in the class, what happens upon instantiation of an object?

A

The required memory is automatically allocated to the pointers.

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

In regards to memory allocation, what happens when the destructor is called?

A

Allocated memory is freed.

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

How do we declare the destructor?

A

~MyClass();

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

When is the destructor called?

A

When delete(Object) is called on the object.

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

What is the purpose of a Copy Constructor?

A

It’s a way to assign members of an object instances based on another instance of the same object.

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

How do we declare a Copy Constructor?

A

MyClass ( const MyClass & )

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

How can we access functions outside the scope of their declaration? Use a class name Person and function named getAge() as an example…

A

Person::getAge();

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

Is function overloading possible?
How do we set a default argument?

A

Yes.
setAge( int x = 28 ) { }

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

Which keyword dynamically assigns memory to a pointer type? Which keyword frees this memory?

A

new
delete

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

What’s the difference between new and malloc?

A

New allocates memory and calls the constructor, whereas malloc only allocates memory.
New also calculates the memory for you, rather than having to explicitly declare it.

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

What’s the difference between delete and free?

A

Delete de-allocates memory and calls the destructor, whereas free only de-allocated memory.

17
Q

How do we return a reference from a function?

A

char &setValue() -> returns a reference to a character.

18
Q

How do we declare a class that extends another class?

A

class Derived: public Base { };

19
Q

What are the 3 access specifiers? And what access do derived classes have to base variables declared with each?

A

Public -> Derived classes can access directly.
Protected -> Derived classes can only access via a public function that returns the Protected variable. However, function can be located in the derived class.
Private -> Derived classes can only access via a public function from the base class, in the same class as the private variable.

20
Q

Is it possible to conduct multiple inheritance?

A

Yes. Through chaining the : operator.

21
Q

Which access specifier should you try to use?

A

Public where possible.

22
Q

What does using the Private specifier protect the base class from?

A

Being affected by changes to the derived class.

23
Q

When the main function is returned, what happens regarding the destructors of instantiated objects ?

A

The return statement calls all the destructor of all objects in the main function.