The evolution of Pokemon society Structures COMP 102

  • Slides: 14
Download presentation
The evolution of Pokemon society Structures

The evolution of Pokemon society Structures

COMP 102 Prog. Fundamentals, Structures / Slide 2 “Good Old Days” At first it

COMP 102 Prog. Fundamentals, Structures / Slide 2 “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 COMP 102 Prog. Fundamentals,

“Old Days” Then there were more Pokemons and their variables COMP 102 Prog. Fundamentals, Structures / Slide 3 Name_1 Id_1 Gender_1 Name_2 Id_2 Dept_1 Gender_2 Dept_2

COMP 102 Prog. Fundamentals, Structures / Slide 4 “Old Days” And more… Name_1 Id_1

COMP 102 Prog. Fundamentals, Structures / Slide 4 “Old Days” And more… Name_1 Id_1 Name_2 Name_3 Id_3 Gender_1 Id_2 Gender_3 Dept_1 Gender_2 Dept_3 Dept_2

COMP 102 Prog. Fundamentals, Structures / Slide 5 “Old Days” And more… Name_1 Id_1

COMP 102 Prog. Fundamentals, Structures / Slide 5 “Old Days” And more… Name_1 Id_1 Gender_1 Name_2 Name_3 Id_3 Name_4 Id_2 Gender_3 Id_4 Dept_1 Gender_2 Dept_3 Gender_4 Dept_2

COMP 102 Prog. Fundamentals, Structures / Slide 6 “Old Days” And more and more

COMP 102 Prog. Fundamentals, Structures / Slide 6 “Old Days” And more and more Name_1 Id_1 Name_m Name_2 Name_3 Id_3 Name_4 Name_n Id_m Gender_1 Id_2 Gender_3 Id_4 Gender_m Dept_1 Gender_2 Dept_3 Gender_4 Gender_n Dept_4 Dept_2

COMP 102 Prog. Fundamentals, Structures / Slide 7 “What a mess! How can any

COMP 102 Prog. Fundamentals, Structures / Slide 7 “What a mess! How can any Pokemon find his variables? We got to get organized!” Every Pokemon’s variable should stay together!

COMP 102 Prog. Fundamentals, Structures / Slide 8 New World Order: An almost perfect

COMP 102 Prog. Fundamentals, Structures / Slide 8 New World Order: An almost perfect world! Pokemon_1 Pokemon_2 Pokemon_3 Pokemon_n Name Id Dept Gender

COMP 102 Prog. Fundamentals, Structures / Slide 9 struct examples l l Example: struct

COMP 102 Prog. Fundamentals, Structures / Slide 9 struct examples l l Example: struct Pokemon{ char Name[15]; int Id; char Dept[5]; char Gender; }; The “Pokemon” structure has 4 members.

COMP 102 Prog. Fundamentals, Structures / Slide 10 struct basics l Declaration of a

COMP 102 Prog. Fundamentals, Structures / Slide 10 struct basics l Declaration of a variable of struct type: <struct-type> <identifier_list>; l Example: Pokemon_1, Pokemon_2; Name Pokemon_1 Id Dept Name Gender Id Gender Pokemon_2 Dept and Pokemon_2 are variables of Pokemon type. Pokemon_1

COMP 102 Prog. Fundamentals, Structures / Slide 11 Ex. 1: struct basics l The

COMP 102 Prog. Fundamentals, Structures / Slide 11 Ex. 1: struct basics l The members of a struct type variable are accessed with the dot (. ) operator: Pokemon_1 <struct-variable>. <member_name>; l 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; Name Id Gender Dept Chan Tai Man 12345 COMP M

COMP 102 Prog. Fundamentals, Structures / Slide 12 Ex. 2: struct-to-struct assignment l l

COMP 102 Prog. Fundamentals, Structures / Slide 12 Ex. 2: struct-to-struct assignment l l The value of one struct type variable can be assigned to another variable of the same Pokemon_1 struct type. Example: Chan Tai Man strcpy(Pokemon_1. Name, "Chan Tai Man"); Pokemon_1. Id = 12345; strcpy(Pokemon_1. Dept, "COMP"); Pokemon_1. gender = 'M'; 12345 Pokemon_2 = Pokemon_1; Chan Tai Man COMP 12345 Pokemon_2 M COMP M

COMP 102 Prog. Fundamentals, Structures / Slide 13 New World Order: An almost perfect

COMP 102 Prog. Fundamentals, Structures / Slide 13 New World Order: An almost perfect world! Pokemon[0] Pokemon[1] Pokemon[2] Pokemon[n] Name Id Dept Gender

COMP 102 Prog. Fundamentals, Structures / Slide 14 Ex. 2: struct-to-struct assignment l l

COMP 102 Prog. Fundamentals, Structures / Slide 14 Ex. 2: struct-to-struct assignment l l The value of one struct type variable can be assigned to another variable of the same Pokemon[1] struct type. Example: Chan Tai Man strcpy(Pokemon[1]. Name, "Chan Tai Man"); Pokemon[1]. Id = 12345; strcpy(Pokemon[1]. Dept, "COMP"); Pokemon[1]. gender = 'M'; 12345 Pokemon[2] = Pokemon[1]; Chan Tai Man COMP 12345 Pokemon[2] M COMP M