Download presentation
Presentation is loading. Please wait.
Published byGregory Murphy Modified over 9 years ago
1
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types
2
Prof. amr Goneid, AUC2 More on Simple Data Types
3
Prof. amr Goneid, AUC3 More on Simple Data Types #define Directive Promotion of Types Type Casting typedef Declaration Enumerated Data Types
4
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 3.14159 // The same as const float pi = 3.14159;
5
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 ==
6
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
7
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)
8
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
9
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
10
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; }
11
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
12
Prof. amr Goneid, AUC12 typedef Declaration Examples: typedef int itemtype ; typedef float costType ; typedef char gradeType ; ……… itemtype item ; …… item = 7 ; costtype cost ; …… cost = 21.36 ; gradetype grade ; …grade = ‘B’ ;
13
Prof. amr Goneid, AUC13 5. Enumerated Data Types
14
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
15
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 ;
16
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) …..
17
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”;0 1 2 3 yellow Using Enumerated Types
18
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;
19
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; }
20
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};
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.