Structure, union, enum
Is there object oriented world only?
Task 0
Create a program, which will create a point_3d structure. Next, create a variable of the structure.
struct student
{
int index;
int semester;
};
int main()
{
student s1, s2, s3;
s1.index = 1;
s1.semester = 3;
s1.index = 2;
s1.semester = 5;
s1.index = 3;
s1.semester = 7;
}
Task 1
Create a new structure (for example: car, university). Add some fields (numeric ones) and own enum. Can we create a value in enum called X, if X has been definied as a structure.
enum person_type
{
guest,
student,
lecturer,
professor
};
int main()
{
person_type somebody = guest;
switch(somebody)
{
case student:
case lecturer:
case professor:
std::cout << "Member of an university" << std::endl;
break;
case guest:
std::cout << "Anybody" << std::endl;
break;
}
}
Task 2
Create student’s structure:
Create two method headers before main function:
Modify main function:
Create definitions for:
int main()
{
std::string text;
std::getline(std::cin, text);
}
Task 3
Compare structure based programming and object oriented.
What is familiar? What is not?
Task 4
Create an union definition.
Then create a variable of created type.
union X
{
int integer;
float floating;
};
int main()
{
X x;
x.integer = 3;
x.floating = 3.14f;
std::cout << x.integer << std::endl;
std::cout << x.floating << std::endl;
}
Task 5
Create a namespace with 3 structures.
One of the structures should contain one member of another structure.
Create some methods headers inside a namespace.
You should use own enums as well.
Create implementations for yours methods.
Create an usage case for your code in main method.