





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






How to structure your data?
How to organization your data?
How to use primitive types to build user-defined types?
How types are implemented?
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
Can do type checking at compile time
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).
Example:
o Int myArray[5] ={-2,0,-3,4,300};
MyArray: 0 -
MyArray: 1 0
MyArray: 2 -
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., field 1
or
field 2 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;
float f;
char c;
} doublyLinkedList;
doublyLinkedList *head;
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.
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;
}
Generic ADT: