typedef
struct
enum
cpp adds following :
classes
typedef
C support the feature known as type definition that allows user to define an identifier that would represent an exiting data type. The user defined datatype isentifier can later be used to declare variables. It takes the general form.
typedef type identifier;
Where type refers to an existing data type may belong to any class of type , including the user defined ones. Remember that the new type is 'new' only in name , but not the data type. typedef can not create a new type. Some example of type definition are :
typedef int units;
typedef float marks;
Here , unit symbolizes int and mark symbolizes folat. They can be later used to declare variables as follows.
units batch1, batch2;
marks name1[50], name2[50];
Enum
Consider the following PPD = #define
#define APPLE 1 OR const int APPLE = 1;//in cppThe C preprocessor processes all C and C++ files before the compiler is called. The C preprocessor will fill in the defined value for every occurance of the define name. So where ever PEAR appears in the code, the C preprocessor will fill in 2.
#define PEAR 2
#define PEACH 3
#define PLUM 4
While #define constants are better than just using unnamed numeric values, this is a crude way to define a set of values. Later versions of C added enumerations, which are also in C++. Here is the syntax:
enum { single, married, divorced, widower};
enum mar_status { single, married, divorced, widower};
Here single has value = 0 , rest increase by 1. An example of a C++ enumeration, defining the same values is shown below:
typedef enum { APPLE = 1, PEAR, PEACH, PLUM } fruit;In C++ enumerations also got a thin veneer of type safety. To convert an enumeration to an integer it was necessary to use a cast.
fruit basket = (fruit)42;The range for enumeration values is not enforced in C++, so the statement above compiles without error, even though 42 is beyond the enumeration range. In C++ enumeration values can also be assigned to integers without a cast operation:
int y = PLUM;Java does not provide an enumeration type, so it is tempting to use something like the C #define. For example:
class StateMachine
{
public static final int WAIT = 1;
public static final int NICKLE = 2;
public static final int DIME = 3;
public static final int QUARTER = 4;
....
private int currentState = WAIT;
}
Examples:
1. enum { v1 = -1, v2, v3=6, v4, v5, v6, v7} var;
printf("%d" , sizeof(var));
O2(DOS)
4(VC++)
2. printf2(v1, v2, v3, v4, v5, v6, v7);
O
-1 0 6 7 8 9
3. Operators like ++ don't work, because enum are const int.
printf2(++v1);
O.error
Unions
A union is a variable which may hold (at different times) objects of different sizes and types. That is a union hold only one member at a time. C uses the union statement to create unions, for example.
union number { short shortnumber; long longnumber; double floatnumber; } anumber |
Difference between Structure and union is Structure allocate storage space for all the members where as union allow only one member at a time. Application of union is when we need only one member of a Structure for a particular application that time we can use union.
No comments:
Post a Comment