previous | index | next

Writing Class Constructors

For most classes, you will write your own constructors that accept arguments (unlike default constructors).

These arguments will often be used to initialize values of the object's instance variables.

class ItemInfo {
public:
    ItemInfo(double p, string c) { 
        price = p;
        color = c;
    }
    double getPrice() { return price; }
    string getColor() { return color; }
private:
    double price;
    string color;
};
Note that constructors do not have return types.


previous | index | next