Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types
Prof. amr Goneid, AUC2 More on Simple Data Types
Prof. amr Goneid, AUC3 More on Simple Data Types #define Directive Promotion of Types Type Casting typedef Declaration Enumerated Data Types
Prof. amr Goneid, AUC4 1. #define Directive Used to give meaningful names to constants Used in older C programs prior to introduction of constants #define Example #define pi // The same as const float pi = ;
Prof. amr Goneid, AUC5 #define Directive (Example) // File: try.cpp // Uses #define Directive #include using namespace std; #define BEGIN { #define END } #define MOD % #define EQ ==
Prof. amr Goneid, AUC6 #define Directive (Example) int main() BEGIN // Local data... intn, d ; cout > n ; for ( ; ; ) BEGIN d = n MOD 2; cout << d; n /= 2; if (n EQ 0) break; END cout << endl; return 0; END
Prof. amr Goneid, AUC7 2. Promotion of Types Type promotion promoting a lower type to a higher type, e.g. 3 + x /2 if x is float, constants would be promoted to float as well and actually be 2.0 and 3.0 Type conversions int to float (number.0) float to int (truncation occurs)
Prof. amr Goneid, AUC8 Mixing Types Example: float x, z ; int y; x = 3.89 ; y = x ; z = y ; // y would contain 3 and z would contain 3.0
Prof. amr Goneid, AUC9 Avoid mixing types but if you need to you can cast a type Type casting allows you to change a type within the program for a specific function Form: type (variable)or (type) variable Example: int n ; float sum, average ; average = sum / float (n); // or average = sum / (float) n ; 3. Type Casting
Prof. amr Goneid, AUC10 Type Casting Example Example: for (j = 0; j <= 25; j++) { for (k = 0; k <= j; k++) cout << char (int (‘A’) + k); cout << endl; }
Prof. amr Goneid, AUC11 Used to give other names to existing data types. The new name can then be used as a qualified data type. Original type remains active. Syntax: typedef ; 4. typedef Declaration
Prof. amr Goneid, AUC12 typedef Declaration Examples: typedef int itemtype ; typedef float costType ; typedef char gradeType ; ……… itemtype item ; …… item = 7 ; costtype cost ; …… cost = ; gradetype grade ; …grade = ‘B’ ;
Prof. amr Goneid, AUC13 5. Enumerated Data Types
Prof. amr Goneid, AUC14 Of course C++ will give errors for the following declarations: icecream scoop ; fruit summerfruit ; color thiscolor ; …………… scoop = vanilla; summerfruit = mango; thiscolor = red ; fruit2 = fruit (int(mango) + 1); How can we make C++ accept these types? Enumerated Data Types
Prof. amr Goneid, AUC15 Enumerated Types Enumerated Data Types: Create new ordinal data types by enumerating their elements in ascending rank ( 0, 1,2 …). Example: enum weekday {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; ……. weekday day ; day = Mon ;
Prof. amr Goneid, AUC16 Using Enumerated Types All operations on ordinal types can be performed on enumerated types. enum classid {freshman, sophomore, junior, senior}; classid newClass; if (newClass == freshman) do something else if (newClass == sophomore) …..
Prof. amr Goneid, AUC17 Examples: enum color {red,green,blue,yellow}; color color1,color2, c ; color1 = red; color2 = color (int(green) +1); for(c = red; c <= yellow; c++) cout << int(c) << “ “ ; cout << endl; color1 = color (3); if(color1 > blue) cout << “yellow”; yellow Using Enumerated Types
Prof. amr Goneid, AUC18 Example: color.cpp // DISPLAYS THE VALUE OF thisColor void writeColor (color thisColor) { switch (thisColor) { case red: cout << "red"; break; case green: cout << "green"; break;
Prof. amr Goneid, AUC19 color.cpp case blue: cout << "blue"; break; case yellow: cout << "yellow"; break; default: status = 0; cerr << "*** ERROR: Invalid color value." << endl; }
Prof. amr Goneid, AUC20 Explicit Enumeration C++ allows to give explicit values to enumerators. For Example: enum NumberBase { Binary = 2, Octal = 8, Decimal = 10, Hexa = 16}; We can also start from a number other than zero. For example: enum German { Ein = 1, Zwei, Drei, Vier};