Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ New Types.

Similar presentations


Presentation on theme: "C++ New Types."— Presentation transcript:

1 C++ New Types

2 Enumeration type (1) enum fruit_tea{apple, banana, orange};
fruit_tea taste; if (taste==apple) cout<<“Taste like apple.”; else if (taste==banana) cout<<“Taste like banana.”; else cout<<“Taste like orange.”; cout<<apple<<“,”<<banana<<“,”<<orange<<endl;

3 Enumeration type (2) enum fruit_tea{apple, banana, orange};
enum boolean {unknow=-1, false, true, off=0, on, yes}; //on=1, yes=2 enum gender{male, female} gender, animal[15]; //enumeration type 名稱可與變數名稱相同 enum {red, green, blue} color; //enumeration type 名稱可省略

4 struct, union and class {struct|union|class} type_name {members} variable_name;

5 struct struct Time { int hour; int minute; int second; };
Time timeObject; Time timeArray[ 10 ]; Time *timePtr; Time &timeRef = timeObject;

6 struct Dot operator (.) for structure and class members
Arrow operator (->) for structure and class members via pointer to object Print member hour of timeObject: cout << timeObject.hour; OR timePtr = &timeObject; cout << timePtr->hour; timePtr->hour same as ( *timePtr ).hour Parentheses required * lower precedence than .

7 union To store variable using the same space E.G.:
union {int a; char b[2];} d; //low byte is stored first, low-indexed element is stored first d.a=0x1234; cout<<hex<<d.a<<endl; //1234 cout<<hex<<(int)d.b[0]<<endl; //34 cout<<hex<<(int)d.b[1]<<endl; //12 d.b[0]=0x56; cout<<hex<<d.a<<endl; //5612

8 typedef for alias typedef char* string; string str=“abcde”;
string s[]={“abc”,”def”,”ghi”}; typedef unsigned char byte; byte a[4],*p,c; tyepdef struct {int a, int b} name; name s1,s2; typedef struct tagname {int a, int b} newname; newname n1,n2; tagname t;

9 The End


Download ppt "C++ New Types."

Similar presentations


Ads by Google