Inheritence Flashcards
What is the syntax to declare a derived class?
class derived_name : access_specifier base_name
What are the most important rules of constructor pre-initialization with base classes?
- The call to the base class must be called before any other constructor
- cannot be called in the class or it just creates and instance inside the class
What are the three modes of inheritence?
- public
- protected
- private
What is the default mode of inheritence?
private
What happens to the members of a public base class?
The public base-members remain public, and the protected base-members remain protected in the derived class.
What happens to the members of a protected base class?
Both public and protected base-members become protected in the derived class.
What happens to member types in a privately inherited base class?
Both public and protected base-members become private in the derived class.
What base class members do derived classes have access to?
Only public and protected, private is inherited but not directly accessible.
How can a derived class access private members of a base class?
Through getters/setters, or if the base class friends the derived class.
What is the essence of polymorphism?
The ability to store a derived class in a bass class and use all the methods of the base class.
How is runtime polymorphism achieved in C++?
With base class pointers and derived class references. ex:
derived d();
base * b = &d;
How do you tell a polymorphic object to use the derived method rather than the base method?
Virtual Methods
What is the syntax to declare a virtual method?
virtual return_type method_name(inputs) {}
Where are virtual methods declared?
In the base class
Given class base with method virtual string print, and class derived with method string print, and object derived d (); base *btod = &d;, what method will btod->print() call?
The derived print method.