User Defined Types

Enumerated Types
Sometimes, it is convenient to refer to values by symbolic names. An enumerated type is a special case of an integer type, but variables of this type may only take a small number of predefined values. The enum type definition gives symbolic names to these values.
enum STATUS {FAILED, OK}; // FAILED has value, 0, OK is 1
enum SUIT {SPADE, HEART, DIAMOND, CLUB} // SPADE has value 0, etc.

Whenever a value is omitted in the enum constant names list, it is set by default, to the previous value plus one. The first value, if omitted, is set to 0.
enum COIN { penny = 1, nickel, dime, quarter, half_dollar}; // nickel has value 2, etc

Structures
Structures, once defined, become a new data type in the program and can be used pretty much the same way as the built-in types.

Examples:
struct INVENTORYITEM
{
	int partNum;
	apstring description
	int quantity;
	double price;
};

struct DATE
{
	int month, day, year;
};

struct CARD
{
	enum SUIT {SPADE, HEART, DIAMOND, CLUB};
	int rank;
};

Declaration
DATE firstDay;

Initialization

	const DATE firstDay { 1, 1, 1999 };
or
	firstDay.month = 1;
	firstDay.day = 1;
	firstDay.year = 1999;


Continue to:  Unit 3  / Prev  / Next