Nama : Deab Sina
NIM : 49012078
Jurusan : D4 TMD ITB Batch 6
This is video about:struct, enumeration, bit-field, typedef,
union
Link to this video:
Source code:
===================================================
+ struct.cpp
===================================================
#include <iostream>
using namespace std;
using namespace std;
struct database{
int id_number;
int age;
float salary;
};
int main()
{
database employee; // There is now an variable that has modifiable // variable inside it
employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.2;
cout<<"employee age = "<<employee.age<<endl;
cout<<"employee id_number = " <<employee.id_number<<endl;
cout<<"employee salary = "<<employee.salary<<endl;
system("pause");
return 0;
}
===================================================
+ union.cpp
===================================================
// union.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
using namespace std;
union sample{
int p;
};
int _tmain(int argc, _TCHAR* argv[])
{
// Union data type
union sample content;
content.p =37;
cout<<"Display the union storage content"<<endl;
cout<<" ONLY one at a time!"<<endl;
cout<<"---------------------------------"<<endl;
cout<<"Integer : "<<content.p<<endl;
system("pause");
return 0;
}
#include "stdafx.h"
#include <iostream>
using namespace std;
union sample{
int p;
};
int _tmain(int argc, _TCHAR* argv[])
{
// Union data type
union sample content;
content.p =37;
cout<<"Display the union storage content"<<endl;
cout<<" ONLY one at a time!"<<endl;
cout<<"---------------------------------"<<endl;
cout<<"Integer : "<<content.p<<endl;
system("pause");
return 0;
}
===================================================
+ enumeration.cpp
===================================================
// enumeration.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
enum days {mon, tue, wed, thu, fri, sat, sun};
days daysinweek; // day in a week
daysinweek = wed; // day in a week is wed
switch (daysinweek){
case sun: cout <<"Sunday"<<endl;
break;
case mon: cout <<"Monday"<<endl;
break;
case tue: cout <<"Tuesday"<<endl;
break;
case wed: cout <<"Wednesday"<<endl;
break;
case thu: cout <<"Thursday"<<endl;
break;
case fri: cout <<"Friday"<<endl;
break;
case sat: cout <<"Saturday"<<endl;
break;
}
system("pause");
return 0;
}
===================================================
+ typedef.cpp
===================================================
// typedef.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef struct TestStruct
{
int p;
char q;
double r;
} mine;
int _tmain(int argc, _TCHAR* argv[])
{
mine testvar; // the declaration becomes simpler
testvar.p = 200;
testvar.q = 'T';
testvar.r = 1.234;
cout<<"testvar.p = "<<testvar.p<<endl;
cout<<"testvar.q = "<<testvar.q<<endl;
cout<<"testvar.r = "<<testvar.r<<endl;
system("pause");
return 0;
}
===================================================
+ bit-field.cpp
===================================================
// bit_field.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <assert.h>
using namespace std;
class MyDay{
unsigned day : 31;// bit field (32 bit)
public:
void SetDay(int aDay){
assert ( aDay <31);
day = aDay;
}
void Print(){
cout<<"Day is : "<<day<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
MyDay d;
d.SetDay(20);
d.Print();
cout<<"Size of MyDay = "<<sizeof(d)<<endl;
system("pause");
return 0;
}
// bit_field.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <assert.h>
using namespace std;
class MyDay{
unsigned day : 31;// bit field (32 bit)
public:
void SetDay(int aDay){
assert ( aDay <31);
day = aDay;
}
void Print(){
cout<<"Day is : "<<day<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
MyDay d;
d.SetDay(20);
d.Print();
cout<<"Size of MyDay = "<<sizeof(d)<<endl;
system("pause");
return 0;
}
No comments:
Post a Comment