previous | index | next

Method Prototypes

Our examples have shown method definitions embedded within the class definition, as in the ItemInfo class.

In practice, however, only instance variables and method prototypes are included in the class definition.

A method prototype shows the method name, return type, and parameter list without the body.

Below is the resulting class definition for ItemInfo. Compare it to the original version shown at right.

class ItemInfo {
public:
  ItemInfo(double p, string c);
  double getPrice();
  string getColor();
  virtual void display();
private:
  double price;
  string color;
};
     
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;
};


previous | index | next