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

Slides:



Advertisements
Similar presentations
Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Advertisements

Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
1 OBJECT-ORIENTED CONCEPTS. 2 What is an object?  An object is a software entity that mirrors the real world in some way.  A software object in OOP.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Chapter 12: Support for Object-Oriented Programming
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
More about classes and objects Classes in Visual Basic.NET.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
1 Object-Oriented Programming  Object: external or problem-oriented view: an object can represent any entity in the solution of a problem; objects interact.
Object-oriented Programming Concepts
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ fundamentals.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Object Oriented Software Development
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
An Object-Oriented Approach to Programming Logic and Design
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Abstraction ADTs, Information Hiding and Encapsulation.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
9-Dec Dec-15  INTRODUCTION.  FEATURES OF OOP.  ORGANIZATION OF DATA & FUNCTION IN OOP.  OOP’S DESIGN.
Salman Marvasti Sharif University of Technology Winter 2015.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
S.Ducasse Stéphane Ducasse 1 Essential OO Concepts Stéphane Ducasse.
ISBN Object-Oriented Programming Chapter Chapter
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CMSC 345 Fall 2000 OO Design. Characteristics of OOD Objects are abstractions of real-world or system entities and manage themselves Objects are independent.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Classes, Interfaces and Packages
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Introduction to Object-oriented Programming
Principles of programming languages 10: Object oriented languages
Object-Oriented Programming Concepts
Classes (Part 1) Lecture 3
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
One class is an extension of another.
One class is an extension of another.
Object-Oriented Programming: Inheritance and Polymorphism
Object-Oriented PHP (1)
Final and Abstract Classes
Presentation transcript:

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; };