Upload
rolf-stone
View
228
Download
0
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
CHAPTER 7
Object Oriented Programming
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.
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.
Example 1. “Airport Departure System”
ในสถานการณทสนามบน ผโดยสารรอในแถว เพอตรวจสอบกอนขนเครองบน ระหวางรอ
ตวแทนของสายการบนจะตรวจสอบตวโดยสาร ของผโดยสารกอน ในปญหานเราตองศกษาเรอง การจดลำาดบคววา ควจะเกดขนอยางไร ผ
โดยสารตองรอนานเทาใด ซงเราสามารถศกษาไดดวยการเขยนโปรแกรมจำาลองสถานการณขนมา
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
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.
Properties of Shapes
A diagram built out of shapes
• An ellipse• A line• A box• A line• A ellipse
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
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.
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
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.
A Class Hierarchy
Shape are classified in follow figure
The nesting emphasizes that each object of a nested class
Shape
Box Ellipse Line
circle
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.
A Class Hierarchy
The nested classification of nested class to be the class hierarchy
shape
Box Ellipse Line
circle
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
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.
Message & State
Objects Respond to Messages, Objects Have State.
Class กำาหนดคณสมบตและพฤตกรรมของ วตถ ซงประกอบดวย
Method ทำางานเมอวตถไดรบ messageVariable เกบคาหรอสถานะตาง ๆ ของวตถ คาน
อาจถกเปลยนดวย method เมอเราจองพนทหนวยความจำาใหกบตวแปร เราหมาย ถงเกดการสราง instance ของคลาส ซง instance ก
คอ object นนเอง
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;
Angle in diagram
0o 90o
180o 270o
Inheritance
The basic idea of inheritance is
The children in a class hierarchy inherit the methods and variables of their ancestors.
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.
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
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.
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;
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.
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.
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
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;
Adding a Subclass
This diagram is treelike; it is not simply a linear list.
source targetcompiler
messages
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
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
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
Review in C++
class Cell { Cell* next;public: int info; Cell(int); ~Cell(); Cell* step() {return next;} Cell* add(int, Cell*);};
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.
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};
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’; }};
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();
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 *)};
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.
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>.
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
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;};