previous | index | next

Virtual Methods

Q: What would be the output of the same program:
    Item item = new OxfordShirtInfo(49.99, "Navy blue", 15, 33);
    item->display();
    item = new ChinoInfo(69.99, "Khaki", 36, 34, false);
    item->display();
if the virtual keyword was missing from ItemInfo?

A:
  The item's price is 49.99
  The item's color is Navy blue
  The item's price is 69.99
  The item's color is Khaki
For non-virtual methods, which method will run is determined at compile time by looking at the type of the variable item.

previous | index | next