Image goes here
Objects in C++
CptS 355 - Programming Language Design
Washington State University

C++

C++ Goals

  • Data abstraction and OO features
  • Better static type checking
  • Compatibility with C: most C code should be legal C++
  • Efficiency of compiled code

    C++ Problem Areas

    Goal here is not to pick on C++ but to point out areas that may be especially problematic.
    • Casts and Conversions
      • Safer than in C – i.e. there are safe casts
    • Automatic conversions (coercions)
    • Interactions with overloading
    • Objects on stack
    • C++ has both object values and object references. The problem is that assignment necessarily has different semantics depending on which is being used. (The value has to fit in the space available). They look quite similar but so you have to pay careful attention.
    • Care requirement to avoid creating dangling references to stack objects: the lifetime of an object on the stack ends when the activation record containing it goes away (e.g., procedure return or handled exception).
    • Overloading
    • Interaction of dynamic lookup, static lookup, and static lookup with overloading. Interaction with conversions/coercions.
    • Multiple Inheritance -- more next time on the associated problems
    • Implementation details necessarily interact with semantics

    C++ Constructors

    • Purpose: initialize member data. Like all methods, constructors may be overloaded.
    • Method name of constructors is the class name

    Note: constructors in C++ are class methods ~ they are called without naming any particular instance.

    C++ classes, unlike python classes, are not themselves objects.

    Destructors

    Every object has a destructor, either default or programmer-provided. The default destructor for an object calls the destructor of each data member. Destructors are instance methods: if a class is used as a base class its destructors should be made virtual ~ so that dynamic dispatch chooses the right one in all cases.

    Visibility attributes of methods and data

    • public ~ visible in any scope which can create or access an object of this type
    • private ~ visible only in the class itself
    • protected ~ visible in the class and in derived classes
    • friend ~ explicit visibility of private data and methods for some other class or function

    Virtual vs. non-virtual member functions

    Dynamic dispatch (see last time) is only used for virtual member functions. For non-virtual member functions the code corresponding to a name is chosen based on the static type of the variable used in making the call. This may not be the right code!

    Only virtual methods can be overridden in derived classes. But (and this is confusing) non-virtual methods can be overloaded. The behaviors of the two are quite different. By default member functions are non-virtual.

    The overload/override problem in a nutshell:

    • A virtual member function is overridden in a derived class
    • A non-virtual member function is over-loaded in a derived class
    • Overriding implies dynamic selection
    • Overloading implies static selection

    Object implementation - single inheritance case

    My vtable drawing is not available yet. See Fig. 12.1 in the textbook.
    • Note: non-virtual member functions are NOT in the vtable.
    • A pure virtual member function: one that has no implementation in the current class
    • An abstract class: a class with at least one pure virtual member function

    You can’t create an instance of an abstract class: it must be sub-classed and the missing member functions supplied in order to create instances. However you can declare pointers and references having as type an abstract class. Knowing the abstract class is sufficient for the compiler to determine the layout of the vtable which is what it needs in order to compile code using a pointer or reference.

  • (c) 2003 Curtis Dyreson, (c) 2004-2006 Carl H. Hauser           E-mail questions or comments to Prof. Carl Hauser