Structuring the Data - Software Paradigms | CSCI 169, Study notes of Computer Science

Material Type: Notes; Professor: Bellaachia; Class: Software Paradigms; Subject: Computer Science; University: George Washington University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-xe4
koofers-user-xe4 🇺🇸

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Structuring the Data
2. Objectives...................................................................................2
3. Built-in Types and Primitive Types............................................2
4. Data Aggregates and Type Constructors....................................3
5. Constructors................................................................................3
6. User-defined Types and Abstract Data Types............................7
A. Bellaachia Page: 1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Structuring the Data - Software Paradigms | CSCI 169 and more Study notes Computer Science in PDF only on Docsity!

Structuring the Data

    1. Objectives...................................................................................
    1. Built-in Types and Primitive Types............................................
    1. Data Aggregates and Type Constructors....................................
    1. Constructors................................................................................
    1. User-defined Types and Abstract Data Types............................

2. 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?

3. 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

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

6. 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.

 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: