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 rectangle An ellipse A 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 list When 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 ClassA description of a set of objects SubclassA subset of a class, with additional properties InstanceA technical term for an object of a class MethodA procedure body implementing an operation MessageA procedure call; request to execute a method
Object-Oriented Thinking Grouping Objects into Class Hierarchies Grouping 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 BoxEllipseLine 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 BoxEllipseLine 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 ทำงานเมื่อวัตถุได้รับ message Variable เก็บค่าหรือสถานะต่าง ๆ ของวัตถุ ค่านี้ อาจถูกเปลี่ยนด้วย method เมื่อเราจองพื้นที่หน่วยความจำให้กับตัวแปร เรา หมายถึงเกิดการสร้าง instance ของคลาส ซึ่ง instance ก็คือ object นั่นเอง
Class Definition Class Diagram Methods initialize, add, setangle, draw Variables shapelist, angle Class Diagram Class Diagram: method setangle(a); angle := a; Class Diagram Class Diagram: method initialize; shapelist := a new empty list of shapes; angle := 0; Class Diagram Class Diagram: method add(shape s); send message setalign(angle) to s; add s to the end of shapelist; Class Diagram Class Diagram: 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 0o0o 90 o 180 o 270 o
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 Shape Methods initialize, draw, offset, setwidth, setheight, setalign Variables width, height, align Class Box Methods draw, offset Class Ellipse Methods draw, offset Class Line Methods initialize, draw Variables shadow Class Circle Methods initialize Class Brace Methods initialize draw Variables 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. E P B E P B
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. sourcetargetcompiler 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 Brace methods initialize draw variables 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 { } Class Box inherits all members of class Shape.
Base and Derived Classes Class B { //declaration of class B Public: 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();
Shapes class 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, and Private members are accessible only to member functions of that class.
Public and Private Base Classes class : public { }; class : private { }; Members of a public base class retain their accessibility in the derived class All members inherited by from become private members of.
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; };