260 likes | 585 Views
Ch1. Object Oriented Programming. This chapter will introduce you to the basic concepts of object-oriented programming (OOP), including an overview of OOP development methods. background and supplementary material . Object-Oriented and Procedural Programming. Procedural Programming
E N D
Ch1. Object Oriented Programming • This chapter will introduce you to the basic concepts of object-oriented programming (OOP), including an overview of OOP development methods. • background and supplementary material
Object-Oriented and Procedural Programming • Procedural Programming • C, top-down design(functional decomposition) • Consists of module such as procedure, or function • Imperative language(=procedural language) • Drawback : • many problems in software maintenance • Cascading changes • Figure 1.1 • Object-Oriented Programming • C++, object-oriented design • Consists of class module
Class • A class is the implementation of an abstract data type (ADT). It defines attributes and methods which implement the data structure and operations of the ADT, respectively. • In Object-oriented design, a Class is a collection of objects. • In Object-oriented language such as C++, a Class is a data type
The Human Class • Class Human is a collection of object,such as you, me, and Mary • Human share the properties of being featherless, bipedal, risible • class can have data member(member variable, field) • class can have method( function member, procedure,operation) • Example Class Human{ // member int feetCount; // method eat(); laugh(); work(); tango(); etc… }
Object • An object is an instance of a class. It can be uniquely identified by its name and it defines a state which is represented by the values of its attributes at a particular time. • The state of the object changes according to the methods which are applied to it. We refer to these possible sequence of state changes as the behaviour of the object: • The behaviour of an object is defined by the set of methods which can be applied on it.
Object Oriented Programming languages • Abstraction • Encapsulation • Inheritance • Polymorphism
The Progress of Abstraction • Assembly • A Small Abstraction of the underlying machine • Imperative Languages( Fotran,Basic, C) • still requires you to think in terms of the structure of the computer rather than the structure of the problem • Need to establish the association between the “solution space” and “problem space” • APL, Lisp, Prolog • OOPL • We refer to the elements in the problem space and their representations in the solution space as “objects.” • Object has a state, and it has operations that you can ask it to perform. • OOP allows you to describe the problem in terms of the problem, rather than in terms of the computer where the solution will run.
Characteristics of OOP Approaches • Everything is an object. • A program is a bunch of objects telling each other what to do by sending messages. • Each object has its own memory made up of other objects. • Every object has a type. • All objects of a particular type can receive the same messages.
Relationships • Relationships among classes and between objects • A Kind of relationship • Has a (has property) relationship • Is a (is a subclass of) relationship • Instance of or belongs to relationship
A kind of relationship class Circle { attributes: int x, y, radius methods: setX(int newX) getX() setY(int newY) getY() setRadius(newRadius) getRadius() } class Point { attributes: int x, y methods: setX(int newX) getX() setY(int newY) getY() } Figure : Illustration of ``a-kind-of'' relationship.
Is a relationship Since the class Circle is a kind of class Point, an instance of Circle, say acircle, is a point. Consequently, each circle behaves like a point. For example, you can move points in x direction by altering the value of x. Similarly, you move circles in this direction by altering their x value. There is an is a relationship PaintMachine class and a Machine class Figure : Illustration of ``is a'' relationship
Part of relationship class Logo { attributes: Circle circle Triangle triangle methods: set(Point where) } Figure :Ilustration of ``part-of'' relationship
Has a relationship • This relationship is just the inverse version of the part-of relationship. Figure : Illustration of ``has-a'' relationship.
1.2 Classes and Abstract Data types • Information Hiding • A design practice in which implementation details for both data structures and algorithms within a module or subroutine are hidden from routines using that module or subroutine. • Class provides a consistent interface(=a set of operation) • Consider a wordprocessor • It provide public keyword to expose the class’s interface and private to hide its implementation. • Class is a module that supports information hiding
Encapsulation • In object-oriented programming, the packaging of attributes (properties) and functionality (methods or behaviors) to create an object that is essentially a black box • one whose internal structure remains private and whose services can be accessed by other objects only through messages passed via a clearly defined interface • Encapsulation ensures that the object providing service can prevent other objects from manipulating its data or procedures directly
Abstract Data Type(ADT) • An abstract data type is more generalized than a data type constrained by the properties of the objects it contains • Information hiding and encapsulation relate closely to that of an ADT. • An abstract data type (ADT) is characterized by the following properties: • It exports a type. • It exports a set of operations. This set is called interface. • Operations of the interface are the one and only access mechanism to the type's data structure. • Axioms and preconditions define the application domain of the type.
Abstraction • Given ``real-life'' problems, the process of modeling a concrete model can be treated in a program is a Abstraction • This model have the data which are affected and the operations which are identified by the problem.
Abstract Data Type • The data structure can only be accessed with defined operations. This set of operations is called interface and is exported by the entity. An entity with the properties just described is called an abstract data type (ADT). • The Class provides direct support for ADT via Information hiding and encapsulation.
Examples of ADT • Stack • integer
The Client/Server Model and Message passing • OOP is based on a client/server model • Example String class and object are server of string objects that provides for string processing A C++ program or code segment that uses the string class is a client String s1 = “the Day the music died”; Int n = s1.length(); • Good servers practice information hiding so that clients find them easy to use
Inheritance and polymorphism • Inheritance • The transfer of the characteristics of a class in object-oriented programming to other classes derived from it. • From windows class, derive a child menuwindow class • Given the classes Car, Motorcycle, and Truck, we could combine their shared features in a common parent class,Vehicle • Inheritance supports a form of code reuse • Figure 1.4.1 An inheritance hierarchy • C++ support single , multiple inheritance
Polymorphism • the ability to redefine a routine in a derived class (a class that inherited its data structures and routines from another class) • Polymorphism allows the programmer to define a base class that includes routines that perform standard operations on groups of related objects, without regard to the exact type of each object. • Overloading, dynamic binding is a way to implement polymorphism
Polymorphism and Recursion • Example1.4.1
Interfaces and Components • A well designed class’s public part consists of high-level methods and its private part consists of low-level methods and data members in support of these high-level methods • Interface • Interface is a set of operation, • Class’s interface consists of what it exposes and that its implementation consists of what it hides • C++ has a abstract base class for the specification of interface, but Java have interface as a reserved word