Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 202 Computer Science II Lab Fall 2009 October 15.

Similar presentations


Presentation on theme: "CS 202 Computer Science II Lab Fall 2009 October 15."— Presentation transcript:

1 CS 202 Computer Science II Lab Fall 2009 October 15

2 Data Types Structure Typedef Union Enum

3 Data Types Structure struct product { int weight; float price; } apple; apple.price

4 Data Types Structure struct product { int weight; float price; } apple, banana, melon; apple.price banana.weight

5 Data Types Structure struct product { int weight; float price; } fruit[10]; fruit[5].price fruit[5].weight

6 Data Types Pointer to Structure *(apple.weight) Value pointed by member weight of object apple *apple.weight (*apple).weight Member weight of object pointed by apple apple->weight Member weight of object apple apple.weight EquivalentWhat is evaluated Expression

7 Data Types Nesting Structure struct movies_t { string title; int year; }; struct friends_t { string name; string email; movies_t favorite_movie; } charlie, maria; friends_t * pfriends = &charlie; charlie.name maria.favorite_movie.title charlie.favorite_movie.year pfriends->favorite_movie.year

8 Data Types Typedef – Defined data types typedef char C; typedef unsigned int WORD; typedef char * pChar; typedef char field [50]; C mychar, anotherchar, *ptc1; WORD myword; pChar ptc2; field name;

9 Data Types Union – allows same portion of memory to be accessed as different data types union myTest { char c; int i; float f; } myVar; myVar.c myVar.i myVar.f

10 Data Types Union union mix_t { long l; struct { short hi; short lo; } s; char c[4]; } mix; 0xA70x430x510x12 mix mix.l mix.s.himix.s.lo mix.c[0]mix.c[1]mix.c[2]mix.c[3]

11 Comparison Between Struct and Union in terms of Memory Space #include using namespace std; struct myStruct{ bool a; bool c; char b[4]; } A; int main(){ cout<<sizeof(A)<<'\n'; return 0; } Answer: >>6 #include using namespace std; union myUnion{ bool a; bool c; char b[4]; } A; int main(){ cout<<sizeof(A)<<'\n'; return 0; } Answer: >>4

12 Data Types Anonymous Union struct { char title[50]; char author[50]; union { float dollars; int yens; }; } book; struct { char title[50]; char author[50]; union { float dollars; int yens; } price; } book; structure with anonymous union structure with regular union book.price.dollars book.price.yens book.dollars book.yens

13 Data Types Enum – Enumerations create new data types enum colors_t {black, blue, green, cyan, red, purple, yellow, white}; colors_t mycolor; … mycolor = blue; if (mycolor == green) mycolor = red; enum months_t { january, february, march, april, may, june, july, august, september, october, november, december } myMonth; enum months_t { january=1, february, march, april, may, june, july, august, september, october, november, december } myMonth; myMonth = January; /* myMonth=0 */ myMonth = february; /* myMonth=1 */ myMonth = march; /* myMonth=2 */ myMonth = January; /* myMonth=1 */ myMonth = february; /* myMonth=2 */ myMonth = march; /* myMonth=3 */

14 Questions?

15 Data Types #include using namespace std; struct myStruct{ bool a; bool c; char b[4]; } A; int main(){ cout<<sizeof(A)<<'\n'; return 0; }


Download ppt "CS 202 Computer Science II Lab Fall 2009 October 15."

Similar presentations


Ads by Google