previous | index | next

Virtual Methods

Here is the revised definition of ItemInfo:

class ItemInfo {
public:
    ItemInfo(double p, string c) { 
        price = p;
        color = c;
    };
    double getPrice() { return price; }
    string getColor() { return color; }
    }
    virtual void display() { // Default behavior for item display
        cout << "The item's price is " << price << endl;
        cout << "The item's color is " << color << endl;
    }
private:
    double price;
    string color;
};

Note that ItemInfo provides default display behavior if a subclass does not override display.


previous | index | next