Slicing
- Happens when copying derived class to base class type
- Some data members will be lost
- Does not apply to pointers/references
Name Conflicts
- Choose the nearest member/method
- Otherwise use
<Base_Class>::<member|method>
Virtual Functions
- Overloading
- same name, different arguments
- overloading resolution is done when the program is compiled
- no dynamic binding
- Overriding
- allow derived class to have different implementation than the base class
- the decision is made when the function is called
- only applies to member methods
- virtual function
- access control is decided during compilation, has nothing to do with dynamic binding
must be const override
Abstract Base Class (ABC)
- Pure virtual function
virtual <type> <function_name>() = 0
- Cannot create object from an ABC
- Can create pointer/reference of ABC (since it can point to derived class)
- If we do not implement the missing function, the derived class is still a ABC
Dynamic Binding
use pointer or reference to call a function
RTTI
typeid(expression), expression must be an object, not pointer
typeid() has the type type_info which we can use == operator
or can use strcmp(typeid(), typeid()) == 0
the base class must be polymorphic (at least 1 virtual function)
the expression should be an object (need to dereference if it’s a pointer)
another way to check is dynamic_cast<Student*>(uperson[i]) != nullptr, which uses typeid to try to cast the expression to the desired type, return nullptr if it fails, if it is a reference and it fails, it’s a runtime error
Final
class A final {};
virtual void print() const override final or
virtual void print() const final override