Polymorphism & Inheritance (9) Flashcards

1
Q

What is inheritance?

A

Occurs when a derived class inherits from one or more base classes

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

What does a derived class do?

A

Accesses base class behaviors
Defines new attributes and behaviors
Overrides behaviors
More specific version

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

What does the constructor of a derived class look like (code)?

A

DerivedClass::DerivedClass (param):BaseClass(param){}

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

What order are the constructors used for a derived class?

A

Base class constructor instantiates an instance then derived class constructor is executed

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

What is an abstract class?

A
Base class that will never have an object instantiated from it
Only used for inheritance (too general)
Any class that has at least one pure virtual function (const = 0)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a virtual function?

A
Function defined as virtual is virtual in the base class and all classes derived
Class should also contain virtual destructor
Permit dynamic binding: when a program uses the function defined for the object type not the function defined for the reference or pointer type
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is polymorphism?

A
When multiple objects from different classes are related by inheritance form the same base class
Having many forms/ functionality but shared base class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a virtual function table?

A

When encountering a virtual function, compiler creates a virtual function table –> keeps track of which function implementation is to be executed when the function is called
Object is instantiated –> compiler attaches a pointer to the table
There are memory and processing costs because of memory needed to hold the address

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

What are some design considerations for inheritance?

A

Wide or deep hierarchies can be bad design
Prepare base classes to be base classes (no accidents)
Upcasting but no down-casting (still good code reuse)

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

What is RTTI?

A
RunTime Type Identification
Method for programs to determine type of an object
Can only be used with class hierarchy that has virtual functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the three components of RTTI

A

dynamic_cast : generates a pointer to a derived type from pointer to a base type
typeid returns value identifying the exact type of an object
type_info holds information about a particular type

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

What does the dynamic_cast operator do?

A

Determines if the address of an object to a pointer can be safely assigned to a pointer of a particular type

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