Polymorphism & Inheritance (9) Flashcards
What is inheritance?
Occurs when a derived class inherits from one or more base classes
What does a derived class do?
Accesses base class behaviors
Defines new attributes and behaviors
Overrides behaviors
More specific version
What does the constructor of a derived class look like (code)?
DerivedClass::DerivedClass (param):BaseClass(param){}
What order are the constructors used for a derived class?
Base class constructor instantiates an instance then derived class constructor is executed
What is an abstract class?
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)
What is a virtual function?
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
What is polymorphism?
When multiple objects from different classes are related by inheritance form the same base class Having many forms/ functionality but shared base class
What is a virtual function table?
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
What are some design considerations for inheritance?
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)
What is RTTI?
RunTime Type Identification Method for programs to determine type of an object Can only be used with class hierarchy that has virtual functions
What are the three components of RTTI
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
What does the dynamic_cast operator do?
Determines if the address of an object to a pointer can be safely assigned to a pointer of a particular type