< Previous | Contents | Next >

Questions and Answers

Q: How many levels of inheritance can you have?

A: Theoretically, as many as you want. But as a beginning programmer, you should keep things simple and try not to go beyond a few levels.

Q: Is friendship inherited? That is, if a function is a friend of a base class, is it automatically a friend of a derived class?

A: No.

Q: Can a class have more than one direct base class?

A: Yes. This is called multiple inheritance. Its powerful, but creates its own set of thorny issues.

Q: Why would you want to call a base class constructor from a derived class constructor?

A: So you can control exactly how the base class constructor is called. For example, you might want to pass specific values to the base class constructor.

Q: Are there any dangers in overriding a base class function?

A: Yes. By overriding a base class member function, you hide all of the overloaded version of the function in the base class. However, you can

Questions and Answers 381


still call a hidden base class member function explicitly by using the base class name and the scope resolution operator.

Q: How can I solve this problem of hiding base class functions?

A: One way is to override all of the overloaded version of the base class function.

Q: Why do you usually want to call the assignment operator member function of the base class from the assignment operator member function of a derived class?

A: So that any base class data members can be properly assigned.

Q: Why do you usually want to call the copy constructor of a base class from the copy constructor of a derived class?

A: So that any base class data members can be properly copied.

Q: Why can you lose access to an objects member functions when you point to it with a base class member?

A: Because non-virtual functions are called based on the pointer type and the object type.

Q: Why not make all member functions virtual, just in case you ever need polymorphic behavior from them?

A: Because theres a performance cost associated with making member functions virtual.

Q: So when should you make member functions virtual? A: Whenever they may be inherited from a base class.

Q: When should you make a destructor virtual?

A: If you have any virtual member functions in a class, you should make the destructor virtual, too. However, some programmers say that to be safe, you should always make a destructor virtual.

Q: Can constructors be virtual?

A: No. This also means that copy constructors cant be declared as virtual either. Q: In OOP, what is slicing?

A: Slicing is cutting off part of an object. Assigning an object of a derived class to a variable of a base class is legal, but you slice the object, losing the data

382 Chapter 10 n Inheritance and Polymorphism: Blackjack


members declared in the derived class and losing access to member functions of the derived class.

Q: What good are abstract classes if you cant instantiate any objects from them?

A: Abstract classes can be very useful. They can contain many common class members that other classes will inherit, which saves you the effort of defining those members over and over again.