14 - Inheritance Flashcards
Define the process of inheritance in C++.
Inheritance is the process by which a new class - known as a derived class - is created from another class, called the base class. We often use parent class and child class as terminology.
A class that is derived from another class will automatically inherit ..
all the member variables and member functions of the base class. We often use parent class and child class as terminology.
Show off the syntax for creating the derived class “HourlyEmployee” from the base class “Employee”.
class HourlyEmployee : public Employee
What is and how do you redefine an inherited member function?
The definition of an inherited member function can be changed in the definition of a derived class that is different from what it is in the base class. This is called redefining the inherited member function.
What is not inherited from the base class?
The constructor (and destructor?). But a constructor for the inherited class begins with an invocation of a constructor for the base class.
Private variables in the base class are not available to the derived class. In what way can you access them?
- Indirectly through public member functions.
- Make it public.
- Make it protected.
What does the protected qualifier do in classes?
If you use the qualifier protected, rather than private or public, before a member variable or member function of a class, then for any class of function other than a derived class the effect is the same as if the member variable were labeled private; however, in a derived class the variable can be accessed by name.
What is the difference with redefining and overloading?
When you redefine a function definition, the new function definition given in the derived class has the same number and types of parameters. When you overload a function, the function in the derived class has a different number of parameters or a parameter of a different type from the function in the base class, and the derived class has both functions.
How can you use overloaded base class operators for the derived class?
They’re not inherited, so you must define them in the derived class - but the code in the function body can be made much simpler by calling the overloaded operator of the base class.
Overloaded operators from the base class are not inherited, so you must define them in the derived class - but the code in the function body can be made much simpler by calling the overloaded operator of the base class. What is required for this to work?
You need to have a correctly functioning assignment operator (=) and a correctly functioning copy constructor () for the base class.
How do you make a destructor in a derived class?
When the destructor for the derived class is invoked, it automatically invokes the destructor of the base class, so there is no need for the explicit writing of a call to the base class destructor: it happens automatically. The derived class destructor thus need only worry about using delete on the member variables. that are added in the derived class.
What should you always be careful of when overloading an assignment operator?
Whenever you overload an assignment operator, always make sure your definition works when the same object occurs on both sides of the assignment operator. In most cases, you will need to make this a special case with some code of its own.
What is a good rule of thumb for deciding whether two concepts should have a family relationship (parent class, child class) or one be the member variable of the other?
The best programming technique is to simply follow what sounds most natural in English. Take the example with 1) A simulation of a jet engine, and 2) a simulation of a passenger airplane. It makes more sense to say “A passenger airplane has a jet engine” than it does to say “A passenger airplane is a jet engine”. So it makes better programing sense to have JetEngine as a member variable of a PassengerAirPlane class. It makes little sense to make the PassengerAirPlane class a derived class of the JetEngine.
What is protected and private inheritance?
Normal inheritance uses the keyword public in the class heading. The use of protected and private inheritance gives different types of inheritance that are seldom used.
What is multiple inheritance?
It is possible for a derived class to have more than one base class. This creates a lot of ambiguous situations, and you should never need to do this.