MC logo
Modular and Class Abstraction
[^] Chapter Outlines
[Ch. 1: Overview and History] [Syntax] [Names and Scope] [Types and Type Systems] [Semantics] [Functions] [Memory Management] [Imperitive Programs and Functional Abstraction] [Modular and Class Abstraction] [Functional Programming] [Logic Programming]
[Translating A Class] [Translating Inheritance] [Dynamic Binding]
<<Imperitive Programs and Functional Abstraction Functional Programming>>
  1. Abstract Data Types
    1. Combine data definition and operations.
    2. Plain C integer stack: header implementation simple driver
    3. Parts relate mostly in the programmer's head.
  2. Encapsulation
    1. Allows logically related constants, types, variables, methods, etc. to be grouped.
    2. Examples include older constructs: functions, procedures, and structs.
    3. Became clear that data types need to be encapsulated: modules, packages, and classes.
  3. Packages.
    1. Collect data and operations into a named scope.
    2. Ada integer stack package: package body simple driver.
  4. Generic Packages.
    1. Allows types and other parameters to a package.
    2. Ada generic stack (seen before under types): package body simple driver.
  5. Classes.
    1. Packages, plus:
      1. Class names are types.
      2. Classes can be instantiated.
      3. Syntax for calling methods of an instance.
        x.f(a,b,c) becomes roughly class::f(x,a,b,c), where the first arg is just the this pointer.
    2. Translating a class
  6. Derived classes.
    1. New class extends an existing class.
    2. Implementation
      1. Just create new dynamic struct with added fields.
      2. A pointer to the derived class is a pointer to the base class part.
      3. Translating
  7. Inheritance polymorphism.
    1. An derived-class object retains its behavior even when referred to by a base-class reference.
    2. Dynamic binding: The code which a function call executes is determined at run time, by the type of object.
    3. Translating.
    4. In Java, all methods are polymorphic; in C++, only those declared virtual.
      1. Using the vtable costs extra space and execution time.
      2. Not knowing at compile time which method will be called prevents optimization based on in-lining the body. Since classes promote the use of small methods, this can be significant.
      3. The C++ design goal emphasizes speed. Its design allows the the programmer to decide if the overhead is needed.
  8. Run-time type identification
    1. Can tell the type of an object at run time.
      In a statically-typed language, this is the dynamic type. There is also a static type.
    2. An object function returns the class. Java: obj.getClass()
      1. Usually a string in interpreted languages.
        In Ruby, get a class object.
      2. Usually a numeric code in compiled languages.
    3. Some syntax to ask if two object have the same type, or share a base class.
    4. C++ RTTI is late feature and only works when the class has virtual functions.
  9. Reflection: Can find out all methods, data, etc about a class at run-time.
  10. Template classes
    1. Allows types and other parameters to a package.
    2. C++ stack template: header, test driver.
  11. Multiple inheritance.
    1. What you might want it for
      1. Create a GUI object which displays the contents of a queue: It is a Queue and it is a Window.
      2. Personnel database: Person is a SalariedEmployee; Person is a Accountant.
    2. Support
      1. Most languages do not support MH.
      2. C++ does.
      3. Approximations
        1. Java interfaces.
        2. Ruby mix-ins.
    3. Problems with MI
      1. Same function is defined differently in multiple base classes.
      2. Some class becomes and ancestor multiple times.
  12. Flavors
    1. In a pure object-oriented language, everything is an object.
      1. The original: Smalltalk.
      2. Ruby, pretty much
        1. In Ruby, if is an actual statement.
        2. In Smalltalk, iftrue: is a message you send to a boolean.
      3. Ruby uses syntactic tricks to make some object thinks look otherwise.
    2. Java is not pure object-oriented
      1. int, double, etc.
      2. Classes are not objects.
    3. C++, Python, and many others are mixed-paradigm, since they have top-level functions and such.
    4. Static typing an polymorphism don't mix well.
<<Imperitive Programs and Functional Abstraction Functional Programming>>