midterm2 Flashcards
- What is the syntax for RTTI using a pointer?
4. What is the syntax for RTTI using a constant reference (an object))?
- Derived * ptr = dynamic_cast (a_base_class_pointer);
if (ptr) //if this is not nullptr, allocate memory for this Derived Data_member = new Derived(*ptr);
4.
const Derived * ptr = dynamic_cast (& const_derived_obj); //or
const Derived * ptr = dynamic_cast (& const_base_reference);
//then
if (ptr) //if this is not nullptr, allocate memory for this Derived
Data_member = new Derived(*ptr);
- What is RTTI?
2. What do we use to implement RTTI?
- Run Time Type Identification - we need RTTI to determine what kind of data to allocate.
- The dynamic_cast operation allows us to determine what kind of data we are pointing at (within a particular path of hierarchy)
- What is RTTI?
2. What do we use to implement RTTI?
- Run Time Type Identification - we need RTTI to determine what kind of data to allocate.
- The dynamic_cast operation allows us to determine what kind of data we are pointing at (within a particular path of hierarchy)
What is a pure virtual function?
What is the syntax for a pure virtual function?
A pure virtual function is one where there is no body for the member function. This is useful in situations where the function is needed in the common base class to allow for dynamic binding but where the function has no real purpose in that class. Pure virtual functions also document which functions MUST be implemented in the derived classes!
syntax:
void func() = 0;
What is an abstract base class?
When is one created?
With an abstract base class, no explicit objects of that class may be created.
An abstract base class is created when you have at least one pure virtual function.
They can still have data members and member functions; they exist when there is at least one pure virtual function. C++ does not use the keyword abstract.
- What must be present for Dynamic Binding?
2. Does dynamic binding apply to constructors?
1. a. Same function names, same arguments, same return types throughout the hierarchy b. The base class has the keyword virtual in front of the prototype for the function(s) c. The function is called via a base class pointer or reference, but assigned to point to the desired derived class object
- No, dynamic binding does not apply to constructors