Study Guide 12: Polymorphism

  1. The compiler uses a different algorithm to locate member variables and regular member functions than it does to locate polymorphic member functions.
    1. True
    2. False
  2. Virtual functions allow you to
    1. create an array of type pointer-to-base c1as that can hold pointers to derived classes.
    2. create functions that can never be accessed.
    3. group objects of different classes so they can all be accessed by the same function code.
    4. use the same function call to execute member functions of objects from different classes.
  3. A pointer to a base (super) class can point to objects of a derived (sub) class.
    1. True
    2. False
  4. Write a prototype for a virtual function called foo() that returns type void and takes one argument of type int.
  5. Deciding what function a particular function call executes, after a program starts running, is called _________________.
  6. Write the declaration for a pure virtual function named bar() that returns void and takes no arguments.
  7. A pure virtual function is a virtual function that (mark all that apply)
    1. causes its class to be abstract.
    2. returns nothing.
    3. is used in a base (super) class.
    4. takes no arguments.
  8. An abstract class is useful when
    1. no classes should be derived from it.
    2. there are multiple paths from one derived class to another.
    3. no objects should be instantiated from it.
    4. you want to defer the declaration of the class.
  9. Which of the following is not one of the defining features of the object-oriented model?
    1. inheritance
    2. association
    3. encapsulation
    4. polymorphism
  10. Examine the following class definition:
    Public class Insect
    {    public:
              virtual void sting(int howHard) { ... }
              virtual int  sting(double howHard) { ... }
    };
    
    1. Function sting is
    2. a correctly overridden function
    3. a correctly overloaded function
    4. an erroneously overridden function
    5. an erroneously overloaded function
  11. Examine the following class definitions:
    class Insect
    {    public:
              void sting(int howHard) { ... }
    }
    
    class Spider : public Insect
    {    public:
              int  sting(int howHard) { ... }
    };
    
    Function sting is
    1. a correctly overridden function
    2. a correctly overloaded function
    3. an erroneously overridden function
    4. an erroneously overloaded function
  12. Polymorphism in C++ requires ALL of the following:
    1. True
    2. False
  13. The function calls in main invoke which functions?
    class Parent
    {
         public:
                             void      funcA();
                   virtual   void      funcB();
                             void      funcC();
    };
    
    class Child : public Parent
    {
         public:
                             void      funcA();
                   virtual   void      funcB();
    };
    
    int  main()
    {
         Parent*   P1 = new Parent;
         Parent*   P2 = new Child;
         Child*    C = new Child;
    
         P1->cA();
         P1->cB();
         P1->cC();
    
         return 0;
    }
    
    1. Parent::funcA; Parent::funcB; Parent::funcC
    2. Child::funcA; Child::funcB; Parent::funcC
    3. Parent::funcA; Child::funcB; Parent::funcC
    4. Parent::funcA; Child::funcB; Child::funcC
    5. Child::funcA; Child::funcB; Child::funcC
  14. The function calls in main invoke which functions?
    class Parent
    {
         public:
                             void      funcA();
                   virtual   void      funcB();
                             void      funcC();
    };
    
    class Child : public Parent
    {
         public:
                             void      funcA();
                   virtual   void      funcB();
    };
    
    int  main()
    {
         Parent*   P1 = new Parent;
         Parent*   P2 = new Child;
         Child*    C = new Child;
    
         P2->cA();
         P2->cB();
         P2->cC();
    
         return 0;
    }
    
    1. Parent::funcA; Parent::funcB; Parent::funcC
    2. Child::funcA; Child::funcB; Parent::funcC
    3. Parent::funcA; Child::funcB; Parent::funcC
    4. Parent::funcA; Child::funcB; Child::funcC
    5. Child::funcA; Child::funcB; Child::funcC
  15. The function calls in main invoke which functions?
    class Parent
    {
         public:
                             void      funcA();
                   virtual   void      funcB();
                             void      funcC();
    };
    
    class Child : public Parent
    {
         public:
                             void      funcA();
                   virtual   void      funcB();
    };
    
    int  main()
    {
         Parent*   P1 = new Parent;
         Parent*   P2 = new Child;
         Child*    C = new Child;
    
         C->funcA();
         C->funcB();
         C->funcC();
    
         return 0;
    }
    
    1. Parent::funcA; Parent::funcB; Parent::funcC
    2. Child::funcA; Child::funcB; Parent::funcC
    3. Parent::funcA; Child::funcB; Parent::funcC
    4. Parent::funcA; Child::funcB; Child::funcC
    5. Child::funcA; Child::funcB; Child::funcC
  16. The function calls in main invoke which functions?
    class Parent
    {
         public:
                             void      funcA();
                   virtual   void      funcB();
                             void      funcC();
    };
    
    class Child : public Parent
    {
         public:
                             void      funcA();
                   virtual   void      funcB();
    };
    
    int  main()
    {
         Child     C;
         Parent    P = C;
    
         P.funcA();
         P.funcB();
         P.funcC();
    
         return 0;
    }
    
    1. Parent::funcA; Parent::funcB; Parent::funcC
    2. Child::funcA; Child::funcB; Parent::funcC
    3. Parent::funcA; Child::funcB; Parent::funcC
    4. Parent::funcA; Child::funcB; Child::funcC
    5. Child::funcA; Child::funcB; Child::funcC
  17. What gets printed out by the code listed below the classes (i.e., below the line)?
    class Animal
    {
        public:
            virtual void whoAreYou() { cout << "an Animal\n";}
    };
    
    class Insect : public Animal
    {
        public:
            void whoAreYou() { cout << "an Insect\n";}
    };
    
    class Spider : public Insect
    {
        public:
            void whoAreYou() { cout << "a Spider\n";}
    };
    
    --------------------------------------------------
    Animal* a = new Insect;
    Animal* i = new Spider;
    Insect* s = new Spider;
    
    a->whoAreYou();
    i->whoAreYou();
    s->whoAreYou();
    
    1. an Insect, a Spider, a Spider
    2. an Animal, an Animal, an Insect
    3. an Animal, an Insect, an Animal
    4. an Insect, an Insect, a Spider
    5. an Animal, an Animal, an Animal
  18. Fill in the blanks to complete
    1. The Employeeclass initializer list
    2. The SpecialEmployee class initializer list
    3. The SpecialEmployee class cal_pay function. A SpecialEmployee's is their salary plus a bonus.

    The highlighted labels are not part of the program.

    class Employee
    {
        private:
            double salary;
        public
            Employee(double s) : (a) ____________ {};
            virtual double calc_pay() { return salary / 24; }
    };
    
    
    class SpecialEmployee : public Employee
    {
        private:
            double bonus;
    
        public:
            SpecialEmployee(double b, double s) : (b) _____________________ {};
            double calc_pay() { (c) ______________________________; }
    };