- Forward


Classes and Objects in C++: Some Advanced Topics
for Java Programmers


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Polymorphism
Back SMYC Forward
  • Etymology:
    • From the Greek - "having many forms"
  • In this course:
    • At run-time, a variable may "take the form of" any of its ancestors.
  • Related Concepts:
    • Dynamic binding
Polymorphism (cont.)
Back SMYC Forward
  • Search Order in Java:
    • If the "message" is sent to an object of the derived class then the derived class is searched (for the existence of such a method) first and the base class is searched second. (Note: The search will move up the class hierarchy as needed.)
    • If the "message" is sent to an object of the base class then only the base class is searched.
  • Search Order in C++:
    • Depends on whether the method is declared virtual
Virtual Methods
Back SMYC Forward
  • Binding (for Dynamically Allocated Objects):
    • Non-virtual methods are resolved at compile time (called static binding)
    • Virtual methods are resolved at run-time (called dynamic binding)
  • Suppose a message is sent to an object...
    • declared to be of the base class and actually of the base class:
      • The method in the base class is always used.
    • declared to be of the derived class and actually of the derived class:
      • First the derived class is searched for the method and then the base class is searched
    • declared to be of the base class and actually of the derived class:
      • If the method is non-virtual only the base class is searched
      • If the method is virtual the derived class is searched first and then the base class is searched
An Example
Back SMYC Forward

The Specification of Account

cppexamples/oopbasics/banking/Account.h
 
An Example (cont.)
Back SMYC Forward

The Implementation of Account

cppexamples/oopbasics/banking/Account.cpp
 
An Example (cont.)
Back SMYC Forward

The Specification of an Overdraft Account

cppexamples/oopbasics/banking/OverdraftAccount.h
 
An Example (cont.)
Back SMYC Forward

The Implementation of an Overdraft Account

cppexamples/oopbasics/banking/OverdraftAccount.cpp
 
An Example (cont.)
Back SMYC Forward

A Driver

First, build and execute the application with the amountAvailable() method declared virtual. Then, build and execute the application without the amountAvailable() method declared virtual. Which behaves like Java?

cppexamples/oopbasics/banking/Driver.cpp
 
Pure Virtual Methods
Back SMYC Forward
  • Defined:
    • A virtual method that is assigned the value 0 when declared/specified
  • An Example:
    • virtual char getSymbol() = 0;
  • Relationship to Java:
    • Essentially the same as abstract methods
Pure Virtual Methods (cont.)
Back SMYC Forward
  • Can be used to create the equivalent of abstract classes in Java (if any of the methods are pure virtual methods)
  • Can be used to create the equivalent of interfaces in Java (if all of the methods are pure virtual methods)
Behavior of Destructors
Back SMYC Forward
  1. The destructor in the derived class is executed
  2. The base class destructor is executed
  3. The memory allocated to the object is returned to the free store
Run-Time Type Identification (RTTI)
Back SMYC Forward
  • Discerning the Type:
    • The typeid() function in <typeinfo> allows one to determine the actual type of an object (it is passed a pointer and returns a type_info object)
  • Casting:
    • The static_cast and dynamic_cast operators can be used to convert types (without and with run-time verification respectively)
  • Advice:
    • Use RTTI sparingly - it's use usually indicates a bad design
There's Always More to Learn
Back -