42
CHAPTER 7 Object Oriented Programming

CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Embed Size (px)

DESCRIPTION

What is an Object? The objects in the program are the entities in the simulation. For example, if the application is a window system, then the relevant objects might be windows, scroll bars, buttons, dialog boxes and so on.

Citation preview

Page 1: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

CHAPTER 7

Object Oriented Programming

Page 2: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Object-oriented program

“Data and Operations go together was independently” is the main idea

The promise of making complex systems easier to structure and manage

A description or simulation of an application.

Page 3: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

What is an Object?

The objects in the program are the entities in the simulation.

For example, if the application is a window system, then the relevant objects might be windows, scroll bars, buttons, dialog boxes and so on.

Page 4: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Example 1. “Airport Departure System”

ในสถานการณทสนามบน ผโดยสารรอในแถว เพอตรวจสอบกอนขนเครองบน ระหวางรอ

ตวแทนของสายการบนจะตรวจสอบตวโดยสาร ของผโดยสารกอน ในปญหานเราตองศกษาเรอง การจดลำาดบคววา ควจะเกดขนอยางไร ผ

โดยสารตองรอนานเทาใด ซงเราสามารถศกษาไดดวยการเขยนโปรแกรมจำาลองสถานการณขนมา

Page 5: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

External and Internal Views of Objects

An Object can represent any entity in the solution of a problem

External

Internal : each object consist its own code and private memory

Airport Departure System• A passenger• A ticket agent• A queue• A ticket

Drawing program• Boxes• ellipses• lines• text strings

Page 6: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Computation

Objects interact by sending messages to each other.

A computation is characterized by the messages sent and received by the objects in a program.

A computation is characterized in terms of the observable behavior of objects, not in terms of the details of how that behavior is implemented.

Page 7: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Properties of Shapes

A diagram built out of shapes

• An ellipse• A line• A box• A line• A ellipse

Page 8: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Properties of Shapes

A shape can be A box or rectangleAn ellipseA line

Classification base on its properties.All shape types have same properties :

Width, Height and Position

Page 9: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Properties of Shapes

the Shape Example, a diagram consists of a list of shapes and the shapes on the list align themselves next to each other from left to right.

A diagram is drawn by drawing each shape in its listWhen a diagram receives a draw message it responds by asking each shape on its list to draw itself next to the previous one. Next, it sends a draw message to each shape on its list.

Page 10: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Vocabulary of OOP

ObjectA collection of data and operation Class A description of a set of objects Subclass A subset of a class, with additional

properties Instance A technical term for an object of a class Method A procedure body implementing an

operation Message A procedure call; request to execute a

method

Page 11: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Object-Oriented Thinking

Grouping Objects into Class HierarchiesGrouping Similar Objects into a Class

Similar objects, objects with common properties, are grouped into a class. A class can be thought of as the type of an object. example class of shape objects, class of ellipse, class of box and so on.

Page 12: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

A Class Hierarchy

Shape are classified in follow figure

The nesting emphasizes that each object of a nested class

Shape

Box Ellipse Line

circle

Page 13: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

A Class Hierarchy

Box is also an object of the enclosing class Shape.

Thus, all boxes are shapes, but not all shapes are boxes. The set of all boxes is a subset of the set of all shapes.

A nested class is said to be a subclass; The converse of subclass is superclass. Thus, Box is a subclass of Shape, and Shape is a superclass of Box.

Page 14: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

A Class Hierarchy

The nested classification of nested class to be the class hierarchy

shape

Box Ellipse Line

circle

Page 15: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

A Class Hierarchy

Class Shape has four subclasses, Box, Ellipse, Line, and Text.

Class Ellipse has one subclass, Circle

A subclass S of a class C is depicted as a child of C in the hierarchy

Page 16: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Inheritance

Single inheritance, where a subclass has just one superclass.

Multiple inheritance, a subclass can have more than one superclass.

For example, a text string has a position within a diagram, a property it shares with shapes, and a font a property it might share with another class of objects.

Page 17: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Message & State

Objects Respond to Messages, Objects Have State.

Class กำาหนดคณสมบตและพฤตกรรมของ วตถ ซงประกอบดวย

Method ทำางานเมอวตถไดรบ messageVariable เกบคาหรอสถานะตาง ๆ ของวตถ คาน

อาจถกเปลยนดวย method เมอเราจองพนทหนวยความจำาใหกบตวแปร เราหมาย ถงเกดการสราง instance ของคลาส ซง instance ก

คอ object นนเอง

Page 18: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Class Definition

Class Class DiagramDiagramMethods initialize, add, setangle, drawVariables shapelist, angle

Class Class DiagramDiagram: method setangle(a); angle := a;

Class Class DiagramDiagram: method initialize; shapelist := a new empty list of shapes; angle := 0;

Class Class DiagramDiagram: method add(shape s); send message setalign(angle) to s; add s to the end of shapelist;

Class Class DiagramDiagram: method draw; previous := a new shape; for each shape s on shapelist do previous := the result of sending draw(previous) to s;

Page 19: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Angle in diagram

0o 90o

180o 270o

Page 20: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Inheritance

The basic idea of inheritance is

The children in a class hierarchy inherit the methods and variables of their ancestors.

Page 21: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Inherit Meaning

All the methods and variables from the ancestor classes became of the child.

Any new methods or variables at a child in hierarchy are simply added to.

Page 22: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Inheritance

Class Class ShapeShapeMethods initialize, draw, offset, setwidth, setheight, setalignVariables width, height, align

Class Class BoxBoxMethods draw, offset

Class Class EllipseEllipseMethods draw, offset

Class Class LineLineMethods initialize, drawVariables shadow

Class Class CircleCircleMethods initialize

Class Class BraceBraceMethods initialize drawVariables adiagram

Page 23: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Meaning of Message

An object determines how it implements a message.

A shape determines what it does in response to a draw message. All of classes have their own methods to be executed in response to a draw message. Thus an object can each respond in its own way to a draw message. The decisions and implementations are localized and isolated so that they can be changed without touching the existing shapes.

Page 24: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Meaning of Message

Implementation of difference two classes

Class Text: method draw(previous) returns Shape; center String on previous; return previous;

Class Ellipse: method draw(previous) returns Shape; center := center of this ellipse relation to previous; lay out an ellipse centered at center; return this ellipse object;

Page 25: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Procedure-oriented approach

A procedure to draw a diagram might look like the following pseudocode:Procedure draw(diagram d);Begin for each shape s in diagram d do begin case s of BOX: code to draw a box; ELLIPSE: code to draw an ellipse; . . .

If a new shape is added, then code for handling the new shape has to be added to each procedure. It is spread across procedures, each of which must be studied before the new code is added.

Page 26: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Information Hiding for Extensibility

In formation hiding facilitates two kinds of changes: Implementation changes. If all interactions

with an object are through its interface, then the algorithms and data structures hidden behind the interface can be changed.

Inheritance change. If all interactions are through the interface to a superclass, then the program can be extended by adding subclasses.

Page 27: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Information Hiding for Extensibility

If the ellipse interacts with a shaded shape.

Specifically, the problem is to compute the center E of the ellipse, relative to the center B, given that line BE is at an angle align.

EPB

EPB

Page 28: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Information Hiding for Extensibility

Since information about box is hidden from the ellipse, it sends a message offset(align) to the box, asking for the point P on the border of the box at which the ellipse will attach itself.

P := result of sending offset(align) to the box;The ellipse computes E from P and other ellipse information;

Page 29: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Adding a Subclass

This diagram is treelike; it is not simply a linear list.

source targetcompiler

messages

Page 30: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Adding a Subclass

The solution is to add a new subclass Brace of Shape that allows a diagram to appear as a shape.

class Bracemethods initialize drawvariables adiagram

Page 31: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Objects and Classes

An object responds to a message by executing a method. A message corresponds to a procedure call, a method to a procedure body.

An object has its own internal state. A class specifies the properties of its objects,

methods and variables A subclass or derived class is an extension of

a class. An object of a subclass uses its own method

to implement a message

Page 32: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

OOP IN C++

A class is a grouping of members, where members can be variables or functions.

A constructor is a member function with the same name as the class; it is clled automatically when the lifetime of an object of the class begins.

A prefix ~ and the class name, the destructor is called automatically before the lifetime of an object ends

Page 33: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Review in C++

class Cell { Cell* next;public: int info; Cell(int); ~Cell(); Cell* step() {return next;} Cell* add(int, Cell*);};

Page 34: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Base and Derived Classes

In C++ terminology, the extension of a base class is called a derived class.

Class Box can be derived from class Shape as follows:

Class Box : public Shape { < added members >}

Class Box inherits all members of class Shape.

Page 35: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Base and Derived Classes

Class B { //declaration of class BPublic: int x; // the full name is B::x char f(); // public member function B(); // constructor};

Class D : public B { // D derived from B int x; // D::x is added, B::x is inherited int g(); // added member function};

Page 36: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Virtual Functions

Class B { public:virtual char f() { return ‘B’; } char g() { return ‘B’; } char testF() { return f(); } char testG() { return g(); }};

Class D { public:virtual char f() { return ‘D’; } char g() { return ‘D’; }};

Page 37: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Virtual Functions

main() { D d; print d.testF(), d.testG();};

Virtual function are taken from the derived class where possible, so

and

d.testF() is called B::testF() calls D::f();

d.testG() is called B::testG() calls B::g();

Page 38: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Shapesclass Shape {protected: float width,height;public: float align; Shape(); Shape(float, float);virtual Point* offset(float);virtual Shape* draw(Shape *) {return this;}};

class Ellipse: public Shape {public: Ellipse(); Ellipse(fload, float);virtual Point* offset(float);virtual Shape* draw(Shape *)};

class Text: public Shape { char *str;public: Text(char *);virtual Shape* draw(Shape *)};

Page 39: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Derived classes and information hiding

C++ has three keywords – public, protected, and private – to control access to members.Public members are accessible from outside

code,Protected members are accessible from

derived classes, andPrivate members are accessible only to

member functions of that class.

Page 40: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Public and Private Base Classes

class <derived> : public <base> { <member-declarations>};

class <derived> : private <base> { <member-declarations>};

Members of a public base class retain their accessibility in the derived class

All members inherited by <derived> from <base> become private members of <derived>.

Page 41: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Privacy Principle

Functions in a derived class cannot access the private members of its base

Privacy principle: The private members of a class are accessible only to member functions of the class

Page 42: CHAPTER 7 Object Oriented Programming. Object-oriented program Data and Operations go together was independently is the main idea The promise of making

Accessibility of Inherited Members

class List { cell *rear;public: List(); int empty();Protected: void add(); void push(int); int get();};

class Queue : public List {public: Queue(); int get() {return List::get()}; void put(int x) { add(x);}};

class Stack : private List {public: Stack(); int pop() {return get();} void push(int x) { List::push(x); } List::empty;};