Polymorphism: Study Guide 12 Answers

  1. a
  2. d
  3. a
  4. virtual void foo(int);
  5. polymorphism, late binding, run-time binding, dynamic binding, or dynamic dispatch
  6. virtual void bar()=0;
  7. a and c
  8. c
  9. b. The three "crown jewels" characterizing the object-oriented paradigm are encapsulation, inheritance, and polymorphism.
  10. b. Overloading is a within-class concept. The argument lists must be unique but the return types may be different. (Early in the development of C++, the return types also had to be the same but this requirement was dropped long enough ago that you should no longer see it mentioned in texts.)
  11. c. Overriding is a between-class concept. The classes must be related through inheritance (also includes classes with a common ancestor), each has a function with the same name, the argument lists must be the same, and the return types must also be the same.
  12. a. These concepts are tested in the following questions.
  13. a. The pointer variable (P1) and the object are both type Parent: all functions called are from the Parent class.
  14. c. The pointer variable (P2) is of type Parent but the object is an instance of Child. funcB is virtual and its call is determined by the object: Child. funcA and funcC are not virtual and their calls are determined by the pointer variable: Parent.
  15. b. The pointer variable (C) and the object are of type Child. funcA and funcB are from the Child class; however, Child does not override funcC, which is inherited from Parent.
  16. a. The object assignment (P = C) is valid. It results in "object slicing." The part of the Child object that is not a Parent is sliced off and discarded. Polymorphism is only possible through address variables (either a pointer or a reference).
  17. a. The object on the right side of the assignment operator determines which function is called.
  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) salary(s) {};
            virtual double calc_pay() { return salary / 24; }
    };
    
    class SpecialEmployee : public Employee
    {
        private:
            double bonus;
    
        public:
            SpecialEmployee(double b, double s) : (b) Employee(s), bonus(b) {};	// Employee(s) must be first in the list
            double calc_pay() { (c) Employee::calc_pay + bonus; }
    };