Computer Science 3675
Fall 2002
Solutions to practice questions for quiz 4

  1. Write a clearly legible T to the left of each of the following that is true, and a clearly legible F to the left of each that is false.

    1. Programs written in an object-oriented style tend to be organized significantly differently from programs written in a functional or procedural style. T Each module (class) handles all characteristics of a particular type of data rather than handling similar operations on many different types of data.

    2. The class is an important concept of object-based programming. F Classes are part of object-oriented computing.

    3. In most object-oriented languages, some type checking must be done at run time. T Dynamic type checking is required in order to do a downcast operation, in which the programmer tells the compiler to move the type of a variable downwards in the hierarchy.

  2. In object-oriented programming, is a private variable of an object accessible only to that one object, or is it possible for other objects to access it directly?

    In object-based programming, a private data field (variable) is accessible only to the object that contains it. In object-oriented programming, it is accessible to that object and to all other objects of the same class. Since the questions asks about object-oriented programming, a private variable in an object can sometimes be used directly by other objects.

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

    Yes. In a single-inheritance language, each class can have at most one base 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?

    An object carries a tag indicating its class. Functions are stored according to their class in a dispatch table, and are looked up in that table when they are needed. The part of the run-time support that locates functions in the dispatch table is called the dispatcher.

  5. Some object-oriented languages allow selection of methods to be done either statically or dynamically, depending on how the method is defined. What is the difference between static and dynamic selection?

    When a method is selected statically, it is selected according to the class of the variable that holds the object. For example, if variable v has type ImaginaryCreature, and operation v.jump() is requested, the jump method is selected using class ImaginaryCreature. Dynamic selection requires selecting a method according to the class of the object that the variable contains. For example, if variable v has type ImaginaryCreature, but it actually contains an object of class Hobbit (where Hobbit is a subclass of ImaginaryCreature) then performing v.jump() selects the jump method from the dispatch table in the row corresponding to Hobbit, not in the row of ImaginaryCreature. If the method has been overridden, it can make a difference.

  6. How does the mechanism for inheriting variables work in single-inheritance object-oriented languages? Is there a separate implementation of each selector for each class, or does one implementation of each selector work for all classes? How do the selector(s) work?

    Variables are found by their position in the block of memory that represents an object's state. They are found at the same position regardless of the class of the object. So a single implementation works for all classes. Selectors do not need to be stored in the dispatch table, and they work very efficiently.

  7. What are the characteristics of a virtual function? What makes it virtual?

    A virtual function logically belongs to a class that is higher in the class hierarchy than the level at which the function can be implemented. It is declared to belong to the higher level, but implemented at the lower levels.

    C++ is an unusual language in this respect. In C++, a virtual function is a function whose method is selected dynamically. A function that is not marked virtual is selected statically. You can declare a C++ function to be a pure virtual function, indicating that you are not providing a definition of it, and are creating an abstract class. Pure virtual functions in C++ correspond to what are simply called virtual functions in most object-oriented languages.

  8. What is an abstract class?

    An abstract class is a class with at least one virtual function. You are not allowed to create an instance of an abstract class unless that instance is actually an instance of a subclass of the abstract class. That is because you would not know what to put for the virtual function.