WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined - - PowerPoint PPT Presentation

with c
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1
  • Prof. amr Goneid, AUC

1

CSCE 110 PROGRAMMING FUNDAMENTALS

WITH C++

  • Prof. Amr Goneid

AUC Part 6. Simple and User Defined Data Types

slide-2
SLIDE 2
  • Prof. amr Goneid, AUC

2

More on Simple Data Types

slide-3
SLIDE 3
  • Prof. amr Goneid, AUC

3

More on Simple Data Types

 #define Directive  Promotion of Types  Type Casting  typedef Declaration  Enumerated Data Types

slide-4
SLIDE 4
  • Prof. amr Goneid, AUC

4

  • 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;

slide-5
SLIDE 5
  • Prof. amr Goneid, AUC

5

#define Directive (Example)

// File: try.cpp // Uses #define Directive #include <iostream> using namespace std; #define BEGIN { #define END } #define MOD % #define EQ ==

slide-6
SLIDE 6
  • Prof. amr Goneid, AUC

6

#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

slide-7
SLIDE 7
  • Prof. amr Goneid, AUC

7

  • 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)

slide-8
SLIDE 8
  • Prof. amr Goneid, AUC

8

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

slide-9
SLIDE 9
  • Prof. amr Goneid, AUC

9

 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)

  • r (type) variable

 Example:

int n ; float sum , average ; average = sum / float (n); // or average = sum / (float) n ;

  • 3. Type Casting
slide-10
SLIDE 10
  • Prof. amr Goneid, AUC

10

Type Casting Example

 Example:

for (j = 0; j <= 25; j++) { for (k = 0; k <= j; k++) cout << char (int (‘A’) + k); cout << endl; }

slide-11
SLIDE 11
  • Prof. amr Goneid, AUC

11

 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> ;

  • 4. typedef Declaration
slide-12
SLIDE 12
  • Prof. amr Goneid, AUC

12

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’ ;

slide-13
SLIDE 13
  • Prof. amr Goneid, AUC

13

  • 5. Enumerated Data Types
slide-14
SLIDE 14
  • Prof. amr Goneid, AUC

14

 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

slide-15
SLIDE 15
  • Prof. amr Goneid, AUC

15

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 ;

slide-16
SLIDE 16
  • Prof. amr Goneid, AUC

16

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) …..

slide-17
SLIDE 17
  • Prof. amr Goneid, AUC

17

 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

slide-18
SLIDE 18
  • Prof. amr Goneid, AUC

18

Example: color.cpp

// DISPLAYS THE VALUE OF thisColor void writeColor (color thisColor) { switch (thisColor) { case red: cout << "red"; break; case green: cout << "green"; break;

slide-19
SLIDE 19
  • Prof. amr Goneid, AUC

19

color.cpp

case blue: cout << "blue"; break; case yellow: cout << "yellow"; break; default: status = 0; cerr << "*** ERROR: Invalid color value." << endl; } }

slide-20
SLIDE 20
  • Prof. amr Goneid, AUC

20

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};