Exercise Set 4 Answers

  1. In functional programming, you create a type whose members have several different forms in a type definition, listing all of the forms. How do you create a type (or class) with many different forms in object-oriented programming?

    You create an abstract class with a subclass for each kind of thing. You use virtual functions in the abstract class for the functions, implementing them in each subclass as appropriate for each kind of thing.

  2. What is a virtual function?

    A virtual function is a function that logically belongs to a class C, but that can only be defined in subclasses because insufficient information is available in class C.

  3. What is the difference between a concrete class and an abstract class?

    If C is a concrete class then it is possible to create an object that is tagged as having class C. That is not possible when C is a abstract class.

  4. In object-oriented programming, you imagine that objects carry functions with them. Yet, the functions are not really stored with the objects. How does an object locate its functions? How does it know which functions to select? What is the name of the system support that is responsible for locating functions?

    When an object needs a function, it goes to the dispatcher to get the function. The dispatcher is part of the run-time support. It maintains a dispatch table that tells, for each class C and function name f, where the implementation of function f for class C is found.

  5. What is the difference between single inheritance and multiple inheritance?

    With single inheritance, a class can only have one base class. With multiple inheritance, a class can have several base classes. (The base class of class C is the class that C extends directly.)

  6. In a single-inheritance language, is there a limit on the number of base classes that a class can have?

    Yes. It can only have one base class.

  7. How does a single-inheritance object-oriented system know where to look for inherited data fields in an object?

    Data fields are inherited by position. A given variable will always be located at the same place relative to the beginning of the memory that represents the object.

  8. To which classes is a protected data field visible? How does a protected data field differ from a private data field?

    A protected data field is visible in a class and in all of its subclasses. A private data field is only visible within a single class, not in its subclasses.

  9. Is a private data field of an object accessible ONLY to that one object, or is it possible for other objects to access it directly?

    Other objects of the same class can see the private data fields of an object.