Sams Teach Yourself C++ in 21 Days

(singke) #1

488 Day 14


Q When should the destructor be made virtual?
A The destructor should be made virtual any time you think the class will be sub-
classed, and a pointer to the base class will be used to access an object of the sub-
class. As a general rule of thumb, if you’ve made any functions in your class
virtual, be certain to make the destructor virtual as well.
Q Why bother making an abstract class—why not just make it nonabstract and
avoid creating any objects of that type?
A The purpose of many of the conventions in C++ is to enlist the compiler in finding
bugs, so as to avoid runtime bugs in code that you give your customers. Making a
class abstract—that is, giving it pure virtual functions—causes the compiler to flag
any objects created of that abstract type as errors.

Workshop ............................................................................................................


The Workshop provides quiz questions to help you solidify your understanding of the
material covered and exercises to provide you with experience in using what you’ve
learned. Try to answer the quiz and exercise questions before checking the answers in
Appendix D, and be certain you understand the answers before continuing to tomorrow’s
lesson.

Quiz ................................................................................................................



  1. What is a down cast?

  2. What does “percolating functionality upward” mean?

  3. If a round-rectangle has straight edges and rounded corners, and your RoundRect
    class inherits both from Rectangleand from Circle, and they in turn both inherit
    from Shape, how many Shapes are created when you create a RoundRect?

  4. If Horseand Birdinherit from Animalusing public virtual inheritance, do their
    constructors initialize the Animalconstructor? If Pegasusinherits from both Horse
    and Bird, how does it initialize Animal’s constructor?

  5. Declare a class called Vehicleand make it an abstract class.

  6. If a base class is an abstract class, and it has three pure virtual functions, how
    many of these must be overridden in its derived classes?

Free download pdf