+ All Categories
Home > Documents > Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming...

Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming...

Date post: 18-Jan-2016
Category:
Upload: judith-carroll
View: 232 times
Download: 0 times
Share this document with a friend
Popular Tags:
39
Chapter 6 Groupings of Data and Operations
Transcript
Page 1: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Chapter 6 Groupings of Data and Operations

Page 2: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Constructs for Program Structuring

The programming with

• Procedures

• Modules

• Classes

Page 3: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Procedures

Raising the level of a Computation

Program design with Procedures

• Behavior

• Implementation

Page 4: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

– Behavior

• In general the behavior of a construct is what is

observable from outside the construct

• Define procedure corresponding to what an

algorithm does

– Implementation• The part that is accessible only from within that

construct

• Hide the details of an algorithm in the

procedure body

Page 5: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Example The evaluation of expression

(512 - 487) * 2;

It can be partitioned into ScanningScanning or Lexical analysis groups

individual characters into tokens

ParsingParsing or Syntax analysis uses the

syntactic structure of the expression to

determine its value.

Page 6: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

CHARACTERSTREAM

(512 - 487) * 2;

TOKENSTREAM

EXPRESSIONVALUE

Scanner

Parser

50

lparen number512 minus number487 rparen times number2 semicolon

Page 7: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Modules A module is a collection of declarations,

including both variables and procedures A module serves as a black box with

which the rest of the program interacts through an interface

Program design with Module• Role

• Interface

• Implementation

Page 8: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

– Role• Modules are usually organized around

data.• If an operation affects some data, then

the operation and the data may belong together in a module

– Interface• A subset of the declaration in the

module

• Contain types, variables, and procedures

• The procedures in the interface determine the behavior of the module

Page 9: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

– Implementation• Consist of everything else about the

module, including the code for procedures and for initialization

• Hide design decision in the private part of a module

Page 10: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Figure 6.2 Public and private view of two modules

Page 11: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Classes : User-defined data type

Design types corresponding to what the

program manipulates

A term class is an abbreviation of “class of

objects”

An object is a run-time entity with data on

which operations can be performed

Page 12: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Comparison of Procedures, Modules and Classes Serve distinct needs can be used in combination with each other procedures - needed to implement

operations in a module or class module - used to statically partition the

source text of program with classes differences in not in activity (the

procedures)but in the organization

Page 13: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Procedures

Define all the details all the representation is known

throughout the program entries are represented as a pointer to a

record containing the data

Page 14: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Modules

Hide the representation provide public operations advantage over procedures is that the

representation is hidden and access is checked

hiding and checking contribute to feel of entries as object rather than pointers

Page 15: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Defined Types

As with modules, the representation is hidden, access is checked

in addition, objects are initialized upon creation

Page 16: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Program Organization Procedures at

the same level– a sequence of

useful procedures

– procedures can call each other

Figure 6.6

Page 17: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Nested procedures– An attempt to manage

programs with many procedures

– The idea is to declare a procedure inside another

– To declare functions close to where they are used

Figure 6.7

Page 18: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Modules– a collection of data

and related actions– a grouping of

declarations, which can include types, variables, and procedures

Figure 6.8

Page 19: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

CHAPTER 6: PART II

6.5 CLASS DECLARATIONS IN C++

6.6 DYANAMIC ALLOCATION IN C++

6.7 TEMPLATES: PARAMETERIZED TYPES

6.8 IMPLEMENTATION OF OBJECTS IN C++

Page 20: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

6.5 CLASS DECLARATIONS IN C++

C++: User-defined types receive the same support as built-in types.

Classes = generalization of records called structures (grouping of data).

Both data and functions can be structure members.

Variables can be declared and objects created in C++ classes.

Page 21: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

STRUCTURE OR CLASS

Structure vs. Class = Public vs. Private All members of a structure are public. All members of a class are private. Take note: These are by default!!!! A structure is a special case of a class of the

same name.

Page 22: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

DECLARATION

Keyword: struct Declarations enclosed in braces {}. Declarations in braces are members.

Page 23: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

SAMPLE CODE

struct Stack{

int top;

char elements[101];

char pop( );

void push (char);

Stack ( );

};

2 variables – top- data member that

holds an integer– elements- data member

that holds an array of characters

3 functions– pop- takes no parameter

and returns a character– push- takes character

and returns nothing– Stack- constructor

Page 24: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Constructors & Destructors

Stack ( );

…………………….. ~Stack ( );

Constructors and destructors are parameterless functions.

Same name as class.

Called automatically:– constructors initialize– destructors clean up

Page 25: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

MEMBER NAMES AND FUNCTION CODE

struct Stack{

int top;

char elements[101];

char pop( );

void push (char);

Stack ( ) { top = 0; }

};

char Stack::pop ( );

top = top - 1;

return elements[top+1];

}

void Stack::push (char c) {top = top + 1;

elements[top] = c;

}

Full member name: <class-name>::<member name>

– full names needed to make explicit that belong to class if used outside of class declaration

If class name is known from context, use only the member name.

Page 26: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

CLASS NAMES AS DEFINED TYPES

#include <stdio.h>

main( ) {

Stack s;

s.push(‘!’); s.push(‘@’); s.push(‘#”);

printf(“%c %c %c\n”, s.pop( ), s.pop( ), s.pop( ));

}

After declared, a class name can be used to declare variables.

Dot notation refers to members of an object.

C struct Complex x; C++ Complex x;

Page 27: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

INITIALIZATION WITH PARAMETERS

struct Complex {

float re;

float im;

Complex(float r, i) {re =r; im = i; }

};

………………………..

Complex x(1, 2)

Initialization of constructor may require passing of parameters.

Here declaration takes 2 parameters.

Declares and initializes complex # x to the parameters in ( ).

Page 28: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

OVERLOADED FUNCTION NAMES

struct Complex {

float re;

float im;

Complex(float r)

{re = r; im = 0; }

Complex(float r, i) {re =r; im = i; }

};

Functions can be overloaded (including constructors).

Must differ by number and/or type of parameters in call.

Page 29: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

ACCESSIBITLITY: PUBLIC, PRIVATE, PROTECTED

Privacy and access class based. Access to members restricted through keywords (apply

to outside class: in class have access to all objects in same class).

Public members are accessible to outside code. Private members are accessible to member functions in

the class declaration. Accessible to all objects of class. Protected members like private members except for

derived classes, visible through inheritance to derived classes.

Page 30: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

PUBLIC vs. PRIVATE

Class Stack {

public:

Stack ( );

char pop( );

void push(char);

private:

int top;

char elements[101];

};

Here the member variables are hidden (private).

By default:– struct = public– class = private

Page 31: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

6.6 DYNAMIC ALLOCATION IN C++

Three ways to create objects in C++:– variable declarations– dynamically through new– as static objects

Page 32: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

NEW, DELETE

new T;

delete p;

Objects created by new exist until destroyed by delete.

New creates object of type T and returns pointer.

Delete destroys object pointed to.

Page 33: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

POINTERS TO OBJECTS

Prefix * is a pointer-deferencing operator. Cell * p; reads as “we get an object of type

cell when we apply * to p.” Summary of pointer notations:

– p->info = member info of object pointed to by p.

– 0 = null pointer, pointing to no object.– this = used within member functions to

point to this object itself.

Page 34: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

DYNAMIC ALLOCATION USING CONSTRUCTORS AND DESTRUCTORS

Class Stack {

int top;

char * elements;

int size;

public:

void push(char);

char pop( );

Stack (int);

~Stack( );

};

Here the size of the array is a parameter.

Since dynamically allocated storage is taken off the heap it needs a destructor to explicitly deallocate (release) it.

Page 35: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

CELLS AND LINKED LISTS

class Cell {

int info;

Cell * next;

Cell(int I) {info = I; next = this;}

Cell(int I, Cell*n) {info = I; next = n;}

friend class List;

};

Friends– friend declaration within a

class gives nonmember functions access to private members of the class

Cell constructors– empty list is a cell that

points to itself– new Cell c(0) or – next = this

Page 36: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

LINKED LISTS

class List {

Cell * rear;

public:

void put(int);

void push(int);

int pop( );

int empty( ) {return rear == rear->next; }

List( ) {rear = new Cell(0);}

~List( ) {while(!empty( )) pop( );}

};

Uses constructors from class Cell (because of friend declaration)

Initializes new list (circularly linked cell)

push adds to front put adds to back pop returns value of

front and deletes cell

Page 37: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

6.7 TEMPLATES: PARAMETERIZED TYPES

Data structures are containers that hold objects.

Operations on a container can be defined for any type.

Type parameters denote the type of element in the container.– Template <class T>

Page 38: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

6.8 IMPLEMENTATION OF OBJECTS IN C++

Objects laid out like records. (class = structure containing data members)

Pointers used to access indirectly. Functions can be expanded in-line to

reduce function call overhead. Class declaration assists with checking

restrictions and in-line expansion at compile time.

Page 39: Chapter 6 Groupings of Data and Operations. Constructs for Program Structuring n The programming with Procedures Modules Classes.

Take you C assignment and translate it directly into C++ using “BIN” objects. See page 246 in PL book for stack objectexample. Due 10/12/2000


Recommended