- Prof. amr Goneid, AUC
1
CSCE 110 PROGRAMMING FUNDAMENTALS
WITH C++
- Prof. Amr Goneid
WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined - - PowerPoint PPT Presentation
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types Prof. amr Goneid, AUC 1 More on Simple Data Types Prof. amr Goneid, AUC 2 More on Simple Data Types #define Directive
1
2
3
4
Used to give meaningful names to constants Used in older C programs prior to introduction of
Example
5
6
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
7
Type promotion
promoting a lower type to a higher type, e.g.
if x is float, constants would be promoted to float
Type conversions
int to float (number.0) float to int (truncation occurs)
8
9
Avoid mixing types but if you need to you can cast a
Type casting allows you to change a type within the
Form:
Example:
10
11
12
13
14
15
Enumerated Data Types:
Example:
16
17
18
// DISPLAYS THE VALUE OF thisColor void writeColor (color thisColor) { switch (thisColor) { case red: cout << "red"; break; case green: cout << "green"; break;
19
case blue: cout << "blue"; break; case yellow: cout << "yellow"; break; default: status = 0; cerr << "*** ERROR: Invalid color value." << endl; } }
20