+ All Categories
Home > Documents > 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the...

1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the...

Date post: 16-Jan-2016
Category:
Upload: joy-king
View: 213 times
Download: 0 times
Share this document with a friend
49
1 Basic Concepts of Object-Oriented Design
Transcript
Page 1: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

1

Basic Concepts of

Object-Oriented Design

Page 2: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

2

What is this Object ?What is this Object ?

There is no real answer to the question, but we’ll call it a “thinking cap”.

The plan is to describe a thinking cap by telling you what actions can be done to it.

Page 3: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

3

Using the Object’s SlotsUsing the Object’s Slots

You may put a piece of paper in each of the two slots (green and red), with a sentence written on each.

You may push the green button and the thinking cap will speak the sentence from the green slot’s paper.

And same for the red button.

Page 4: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

4

ExampleExample

Page 5: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

5

ExampleExample

That test was a breeze !

Page 6: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

6

ExampleExample

I shouldstudy harder !

Page 7: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

7

Thinking Cap ImplementationThinking Cap Implementation

We can implement the thinking cap using a data type called a class.

class ThinkingCap {

. . .

};

Page 8: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

8

Thinking Cap ImplementationThinking Cap Implementation

The class will have two components called green_string and red_string. These compnents are strings which hold the information that is placed in the two slots.

Using a class permits two new features . . .

class ThinkingCap { . . . char green_string[50]; char red_string[50]; };

Page 9: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

9

Thinking Cap ImplementationThinking Cap Implementation

The two components will be private member variables. This ensures that nobody can directly access this information. The only access is through functions that we provide for the class.

class ThinkingCap { . . .private: char green_string[50]; char red_string[50];};

Page 10: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

10

Thinking Cap ImplementationThinking Cap Implementation

In a class, the functions which manipulate the class are also listed.

class ThinkingCap {public: . . .private: char green_string[50]; char red_string[50];};Prototypes for the Prototypes for the

thinking cap thinking cap functions go here,functions go here,after the word after the word public:public:

Page 11: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

11

Thinking Cap ImplementationThinking Cap Implementation

In a class, the functions which manipulate the class are also listed.

class ThinkingCap {public: . . .private: char green_string[50]; char red_string[50];};Prototypes for the Prototypes for the

thinking cap thinking cap member functions member functions go herego here

Page 12: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

12

Thinking Cap ImplementationThinking Cap Implementation

class ThinkingCap {public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const;private: char green_string[50]; char red_string[50];};

Our thinking cap has at least three member functions:Our thinking cap has at least three member functions:

Functio

n bodie

s

will b

e el

sewher

e.

Page 13: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

13

Thinking Cap ImplementationThinking Cap Implementation

class ThinkingCap {public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const;private: char green_string[50]; char red_string[50];};

The keyword The keyword constconst appears after two prototypes:appears after two prototypes:

This means that thesefunctions will not changethe data stored in aThinkingCap.

Page 14: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

14

Files for the Thinking CapFiles for the Thinking Cap

The ThinkingCap class definition, which we have just seen, is placed with documentation in a file called thinker.h, outlined here.

The implementations of the three member functions will be placed in a separate file called thinker.cxx, which we will examine in a few minutes.

Documentation

Class definition:• ThinkingCap class definition which we have already seen

Page 15: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

15

Using the Thinking CapUsing the Thinking Cap

A program that wants to use the thinking cap must include the thinker header file (along with its other header inclusions).

#include <iostream.h>#include <stdlib.h>#include "thinker.h"

...

Page 16: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

16

Using the Thinking CapUsing the Thinking Cap

Just for fun, the example program will declare two ThinkingCap variables named student and fan.

#include <iostream.h>#include <stdlib.h>#include "thinker.h"

int main( ){ ThinkingCap student: ThinkingCap fan;

Page 17: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

17

Using the Thinking CapUsing the Thinking Cap

Just for fun, the example program will declare two ThinkingCap objects named student and fan.

#include <iostream.h>#include <stdlib.h>#include "thinker.h"

int main( ) { ThinkingCap student; ThinkingCap fan;

Page 18: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

18

Using the Thinking CapUsing the Thinking Cap

The program starts by calling the slots member function for student.

#include <iostream.h>#include <stdlib.h>#include "thinker.h"

int main( ) { ThinkingCap student; ThinkingCap fan;

student.slots( "Hello", "Goodbye");

Page 19: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

19

Using the Thinking CapUsing the Thinking Cap

The program starts by activating the slots member function for student.

#include <iostream.h>#include <stdlib.h>#include "thinker.h"

int main( ) { ThinkingCap student: ThinkingCap fan;

student.slots( "Hello", "Goodbye");

Page 20: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

20

Using the Thinking CapUsing the Thinking Cap

The member function activation consists of four parts, starting with the object name.

int main( ) { ThinkingCap student; ThinkingCap fan;

student.slots( "Hello", "Goodbye");

Name of the object

Name of the object

Page 21: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

21

Using the Thinking CapUsing the Thinking Cap

The instance name is followed by a period.

int main( ) { ThinkingCap student; ThinkingCap fan;

student.slots( "Hello", "Goodbye");A

Per

iod

A P

erio

d

Page 22: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

22

Using the Thinking CapUsing the Thinking Cap

After the period is the name of the member function that you are activating.

int main( ) { ThinkingCap student; ThinkingCap fan;

student.slots( "Hello", "Goodbye");

Name of the Function

Name of the Function

Page 23: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

23

Using the Thinking CapUsing the Thinking Cap

Finally, the arguments for the member function. In this example the first argument (new_green) is "Hello" and the second argument (new_red) is "Goodbye".

#include "thinker.h"

int main( ) { ThinkingCap student; ThinkingCap fan;

student.slots( "Hello", "Goodbye");

Arg

umen

ts

Arg

umen

ts

Page 24: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

24

A QuizA Quiz

How would you activate student's push_green member function ?

What would be the output of student's push_green member function at this point in the program ?

int main( ) { ThinkingCap student; ThinkingCap fan;

student.slots( "Hello", "Goodbye");

Page 25: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

25

A QuizA Quiz

Notice that the push_green member function has no arguments.

At this point, activating student.push_green will print the stringHello.

int main( ) { ThinkingCap student; ThinkingCap fan;

student.slots( "Hello", "Goodbye");

student.push_green( );

Page 26: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

26

A QuizA Quiz

Trace through this program, and tell me the complete output.

int main( ) { ThinkingCap student; ThinkingCap fan;

student.slots( "Hello", "Goodbye");

fan.slots( "Go Cougars!", "Boo!");

student.push_green( );

fan.push_green( );

student.push_red( ); . . .

Page 27: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

27

A QuizA Quiz

Hello

Go Cougars!

Goodbyeint main( ) { ThinkingCap student; ThinkingCap fan;

student.slots( "Hello", "Goodbye");

fan.slots( "Go Cougars!", "Boo!");

student.push_green( );

fan.push_green( );

student.push_red( ); . . .

Page 28: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

28

The Key Software Trend: Object Technology Objects

Reusable software components that model real world items

Meaningful software units– Date objects, time objects, paycheck objects,

invoice objects, audio objects, video objects, file objects, record objects, etc.

– Any noun can be represented as an object More understandable, better organized and

easier to maintain than procedural programming

Favor modularity

Page 29: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

Information Hiding

Hiding the details of a function or data structure with the goal of controlling access to the details of a module or structure.

PURPOSE: To prevent high-level designs from depending on low-level design details that may be changed.

Page 30: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

Two Approaches to Building Manageable Modules

Divides the problem into more easily handled subtasks, until the functional modules (subproblems) can be coded.

Identifies various objects composed of data and operations, that can be used together to solve the problem.

FUNCTIONALDECOMPOSITION

OBJECT-ORIENTED DESIGN

FOCUS ON: processes FOCUS ON: data objects

Page 31: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

FindWeighted Average

PrintWeighted Average

Functional Design Modules

Main

Print Data

Print Heading

Get DataPrepare File for Reading

Page 32: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

Object-Oriented Design

A technique for developing a program in which the solution is expressed in terms of objects -- self- contained entities composed of data and operations on that data.

Private data

<<

setf...

Private data

>>

get...

ignore

cin cout

Page 33: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

33

OCD: Object-Centered Design

1. Identify the objects in the problem's specification and their types.

2. Identify the operations needed to solve the problem.

3. Arrange the operations in a sequence of steps, called an algorithm, which, when applied to the objects, will solve the problem.

Page 34: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

More about OOD

Languages supporting OOD include: C++, Java, Smalltalk, Eiffel, and Object-Pascal.

A class is a programmer-defined data type and objects are variables of that type.

In C++, cin is an object of a data type (class) named istream, and cout is an object of a class ostream. Header files iostream.h and fstream.h contain definitions of stream classes.

Page 35: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

Procedural vs. Object-Oriented Code

“Read the specification of the software you want to build. Underline the verbs if you are after procedural code, the nouns if you aim for an object-oriented program.”

Brady Gooch, “What is and Isn’t Object Oriented Design,” 1989.

Page 36: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

36

Data Abstraction

Separation of a data type’s logical properties from its implementation.

LOGICAL PROPERTIES IMPLEMENTATION

What are the possible values? How can this be done in C++?

What operations will be needed? How can data types be used?

Page 37: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

37

APPLICATION

0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1

REPRESENTATION

Data Encapsulation

is the separation of the representation of data from the applications that use the data at a logical level; a programming language feature that enforces information hiding.

int y;

y = 25;

Page 38: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

38

Encapsulated C++ Data Type int

Value range: INT_MIN . . INT_MAX

Operations: + prefix - prefix + infix - infix * infix / infix % infixRelational Operators infix

TYPE int

(inside)

Representation of

int

as 16 bits two’s complement

+

Implementation of Operations

Page 39: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

39

Abstract Data Type (ADT)

A data type whose properties (domain and operations) are specified independently of any particular implementation.

Page 40: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

40

Application (or user) level: modeling real-life data in a specific context.

Logical (or ADT) level: abstract view of the domain and operations. WHAT

Implementation level: specific representation of the structure to hold the data items, and the coding for operations. HOW

Data from 3 different levels

Page 41: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

41

Viewing a library from 3 different levels

Application (or user) level: Library of Congress, or Baltimore County Public Library.

Logical (or ADT) level: domain is a collection of books; operations include: check book out, check book in, pay fine, reserve a book.

Implementation level: representation of the structure to hold the “books”, and the coding for operations.

Page 42: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

42

Composite Data Type

A composite data type is a type which

stores a collection of individual data components under one variable name,

and allows the individual data components to be accessed.

Page 43: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

4 Basic Kinds of ADT Operations

Constructor -- creates a new instance (object) of an ADT.

Transformer -- changes the state of one or more of the data values of an instance.

Observer -- allows us to observe the state of one or more of the data values without changing them.

Iterator -- allows us to process all the components in a data structure sequentially.

43

Page 44: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

Two Forms of Composite Data Types

Components are not organized with respect to one another.

The organization determines method used to access individual data components.

UNSTRUCTURED STRUCTURED

EXAMPLES: EXAMPLES: arraysclasses and structs

44

Page 45: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

45

C++ Built-In Data TypesC++ Built-In Data Types

Composite

array struct union class

Address

pointer reference

Simple

Integral Floating

char short int long enum

float double long double

Page 46: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

46

Records

A record is a composite data type made up of a finite collection of not necessarily homogeneous elements called members or fields. For example . . .

.year 1999

.maker ‘h’ ‘o’ ‘n’ ‘d’ ‘a’ ‘\0’ . . .

.price 18678.92

thisCar at Base Address 6000

Page 47: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

47

struct CarTypestruct CarType{

int year ; char maker[10];

float price ;

} ;

CarType thisCar; //CarType variables

CarType myCar;

Page 48: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

48

Accessing struct members

The member selection operator (period . ) is used between the variable name and the member identifier to access individual members of a record (struct or class) type variable.

EXAMPLES

myCar.year

thisCar.maker[4]

Page 49: 1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

49

Valid struct operations

Operations valid on an entire struct type variable:

assignment to another struct variable of same type,

pass as a parameter to a function

(either by value or by reference),

return as the value of a function.


Recommended