150 likes | 323 Views
The evolution of Pokemon society. Structures. “Good Old Days” At first it was Pokemon and his variables. Name. Id. Gender. Dept. “Old Days” Then there were more Pokemons and their variables. Name_1. Id_1. Gender_1. Dept_1. Name_2. Id_2. Gender_2. Dept_2. “Old Days” And more….
E N D
The evolution of Pokemon society Structures
“Good Old Days”At first it was Pokemon and his variables Name Id Gender Dept
“Old Days”Then there were more Pokemons and their variables Name_1 Id_1 Gender_1 Dept_1 Name_2 Id_2 Gender_2 Dept_2
“Old Days”And more… Name_1 Id_1 Gender_1 Dept_1 Name_2 Id_2 Gender_2 Dept_2 Name_3 Id_3 Gender_3 Dept_3
“Old Days”And more… Name_1 Id_1 Gender_1 Dept_1 Name_2 Id_2 Gender_2 Dept_2 Name_3 Id_3 Gender_3 Dept_3 Name_4 Id_4 Gender_4 Dept_4
“Old Days”And more and more Name_m Id_m Gender_m Dept_m Name_1 Id_1 Gender_1 Dept_1 Name_2 Id_2 Gender_2 Dept_2 Name_3 Id_3 Gender_3 Dept_3 Name_4 Id_4 Gender_4 Dept_4 Name_n Id_n Gender_n Dept_n
“What a mess! How can any Pokemon find his variables? We got to get organized!” Every Pokemon’s variable should stay together!
Name Id Gender Dept Name Id Gender Dept Name Id Gender Dept Name Id Gender Dept New World Order: An almost perfect world! Pokemon_n Pokemon_1 Pokemon_2 Pokemon_3
struct examples • Example: struct Pokemon{ char Name[15]; int Id; char Dept[5]; char Gender; }; The “Pokemon” structure has 4 members.
Name Id Gender Dept Name Id Gender Dept struct basics • Declaration of a variable of struct type: <struct-type> <identifier_list>; • Example: Pokemon Pokemon_1, Pokemon_2; Pokemon_1andPokemon_2are variables of Pokemon type. Pokemon_1 Pokemon_2
Chan Tai Man 12345 M COMP Name Id Gender Dept Ex. 1: struct basics • The members of a struct type variable are accessed with the dot (.) operator: <struct-variable>.<member_name>; • Example: strcpy(Pokemon_1.Name, "Chan Tai Man");Pokemon_1.Id = 12345;strcpy(Pokemon_1.Dept, "COMP");Pokemon_1.gender = 'M';cout << "The student is ";switch (Pokemon_1.gender){case 'F': cout << "Ms. "; break;case 'M': cout << "Mr. "; break;}cout << Pokemon_1.Name << endl; Pokemon_1
Ex. 2: struct-to-struct assignment • The value of one struct type variable can be assigned to another variable of the same struct type. • Example: strcpy(Pokemon_1.Name, "Chan Tai Man");Pokemon_1.Id = 12345;strcpy(Pokemon_1.Dept, "COMP"); Pokemon_1.gender = 'M';Pokemon_2 = Pokemon_1; Pokemon_1 Chan Tai Man 12345 M COMP Chan Tai Man 12345 M COMP Pokemon_2
Name Id Gender Dept Name Id Gender Dept Name Id Gender Dept Name Id Gender Dept New World Order: An almost perfect world! Pokemon[n] Pokemon[0] Pokemon[1] Pokemon[2]
Ex. 2: struct-to-struct assignment • The value of one struct type variable can be assigned to another variable of the same struct type. • Example: strcpy(Pokemon[1].Name, "Chan Tai Man");Pokemon[1].Id = 12345;strcpy(Pokemon[1].Dept, "COMP");Pokemon[1].gender = 'M';Pokemon[2] = Pokemon[1]; Pokemon[1] Chan Tai Man 12345 M COMP Chan Tai Man 12345 M COMP Pokemon[2]