Home
51.
Why is the parameter of a copy constructor one that takes the copy source by reference?

For one, the copy constructor is expected by the compiler to be that way. The reason behind it is that a copy constructor would invoke itself if it accepted the copy source by value, resulting in an endless copy loop.

52.
I have been asked to model class Mammal along with a few mammals such as the Human, Lion, and Whale. Should I use an inheritance hierarchy, and if so which one?

As Human, Lion, and Whale are all mammals and essentially fulfill an is-a relationship, you should use public inheritance where class Mammal is the base class, and others such as class Human, Lion, and Whale inherit from it.

53.
What is the difference between the terms derived class and subclass?

Essentially none. These are both used to imply a class that derives-that is,specializes-a base class.

54.
A derived class uses public inheritance in relating to its base class. Can it access the base class's private members?

No. The compiler always ensures that the most restrictive of the applicable access specifiers is in force. Irrespective of the nature of inheritance, private members of a class are never accessible outside the class. An exception to this rule applies to classes and functions that have been declared as a friend.

55.
Why use the virtual keyword with a base class function when code compiles without it?

Without the virtual keyword, you are not able to ensure that someone calling objBase.Function() will be redirected to Derived::Function(). Besides, compilation of code is not the only measure of its quality.

56.
Why did the compiler create the Virtual Function Table?

To store function pointers that ensure that the right version of a virtual function is invoked.

57.
Should a base class always have a virtual destructor?

Ideally yes. Only then can you ensure that when someone does a
Base* pBase = new Derived();
delete pBase;
delete on a pointer of type Base* results in the destructor ~Derived() being invoked. This occurs when destructor ~Base() is declared virtual.

58.
What is an Abstract Base Class good for when I can't even instantiate it standalone?

The ABC is not meant to be instantiated as a standalone object; rather it is always meant to be derived from. It contains pure virtual functions that define the minimal blueprint of functions that deriving classes need to implement, thus taking the role of an interface.

59.
Given an inheritance hierarchy, do I need to use the keyword virtual on all declarations of a virtual function or just in the base class?

It is enough to declare a function as virtual once, but that declaration has to be in the base class.

60.
Can I define member functions and have member attributes in an ABC (Abstract Base Class)?

Sure you can. Remember that you still cannot instantiate an ABC (Abstract Base Class) as it has at least one pure virtual function that needs to be implemented by a deriving class.