User-Defined Data Types

Default Image

C++ is a flexible language, it allows user to define their own data types. User can do this with a keyword typedef. Format to declare a user defined data type is:

                     typedef existing_type new_type_name


          In above format existing_type
indicate the standard data types of C++ and new_type_name shows the data
type name defined by the user. for example, if:


  • typedef char C
  • typedef unsigned int U
  • typedef char box [30]

           In this case we defined three
data types: C, U, and field as char, unsigned int, and char[30]
respectively, that we could perfectly use in declarations later as any
other valid type:


  • C ultra, fest,mob;
  • U nin;
  • box name;

            typedef does not create
different types. It only creates synonyms of existing types. That means
that the type of nin can be either U or unsigned int, since both are in
fact the same type. typedef can be useful to define an alias for a type
that is frequently used within a program. It is also useful to define
types when it is possible that we will need to change the type in later
versions of our program, or if a type you want to use has a name that is
too long or confusing.

Read Count: 3 times

Comments

No comments yet.

Leave a Comment