<<

Basic Types & User Defined Types

1. Objectives ...... 2 2. Built-in Types and Primitive Types...... 2 3. Data Aggregates and Type Constructors ...... 3 4. Constructors ...... 3 5. User-defined Types and Abstract Data Types ...... 7

A. Bellaachia Page: 1

1. Objectives

 How to your data?  How to organization your data?  How to use primitive types to build user-defined types?  How types are implemented?

2. Built-in Types and Primitive Types

 What is a type? . Values . Operations

 A primitive type mimics hardware units.

 We call primitive types those types that are not built from other types.

 Example: . is a primitive type in , but String is not. . Boolean is not a primitive type in C while it is in Java.

 Examples: . Booleans . . Reals . Characters

 Benefits: . Hides underlying representation

A. Bellaachia Page: 2

. Can do type checking at compile time . Can resolve overloaded functions at compile time . Programmer can specify the accuracy required

3. Data Aggregates and Type Constructors  Definition: . An aggregate , also called compound object, consists of one or more data type objects. . Example: o Arrays: consists of one or more elements of a single data type placed in contiguous locations from first to last.

 Constructors: . It is the method used to create an aggregate data type. . Examples: o Cartesian Products o Sequences o Etc.

4. Constructors

 Cartesian Product: . A constructor used to create records and . Definition: Cartesian product of n sets A1, A2, A3, …, An is a whose elements are ordered n-tuples (a1, a2, a3, …, an), where each ak belongs to Ak for 1kn. . Example:

A. Bellaachia Page: 3

o Structure in C o Records in Pascal and Ada. o A C example: #include

struct { char name[30]; int age; char ssn[10]; } person;

main (){

person myName = {"Bell", 20, "222222222"};

printf("Person Info: \n"); printf("\t\t%s\n\t\t%d\n\t\t%s\n", myName.name,myName.age, myName.ssn);

}

 Mapping . Definition: o A mapping is a from a set of values (domain) to a set of values (range):

F: -> real

. Array Constructor: is a mapping from a set of integers (index) to a set of values (content of the array).

A. Bellaachia Page: 4

. Example: o Int myArray[5] ={-2,0,-3,4,300};

MyArray: 0  -2 MyArray: 1  0 MyArray: 2  -3 MyArray: 3  4 MyArray: 4  300

. Another Example:

enum Colors {red, white, black, green, blue};

The compiler assigns an integer number to each name in the starting from zero.

 Union and Discriminated Union

. Union is a constructor that allows objects to be specified by a disjunctive of fields, e.g., field1 or field2 or ... or fieldn.

. An instance object has only one field: Save space.

. It is up to the programmer to remember which address is valid

. Example: o Variant records in Pascal o Union structures in C:

union mixed { int i;

A. Bellaachia Page: 5

float f; char c; };

union mixed m;

m.i = 10;

 Powerset: . Powerset(T): This is a constructor that creates a variable whose is a set of elements of type T. . T is called the base type. . Example: o set type in Pascal o Java APIs: Sets.

 Sequence: . Sequences constructor allows the creation of objects whose number of elements is not specified.

. Objects will have arbitrary size.

. Example: sequential files, Vector, List in Java.

 Recursion . Recursion is a constructor that allows the size of an object to grow dynamically (add or delete elements). . Example: Create the structure of a in C” typedef strcut { A. Bellaachia Page: 6

DoulyLinkedList * previous; Char name[30]; DoulyLinkedList * next;

} doublyLinkedList; doublyLinkedList *head;

5. User-defined Types and Abstract Data Types

 The ability to create new data types using primitive data types.

 Example:

typedef struct { int numerator; int denominator; } fraction;

Define variables of type fraction:

fraction f1, f2;

(ADT)

. It is an extension to the record or structure construct: Plus routines

. C++/Java: ADT is a class construct.

. Constructor: It allocates and initializes the fields of an ADT. Languages usually provides a default constructor and default initializations. A. Bellaachia Page: 7

. Deconstructor:

o It releases the memory allocated for an instance.

o C++: a deconstructor has the name of the class prefixed by ‘~’

o Java: provide a garbage collector to clean memory allocated by an object.

. Example: C++

class fraction{

public: fraction (int i, int j){ numerator = i; denominator = j; } fraction (){ numerator = 0; denominator = 0; } fraction mul (fraction f1, fraction f2){

fraction f(0,0); f.numerator = f1.numerator * f1.numerator; f.denominator = f1.denominator * f1.denominator; return (f); }

print (){ cout << numerator/denominator; }

private: int numerator; int denominator; }

A. Bellaachia Page: 8

 Generic ADT:

. An ADT that takes data types as parameters.

. C++ : .

. Example: (www.cpluplus.com)

// Create a template function that returns the // greater one of two objects we could use: #include

template T GetMax (T a, T b) { T result; result = (a>b)? a : b; return (result); }

int main () { int i=5, j=6, k; long l=10, m=5, n; k=GetMax(i,j); n=GetMax(l,m); cout << k << endl; cout << n << endl; return 0; }

A. Bellaachia Page: 9