// This tut contains Structures,Unions & Enums in C++
#include<iostream>
using namespace std;
//******************** Structures ***************************"
// Structures are different than array. Array contains only 1 data type elements not more like only int,only char etc
// Structures hold different data types elements.
// struct employee
// {
// /* data */
// int eId;
// char favChar;
// float salary;
// };
// int main(){
// cout<<"Without using typedef :"<<endl;
// struct employee harry;
// harry.eId=1;
// harry.favChar='N';
// harry.salary=200000;
// cout<<"The value is :"<<harry.eId<<endl;
// cout<<"The value is :"<<harry.favChar<<endl;
// cout<<"The value is :"<<harry.salary<<endl;
// return 0;
// }
typedef struct employee
{
/* data */
int eId;
char favChar;
float salary;
} ep;
// Union is one which is used to calculate value from any one of the data types means you can assign value to any one data type,
// and your work will be done by calling any one data type.
union money
{
/* data */
int rice;
char car;
float pounds;
};
int main(){
ep harry;
harry.eId=1;
harry.favChar='N';
harry.salary=200000;
cout<<"Using typedef and ep :"<<endl;
cout<<"The value is :"<<harry.eId<<endl;
cout<<"The value is :"<<harry.favChar<<endl;
cout<<"The value is :"<<harry.salary<<endl;
union money m1;
m1.pounds= 650.50;
cout<<"m1.pounds"<<m1.pounds<<endl;
cout<<"m1.car"<<m1.pounds<<endl; // Call this(m1.pound) only once otherwise it will return garbage value
// enum will return index value of element in enum
enum Meal{ breakfast, lunch, dinner};
Meal M1 = lunch;
cout<<(M1==2);
cout<<breakfast<<endl;
cout<<lunch<<endl;
cout<<dinner<<endl;
return 0;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.