200 likes | 440 Views
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++. Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types. More on Simple Data Types. More on Simple Data Types. #define Directive Promotion of Types Type Casting typedef Declaration Enumerated Data Types. 1. #define Directive.
E N D
CSCE 110PROGRAMMING FUNDAMENTALSWITH C++ Prof. AmrGoneid AUC Part 6. Simple and User Defined Data Types Prof. amr Goneid, AUC
More on Simple Data Types Prof. amr Goneid, AUC
More on Simple Data Types • #define Directive • Promotion of Types • Type Casting • typedef Declaration • Enumerated Data Types Prof. amr Goneid, AUC
1. #define Directive • Used to give meaningful names to constants • Used in older C programs prior to introduction of constants #define <token> <replacement-text> • Example #define pi 3.14159 // The same as const float pi = 3.14159; Prof. amr Goneid, AUC
#define Directive (Example) // File: try.cpp // Uses #define Directive #include <iostream> using namespace std; #define BEGIN { #define END } #define MOD % #define EQ == Prof. amr Goneid, AUC
#define Directive (Example) int main() BEGIN // Local data ... int n , d ; cout << "Enter n : "; cin >> 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, AUC
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, AUC
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, AUC
3. Type Casting • 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 ; Prof. amr Goneid, AUC
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, AUC
4. typedef Declaration • 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 <C++ type> <other name> ; Prof. amr Goneid, AUC
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’ ; Prof. amr Goneid, AUC
5. Enumerated Data Types Prof. amr Goneid, AUC
Enumerated Data Types • 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? Prof. amr Goneid, AUC
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, AUC
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, AUC
Using Enumerated Types • 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 Prof. amr Goneid, AUC
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, AUC
color.cpp case blue: cout << "blue"; break; case yellow: cout << "yellow"; break; default: status = 0; cerr << "*** ERROR: Invalid color value." << endl; } } Prof. amr Goneid, AUC
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}; Prof. amr Goneid, AUC