structures, unions and enum in c++
#include<iostream>
using namespace std;
//structure is a user defined data type which stores value of other data types.
//arrays ko use kr skte h sirf similar type ke data type ko store krne k liye
typedef struct employee
{
/* data */
int employee_id; //4 bytes
char favchar; //1 byte
float salary; //4 bytes
}KS;
union money
{
/* data */
int rice;//4
char cars;//1 //hum in teeno m se ek time pr sirf ek cheez use krenge
float homes;//4 //teeno mese koi ek set kr skte h. mtlb koi ek trh ka data type use krna chahein
};
//union memory share kradega inn teeno ke beech main
//union 4 (max) byte memory allocate krdega so that teeno ka kaam bn jaega, as use to koi ek hona h
//emplyee yaha pe ek structure data type hai jo different different data types ki
//value ko store kar raha hai
/*typedef keyword ka use ye hai ki typedef lgane ke baad after bracket hum jo bi likhenge (KS)
vahi key word use krr skte hain in place of "struct employee"*/
int main(){
//struct employee krishna;
//struct employee shalini; //emplyee structure use krke hum uske data type
//struct employee krisha; //bana skte hain for example shalini and krisha
KS krishna;
KS shalini;
KS krisha;
krishna.employee_id = 5;
krishna.favchar = 's';
krishna.salary = 57275727;
shalini.employee_id = 7;
shalini.favchar = 'k';
shalini.salary = 57275727;
union money ks;
ks.rice = 57;
//ks.how_many_cars = 4 ye krenge to garbage value milegi as ek time m ek use kr skte h
ks.cars = 'b';
//cout<<ks.rice<<endl;
cout<<ks.cars<<endl;
/***************ENUMS**************/
enum meals {breakfast, lunch, dinner};
//enum ek data type hai jo given chizo ko integer values m store krta hai mtlb unko 012 and so on allot krdeta
meals m1=breakfast;
cout<<m1<<endl;
cout<<breakfast<<endl;
cout<<lunch<<endl;
cout<<dinner<<endl;
cout<<(m1==2)<<endl; //"0" mtlb false
//enums se breakfast lunch aur dinner ko 0 1 2 values allot hui iss trh se program zyada readable hojaega
cout<<"the value of krishna's salary "<<krishna.salary<<endl;
cout<<"the employee id of krishna is "<<krishna.employee_id<<endl;
cout<<"the fav character of krishna is "<<krishna.favchar<<endl;
cout<<"the value of shalini's salary "<<shalini.salary<<endl;
cout<<"the employee id of shalini is "<<shalini.employee_id<<endl;
cout<<"the fav character of shalini is "<<shalini.favchar<<endl;
//unions ekdm structure ki trh hote h but vo better memory management provide krte h
return 0;
}
Comments
Post a Comment