14 - Inheritance Flashcards

1
Q

Define the process of inheritance in C++.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A class that is derived from another class will automatically inherit ..

A

all the member variables and member functions of the base class. We often use parent class and child class as terminology.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Show off the syntax for creating the derived class “HourlyEmployee” from the base class “Employee”.

A

class HourlyEmployee : public Employee

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is and how do you redefine an inherited member function?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is not inherited from the base class?

A

The constructor (and destructor?). But a constructor for the inherited class begins with an invocation of a constructor for the base class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Private variables in the base class are not available to the derived class. In what way can you access them?

A
  1. Indirectly through public member functions.
  2. Make it public.
  3. Make it protected.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the protected qualifier do in classes?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference with redefining and overloading?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you use overloaded base class operators for the derived class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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?

A

You need to have a correctly functioning assignment operator (=) and a correctly functioning copy constructor () for the base class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you make a destructor in a derived class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What should you always be careful of when overloading an assignment operator?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is protected and private inheritance?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is multiple inheritance?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly