Basic Types & User Defined Types

Basic Types & User Defined Types

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 structure 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: . Character is a primitive type in C, but String is not. Boolean is not a primitive type in C while it is in Java. Examples: . Booleans . Integers . 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 data type, 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 structures . Definition: Cartesian product of n sets A1, A2, A3, …, An is a set whose elements are ordered n-tuples (a1, a2, a3, …, an), where each ak belongs to Ak for 1kn. Example: A. Bellaachia Page: 3 o Structure in C o Records in Pascal and Ada. o A C example: #include <stdio.h> typedef 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 function from a set of values (domain) to a set of values (range): F: integer -> 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 enumerated type 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 value 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 Linked List 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; Abstract Data Type (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++ : template. Example: (www.cpluplus.com) // Create a template function that returns the // greater one of two objects we could use: #include <iostream.h> template <class T> 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<int>(i,j); n=GetMax<long>(l,m); cout << k << endl; cout << n << endl; return 0; } A. Bellaachia Page: 9 .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    9 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us