C++ User Defined data types
Structure and classes
we also used in C language user defined data type such as struct and union in c. while these data types are also legal in c++, some more features have been added to make them
suitable for object oriented programming. C++ language also permits us to define another user defined data type
known as class which can be used, just like any other basic data type, to declare the variables.
the class variables are known as objects, which are the main focus of object-oriented programming. More about these data types is discussed
Enumerated Data type
An enumerated data type is another user defined type which provides a way for attaching the name to the numbers, thereby increasing comprehensibility of the code. The enum keyword (also use in c) automatically enumerates a list of words by assigning them values 0,1,2 and so on. This facility provides an alternative means for
creating symbolic constants. the syntax of an enum statement is similar to the struct statement
take example
shape{circle,square,triangle};
colour{red,blue,green,yellow};
position{off,on};
|
talk about void some the type void was introduced in ANSI C. two normal uses of void are
The enumerated data types differ slightly in c++ when compared with those in ANSII C.
In C++ language, the tag names like shape, colour, and position become new type names. By using these tag names, we can declare new variables
Examples:
shape circle;
colors background
|
ANSI C defines the types of enumes to be ints. In c++ each enumerated data type retains its own seprate type.
This means that c++ language does not permit(offer) an int value to be automatically converted to an enum value
take the example
however en enumerated value can be used in place of an int value
int c=red;
so the by default the enumerators are assigned the integer values starting with 0(zero) for the first enumerators, 1 for the second, and 2 for the third and so on. we can over ride the default values by explicit;y assigning integer values to the enumerators. for example
colour backgrounds=blue
colour backgrounds=7;
colour backgrounds=(colour)/7;
|
colours {red,blue=4,green=8};
colours {blue=4,red,green};
|
are valid definitions.in the first case red is 0(default value), in the second case, red is 5 and green is 6
note: that the subsequent initialized enumerators are larger by one than there predecessors
c++ also permits the creation of anonymous enums (enums without tag names)
example
enum{off,on};
|
here, off is 0 and on is 1. these constant may be referenced in the same manner as regular constant examples
int switch_1=off;
int switch_2=on;
|
so we can also define symbolic constants for a switch statement example
shape
{
circle,rectangle,triangle
};
main(void)
{
code;
std::cout<<"Enter the code of shape";
std::cin>>code;
(code>=circle&&code<=triangle)
{
(code)
{
circle:
;
case rectangle:
;
triangle:
;
}
std::cout<<"Enter shape code";
std::cin>>code;
}
std::cout<<"Bye \n";
0;
}
|
so An enum defined within a class(or structure) is local to that class (or structure) only.