how to use enum datatype?Please explain me?

Answer Posted / deepak mundhada

enum is a user defined data type . we have to use enum
keyword to specify it.
eg. #include<iostream.h>
class A
{ public:
enum Colour
{ colour_green=0,
colour_peach=0,
colour_white =1,
colour_yellow =2
};
//we cannot have color_green 2 times here
/*enum Colour
{ colour_green=0,
colour_peach=0,
colour_white =1,
colour_yellow =2,
colour_green=3, };
*/
//We cannot have another variable of same type as in enum
Colour
//as below
//int colour_yellow;

//We cannot have colour_white as it already declared in
enum Color
/*enum MyColour
{
colour_red, colour_white };
*/
//We cannot have another Struct or Union of same type as
below
//in the same scope
/*struct Colour
{ int red;
int green; };
*/

void compare(Colour colour1, Colour colour2);
};
void A::compare(A::Colour colour1, A::Colour colour2)
{ if(colour1 == colour2)
{ cout<<"colour1 and colour2 are same\n"; }
}}
int main()
{ A a;
a.compare(A::colour_green,A::colour_peach);
return(0);
}
Output:./a.out
colour1 and colour2 are same

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are header files and what are its uses in C programming?

713


How does free() know explain how much memory to release?

706


What are the data types present in c?

720


Explain what will be the outcome of the following conditional statement if the value of variable s is 10?

855


When should the const modifier be used?

747






What is the use of volatile?

733


What is the difference between struct and union in C?

729


List some of the static data structures in C?

855


How many main () function we can have in a project?

721


What is structure padding in c?

727


Give a one-line C expression to test whether a number is a power of 2. [No loops allowed - it's a simple test.]

723


Why are all header files not declared in every c program?

700


Explain how can you check to see whether a symbol is defined?

756


What is a keyword?

856


What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?

728