11.11.2014 Views

Introduction to - Department of Computer Science

Introduction to - Department of Computer Science

Introduction to - Department of Computer Science

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Abstract Data Types<br />

Representing information is fundamental <strong>to</strong><br />

computer science. The primary purpose <strong>of</strong> most<br />

computer programs is not <strong>to</strong> perform calculations,<br />

but <strong>to</strong> s<strong>to</strong>re and efficiently retrieve information. For<br />

this reason, the study <strong>of</strong> data structures and the<br />

algorithms that manipulate them is at the heart <strong>of</strong><br />

computer science.<br />

Different Views <strong>of</strong> Data:<br />

-When we talk about the function <strong>of</strong> a program we<br />

use the words “add”, “read”, “calculate”. The<br />

function <strong>of</strong> a program describes what it does in<br />

terms <strong>of</strong> the verbs or actions.<br />

The data are the nouns <strong>of</strong> the programming world:<br />

the objects that are manipulated, the information<br />

that is processed by the computer program.<br />

<strong>Computer</strong>s view all information as bits <strong>of</strong> 0s and 1s,<br />

however as humans we tend <strong>to</strong> think in larger units<br />

such as numbers, lists, etc.. We use what is called<br />

Data abstraction<br />

Data Abstraction:<br />

We use data abstraction, even when we think we are<br />

manipulating concrete entities: For example, our<br />

program might be using integers, however the<br />

physical representation <strong>of</strong> an integer might differ<br />

from one machine <strong>to</strong> the other. The way that<br />

integers are physically represented determines how


the computer manipulates them. However as<br />

programmers, we rarely get involved at this level.<br />

Binary : 10011001<br />

Decimal: 153 (unsigned), -25 (sign and magnitude)<br />

-102 (one’s complement), -103 (two’s<br />

complement)…<br />

Data Encapsulation:<br />

Data encapsulation means that the physical<br />

representation <strong>of</strong> a program’s data is surrounded.<br />

The user <strong>of</strong> the data does not see, nor usually cares<br />

about the implementation, but deals with the data<br />

only in its logical picture- its abstraction.<br />

To make encapsulation effective, operations must<br />

be provided <strong>to</strong> allow the user <strong>to</strong> create, access and<br />

change data. For example for the data type int, C++<br />

provides ways <strong>to</strong> create variables <strong>of</strong> this data type<br />

by using declarations; we can assign values <strong>to</strong> them<br />

using the assignment opera<strong>to</strong>r, we can perform<br />

arithmetic operations using +, - *,….<br />

Abstract Data Type and Data structures:<br />

A type is a collection <strong>of</strong> values. For example the<br />

Boolean type consists <strong>of</strong> the values true and false.<br />

The integers also form a type.<br />

An integer is a simple type because its values<br />

contain no subparts.<br />

A bank account record will typically contain several<br />

pieces <strong>of</strong> information such as name, address,


account number, and an account balance. Such a<br />

record is an example <strong>of</strong> an aggregate type or<br />

composite type.<br />

A data type is a type <strong>to</strong>gether with a collection <strong>of</strong><br />

operations <strong>to</strong> manipulate the type.<br />

An abstract data type (ADT)<br />

A data type whose properties are specified<br />

independently <strong>of</strong> any particular implementation. An<br />

abstract data type describes the logical properties <strong>of</strong><br />

the data type. The interface <strong>of</strong> the ADT is defined in<br />

terms <strong>of</strong> a type and a set <strong>of</strong> operations on that type.<br />

The behavior <strong>of</strong> each operation is determined by its<br />

inputs and outputs. An ADT does not specify how<br />

the data type is implemented, these details are<br />

hidden from the user <strong>of</strong> the ADT and protected<br />

from outside access by encapsulation..<br />

Data encapsulation:<br />

The separation <strong>of</strong> the representation <strong>of</strong> data from<br />

the applications that use the data at a logical level.<br />

A data structure is the concrete implementation <strong>of</strong><br />

an ADT.<br />

Data structures are usually aggregate types; they<br />

can be decomposed in<strong>to</strong> their component elements.<br />

The arrangement <strong>of</strong> these components is a feature <strong>of</strong><br />

the structure that effects how each element can be<br />

accessed.<br />

Both the arrangement <strong>of</strong> the elements and the way<br />

they are accessed can be encapsulated.


Example: a library<br />

A library can be decomposed in<strong>to</strong> its component<br />

elements : books.<br />

The collection <strong>of</strong> individual books can be arranged<br />

in a number <strong>of</strong> ways. However the user need not<br />

know the system in order <strong>to</strong> locate a book, because<br />

the librarian or the direc<strong>to</strong>ry can point him/her <strong>to</strong><br />

the book they want.<br />

We use the same approach <strong>to</strong> data structures: A data<br />

structure is defined by 1) the logical arrangement <strong>of</strong><br />

data elements combined with (2) the set <strong>of</strong><br />

operations we need <strong>to</strong> access the elements.<br />

Abstract Data Types Opera<strong>to</strong>r Categories:<br />

Four basic operations are performed on an abstract<br />

data type: construc<strong>to</strong>rs, transformers, observers, and<br />

itera<strong>to</strong>rs.<br />

A construc<strong>to</strong>r is an operation that creates a new<br />

instance (object) <strong>of</strong> an ADT. It is invoked at the<br />

language level by some sort <strong>of</strong> declaration.<br />

Transformers are operations that change the state <strong>of</strong><br />

one or more <strong>of</strong> the data values, such as inserting an<br />

item in<strong>to</strong> an object, deleting an item from an<br />

object…<br />

An observer is an operation that allows us <strong>to</strong><br />

observe the state <strong>of</strong> one or more <strong>of</strong> the data values


without changing them. These include predicates<br />

checking if a certain property is true, accessor or<br />

selec<strong>to</strong>r functions that return a copy <strong>of</strong> an item …<br />

An itera<strong>to</strong>r is an operation that allow us <strong>to</strong> process<br />

all components in a data structure sequentially.<br />

Composite Data Types<br />

C++ provides three types <strong>of</strong> composite data types: records<br />

(struct), classes and arrays with various dimensions.<br />

Classes and structs can have member functions as well as<br />

data. Classes and structs are logically unstructured.<br />

Records:<br />

In C++, records are implemented by structs. C++ classes<br />

are another implementation <strong>of</strong> a record.<br />

Logical Level: A record is a composite type made up <strong>of</strong> a<br />

finite collection <strong>of</strong> not necessarily homogeneous elements<br />

called members or fields. Accessing is done directly<br />

through a set <strong>of</strong> named member or field selec<strong>to</strong>rs.<br />

struct CarType<br />

{ int year;<br />

char maker[10];<br />

float price;<br />

};<br />

CarType myCar;


The record variable myCar is made up <strong>of</strong> 3 components.<br />

The first, year is <strong>of</strong> type int. The second, maker, is an<br />

array <strong>of</strong> characters. The third, price, is a float number.<br />

The name <strong>of</strong> the components make up the set <strong>of</strong> member<br />

selec<strong>to</strong>rs.<br />

The syntax <strong>of</strong> the component selec<strong>to</strong>r is the record<br />

variable name, followed by a period, followed by member<br />

selec<strong>to</strong>r. myCar.price<br />

Higher-Level Abstraction and the C++ Class<br />

Type<br />

When we design an ADT, we want <strong>to</strong> bind the operations<br />

<strong>of</strong> the data type with the data that are being manipulated.<br />

The Class Type is the perfect mechanism for doing that.<br />

The class type is a construct in which the members <strong>of</strong> the<br />

class can be both functions and data. A class encapsulates<br />

a fixed number <strong>of</strong> data components with the functions<br />

that manipulate them; the predefined operations on an<br />

instance <strong>of</strong> a class are whole assignment and component<br />

access.<br />

In an object-oriented language, such as C++, an ADT and<br />

its implementation <strong>to</strong>gether make up a class.<br />

Each operation associated with the ADT is implemented<br />

by a member function.<br />

The variables that define the space required by a data item<br />

are referred <strong>to</strong> as data members.<br />

An object is an instance <strong>of</strong> a class, that is, something that<br />

is created and takes up s<strong>to</strong>rage during the execution <strong>of</strong> a<br />

computer program.


Classes are written in two parts, the specification and the<br />

implementation.<br />

The specifications include data and operations on the<br />

data.<br />

The implementation section fully defines the operations<br />

and provides the manner in which these data can be<br />

manipulated.<br />

Class Specification:<br />

Although the class specification and implementation<br />

could reside in the same file, the two parts <strong>of</strong> a class are<br />

usually separated in<strong>to</strong> two files.<br />

The specification goes in<strong>to</strong> a header file(.h extension) and<br />

the implementation goes in<strong>to</strong> a file with the same name<br />

but with a .cpp extension.<br />

//Declares a class <strong>to</strong> represent the Date ADT.<br />

//This is file DateType.h<br />

class DateType<br />

{<br />

public:<br />

void Initialize(int newMonth, int newDay, int<br />

newYear);<br />

int GetYear() const;<br />

int GetMonth() const;<br />

int GetDay() const;<br />

private:<br />

int year;<br />

int month;


};<br />

int day;<br />

The member functions <strong>of</strong> the class are Initialize, GetYear,<br />

GetMonth, GetDay. They are marked public, which<br />

means that outside code (client) can access these<br />

functions.<br />

Initialize is a construc<strong>to</strong>r operation: it takes values for the<br />

year, month and day and s<strong>to</strong>res these values in<strong>to</strong> the<br />

appropriate data members <strong>of</strong> an instance <strong>of</strong> the class or an<br />

object.<br />

GetYear, GetMonth and GetDay are accessor functions,<br />

they access data members <strong>of</strong> the class. The const beside<br />

accessor function names guarantees that these functions<br />

do not change any <strong>of</strong> the data members <strong>of</strong> the objects <strong>to</strong><br />

which they apply.<br />

Class Implementation<br />

Only member functions <strong>of</strong> the class DateType can access<br />

the data members. so we must associate the class name<br />

with the function definitions. We do so by inserting the<br />

class name before the function name, separated by the<br />

scope resolution opera<strong>to</strong>r(::)<br />

The implementation <strong>of</strong> the member functions goes in<strong>to</strong><br />

the file DateType.cpp.<br />

To access the specifications, we must insert the file<br />

DateType.h by using an #include directive<br />

//Define member functions <strong>of</strong> class DateType


This is the file DateType.cpp<br />

#include “DateType.h” //Gain access <strong>to</strong> the specification<br />

<strong>of</strong> the class<br />

void DateType::Initialize (int newMonth, int newDay, int<br />

newYear)<br />

{<br />

year= newYear;<br />

month= newMonth;<br />

day= newDay;<br />

}<br />

int DateType::GetMonth() const<br />

{<br />

return month;<br />

}<br />

int DateType::GetDay() const<br />

{<br />

return day;<br />

}<br />

int DateType::GetYear() const<br />

{<br />

return year;<br />

}<br />

A client <strong>of</strong> the class DateType must have an #include<br />

“DateType.h” directive for the specification (header) file<br />

<strong>of</strong> the class.<br />

Note: System-supplied header files are enclosed in anglebrackets,<br />

whereas user defined header files are enclosed<br />

in double quotes.


The client then declares a variable <strong>of</strong> type DateType just<br />

as it would any other variable.<br />

#include <br />

#include “DateType.h”<br />

using namespace std;<br />

DateType <strong>to</strong>day;<br />

DateType anotherDay;<br />

<strong>to</strong>day. Initialize(9, 5, 2007);<br />

anotherDay.Initialize(9, 25, 2007);<br />

cout


RelationType ComparedTo(DateType someDate);<br />

//Compares self with someDate<br />

To determine which date comes first, we must compare<br />

the year members <strong>of</strong> the instance and the parameter. If<br />

they are the same, we must compare the month members.<br />

If the year members and the month members are the<br />

same, we must compare the day members. To access the<br />

fields <strong>of</strong> the instance, we just use their names. To access<br />

the fields <strong>of</strong> the parameter, we prefix the field name with<br />

the parameter name and a dot.<br />

RelationType DateType::ComparedTo(DateType aDate)<br />

{<br />

}<br />

if (year aDate.year)<br />

return GREATER;<br />

else if (month aDate.month)<br />

return GREATER;<br />

else if (day < aDate.day)<br />

return LESS;<br />

else if (day >aDate.day)<br />

return GREATER<br />

else return EQUAL;


In this code, the year refers <strong>to</strong> the year data member <strong>of</strong><br />

the object <strong>to</strong> which the function is applied; aDate.year<br />

refers <strong>to</strong> the data member <strong>of</strong> the object passed as a<br />

parameter.<br />

The object <strong>to</strong> which the a member function is applied is<br />

called self. In the function definition, the data members <strong>of</strong><br />

self are referenced directly without using the dot notation.<br />

switch (<strong>to</strong>day.ComparedTo(anotherDay))<br />

{<br />

case LESS : cout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!