Building Your own data types
- typedef existingname new name
- typedef int NumberType;
- This is useful when you want to think about data in a
different way.
- You may want to allow for future changes in your code.
- Develop your code with typedef float real;
- later replace this with typedef long double real;
- This is sort of equivalent to declaring a constant.
- You may want to think about something differently and the label will make this easier. More on this later.
- You may want to declare your own data type, with a reasonable name, but more on this later.
- Enumerated Types
- You can declare your own type with it's own values it can assume.
- This is an simple type.
- enum typename { enumerator1, enumator2, ... enumatorn}
- enum typename { enumerator1=intConstat, enumator2=intConstant, ... enumatorn=intConstant}
- enum MonthT {JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER};
- This declares a new type, MonthT, which we can
- Declare constants and variables,
- Declare routines which take/return values of this type
- Declare more complex types constructed from this type (eventually)
- The rules
- The type must be a valid identifier
- I think UpperCaseT would be good for identifiers
- The enumerators (values inside of {}) are also identifiers
- They should be treated as constants when naming.
- You can increment enumerated type variables
- They are mapped to the integers, so it works
- But what happens is the type is coerced to an int and the result is an int.
- So it must be cast back to the enumerated type
- MonthT m=JANUARY;
- MonthT(m+1)
- Generally when you build a new type, you build a set of supporting
routines to accompany it.
- You also build any constants necessary to support them.
- When building an enumerated type I almost always
- Build a routine that takes a version of the enumerated type and
returns a corresponding string.
- Build a routine that takes a string and returns the corresponding type
- Build FIRST_ and LAST_ constants
- Build _SIZE
- Please see myCal.C