20120227170202MTS 3033- Bab1 Introduction to OOP
-
Upload
ainisuzanimislan -
Category
Documents
-
view
26 -
download
0
Transcript of 20120227170202MTS 3033- Bab1 Introduction to OOP
MTS 3033 - OBJECT ORIENTED PROGRAMMING
1
Objectives: Know the concept of OOP Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know four design principles of OOP Know OOP programming languagesMTS 3033 - OBJECT ORIENTED PROGRAMMING
2
Object oriented programming is introduced as a new programming concept which should help one in developing high quality software. It attempts to solve the problem with only one approach by dividing the problems in sub-modules and using different objects Objects of the program interact by sending messages to each other.MTS 3033 - OBJECT ORIENTED PROGRAMMING
3
Programming with objects is quite like working with real world objects. It groups operations and data into modular units called objects. These objects can be combined into structured networks to form a complete program, similar to how the pieces in a puzzle fit together to create a picture.MTS 3033 - OBJECT ORIENTED PROGRAMMING
4
By breaking down complex software projects into small, self-contained and modular units, object orientation ensure that changes to one part of software project will not affect other portions of the software.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
5
STRUCTURED PROGRAMMING:
MAIN PROGRAM
GLOBAL DATA
FUNCTION 1
FUNCTION 2
FUNCTION 3
FUNCTION 4
FUNCTION 5
MTS 3033 - OBJECT ORIENTED PROGRAMMING
6
There are features of structured programming: Emphasis is on doing things (algorithms) Using function Function & program is divided into modules Every module has its own data and function which can be called by other modules. Most of the functions share global data
MTS 3033 - OBJECT ORIENTED PROGRAMMING
7
Data move openly around the system from function to function Functions transform data from one form to another Employs top down approach in program design.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
8
Object 1 MessageData Function
Object 2
Data Function
Message
Object 3
Message
Data Function
MTS 3033 - OBJECT ORIENTED PROGRAMMING
9
There are features of object oriented programming: Emphasis is on data rather than procedure Programs are divided into what are known as objects Data structures are designed such that they characterize the objects. Functions that operate on the data of an object are tied together in the data structure
MTS 3033 - OBJECT ORIENTED PROGRAMMING
10
Data is hidden and cannot be accessed by external functions. Objects may communicate with each other through functions. New data and functions can be easily added whenever necessary.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
11
FUNCTIONAL PROGRAMMING Emphasis is on doing things (algorithms) Data and functions are kept separately. Design is not very strong, hard to understand and difficult to implement. Using top-down approach. It breaks a program down into components until they cannot be composed anymore.MTS 3033 - OBJECT ORIENTED PROGRAMMING
OOP Emphasis is on data rather than procedure Data and functions are group in class
Design of the whole system could be understand by others (even doesn t have background on science computer) Using bottom-up approach in program design
12
Current problem of software: Software is difficult to develop, maintain and modify. Most software is over budget and delivered late. Programmers still have to create software from the ground-up. OO introduces techniques that helps: Developing a more cost-effective and efficient software that will be delivered on time. Adapt quickly to new changes or client demand.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
13
Pure OO Languages Simula Simulation programming language Found in the late 1960 s By O.J. Dahl and Kristen Nygaard Smalltalk Introduced in the early 1970 s By Alan Kay First successful object oriented language.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
14
Hybrid OO Languages C++, Eiffel, etc Emerged in the mid 1980 s
MTS 3033 - OBJECT ORIENTED PROGRAMMING
15
OOP offers several advantages to both program designer and the user. Object orientation contributes to the solution of many problems associated with the development and quality software and lesser maintenance cost. The principles advantages are : i. Through inheritance, we can eliminate redundant code and extend the use of existing class ii. We can build programs from the standard working modules that communicate width one another, rather than having to start writing the code from scratch. This leads to saving development time and higher productivity.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
16
iii.
iv.
v.
The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of program. It is easy to partition the work in a project based on objects. Object oriented system can be easily upgraded from small to large systems.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
17
i. ii. iii.
Object Class MessageObject Communication
iv. v. vi. vii.
Encapsulation Abstraction Inheritance Polymorphism
MTS 3033 - OBJECT ORIENTED PROGRAMMING
18
The key concepts are: Object
-Directly relate to the real world entities. -Can be a person, thing or concept (a noun). -Like a black box , therefore all the implementation is hidden.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
19
Object has a
Attribute - description of objects in a class Method - an action performed by an object (a verb) Identity (unique name)
MTS 3033 - OBJECT ORIENTED PROGRAMMING
20
Example for attributes and methods
Attributes: manufacturer s name model name year made color number of doors size of engine etc.MTS 3033 - OBJECT ORIENTED PROGRAMMING
21
Methods: Define data items (specify manufacturer s name, model, year, etc.) Change a data item (color, engine, etc.) Display data items Calculate cost etc.MTS 3033 - OBJECT ORIENTED PROGRAMMING
22
Classes can be used to instantiate as many objects as are needed. Each object that is created from a class is called an instance of the class A program is simple a collection of objects that interacts with each other to accomplish a goal
MTS 3033 - OBJECT ORIENTED PROGRAMMING
23
Class
The most important term is the class. A class is the template or blueprint from which objects are actually made. You can create your own class to describe the objects of the problem domains of your application and to adapt the classes that are supplied by the standard library to your own purposes. A class encapsulates the attributes and actions that characterizes a certain type of object.MTS 3033 - OBJECT ORIENTED PROGRAMMING
24
Int x, y; Char ch;
Data Declarations
Method Declarations
MTS 3033 - OBJECT ORIENTED PROGRAMMING
25
Object vs. Class object is created from a
class. object is considered as an instance of a class. class is considered as a template from which objects are instantiated can create an object or many objects from a class.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
26
Object vs ClassDiagram 1: Class CarCar Door ySeat yType yModely
Diagram 2: MyCar as an Object
Drive yStop yLock yUnlocky
MTS 3033 - OBJECT ORIENTED PROGRAMMING
27
Messages Requests for the receiver objects to carry out the indicated method or behavior and return the result of that action to the sender objects
MTS 3033 - OBJECT ORIENTED PROGRAMMING
28
Object Communications Objects communicate by sending messages
MTS 3033 - OBJECT ORIENTED PROGRAMMING
29
MTS 3033 - OBJECT ORIENTED PROGRAMMING
30
MTS 3033 - OBJECT ORIENTED PROGRAMMING
31
Four main design principles of Object-Oriented Programming(OOP): Abstraction Encapsulation Polymorphism Inheritance
MTS 3033 - OBJECT ORIENTED PROGRAMMING
32
Data abstraction is a process to delete all unnecessary attributes and remain the necessary attributes to describe an object. Object in a program is an abstraction from a real object (in real world). Attributes p characteristics, which can be seen. Behaviours p actions that are done to an object. Figure 1.1 shows how data abstraction is done for class Student and Figure 1.2 for class Box.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
33
OBJECT STUDENTCHARACTERISTIC/ATRRIBUTES
CLASS STUDENT
NAME, MATRIK NUMBER, ADDRESS, IC NUMBER ABSTRACTION
BEHAVIOUR/METHOD CALCULATE_MARK (), DETERMINE_GRED(), PRINT_RESULT()
FIGURE 1.1MTS 3033 - OBJECT ORIENTED PROGRAMMING
34
OBJECT BOX
CLASS BOXCHARACTERISTIC/ATRRIBUTES ABSTRACTION
OBJECT BOX
Length, width, depth
BEHAVIOUR/METHOD Calculate_Volume() Calculate_Area()
FIGURE 1.2MTS 3033 - OBJECT ORIENTED PROGRAMMING
35
Focus only on the important facts about the problem at hand To design, produce and describe so that it can be easily used without knowing the details of how it works. Analogy: When you drive a car, you don t have to know how the gasoline and air are mixed and ignited. Instead you only have to know how to use the controls. Draw map
MTS 3033 - OBJECT ORIENTED PROGRAMMING
36
Encapsulation is a
process of tying together all data and methods that form a class and control the access to data by hiding its information. It enables access to object just by using methods of that object. It is one of the security features in objectoriented programming (OOP). Figure 1.3 shows the concept of encapsulation for a class Student and Figure 1.4 for class Box.MTS 3033 - OBJECT ORIENTED PROGRAMMING
37
Student private : int matricNum int icNum char name[30] char address[100] public : double calculate_mark() void determine_mark() void print_result()
Box private : float length float width float depth
public : float calculate_volume() float area()
FIGURE 1.3
FIGURE 1.4
MTS 3033 - OBJECT ORIENTED PROGRAMMING
38
Also known as data hiding >> set () and get () method Only object s methods can modify information in the object. Process of hiding the implementation details of an object. Access to manipulate the object data is through its interface (operations/ functions). Protects an object s internal state from being corrupted by other programs.MTS 3033 - OBJECT ORIENTED PROGRAMMING
39
Program maintenance is easier and less expensive because changes in the object data or implementation is only modified in one place Allows objects to be viewed as black boxes.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
40
the same word or phrase can mean different things in different contexts Analogy: In English, bank can mean side of a river or a place to put money move -
MTS 3033 - OBJECT ORIENTED PROGRAMMING
41
Polymorphism means "many forms A reference variable is always of a single, unchangeable type, but it can refer to a subtype object. A single object can be referred to by reference variables of many different typesas long as they are the same type or a supertype of the object. The reference variable's type (not the object's type), determines which methods can be called! Polymorphism allows extensibility.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
42
Overloaded methods are methods with the same name signature but either a different number of parameters or different types in the parameter list. For example 'spinning' a number may mean increase it, 'spinning' an image may mean rotate it by 90 degrees. By defining a method for handling each type of parameter you achieve the effect that you want. Overridden methods are methods that are redefined within an inherited or subclass. They have the same signature and the subclass definition is used.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
43
of a class may have the same name : - if they have different parameter kinds (value, ref/out) - if they have different parameter types, or - if they have different numbers of parameters Example: void F (int x){} void F (char x) {} void F (int x, long y) {} void F (long x, int y){}MTS 3033 - OBJECT ORIENTED PROGRAMMING
Methods
44
The
write method is overloaded:
Write(String s) Write(int i) Write(double d) and so on... The
following lines invoke different versions of the Writeln method:Console.Writeln ("The total is:"); Console.Writeln(total);
MTS 3033 - OBJECT ORIENTED PROGRAMMING
45
Keyword out and ref are we override the declared as ref or out in order to override the by value characteristic to either by ref or out. Keyword ref : The value type will be passed by reference instead of the default by value behavior In the case of the ref keyword, the variable must be initialized before passing it to the method by reference. C++ also provides the out keyword. This is used for passing a variable for output purposes. This will again be passed by reference. However, when using the out keyword, it is not necessary to init
MTS 3033 - OBJECT ORIENTED PROGRAMMING
46
Resolution of overloading occurs at compile time. The compiler chooses a unique method signature out of several different signatures available (or flags a compile time error if it cannot). Overriding occurs in the context of a single signature. In general, if the class hierarchy contains several definitions with identical method signatures, the appropriate definition is chosen at run time. Within the body of a class that overrides a method, the method from the superclass can be invoked instead by using the super prefix.
MTS 3033 - OBJECT ORIENTED PROGRAMMING
47
Inheritance a way of organizing classes Term comes from inheritance of traits like eye color, hair color, and so on. Classes with properties in common can be grouped so that their common properties are only defined once. Superclass inherit its attributes & methods to the subclass(es). Subclass can inherit all its superclass attributes & methods besides having its own unique attributes & methods.MTS 3033 - OBJECT ORIENTED PROGRAMMING
48
term inheritance refers to the fact that one class can inherit part or all of its structure and behavior from another class. The class that does the inheriting is said to be a subclass of the class from which it inherits. An inherited class is called Shape is a. Example : Triangle is a shape TheShape Triangle Rectangle
MTS 3033 - OBJECT ORIENTED PROGRAMMING
49
Vehicle represents all of the generic attributes and methods of a vehicle
Vehicle
Vehicle is the parent class
Is a relationship Car and Truck are child classes of vehicle Car Truck Car and Truck are specialized versions of a vehicle
MTS 3033 - OBJECT ORIENTED PROGRAMMING
50
Superclass Vehicle Subclasses
Automobile
Motorcycle
Bus
Sedan
Sports Car
Luxury Bus
School Bus
What properties does each vehicle inherit from the types of vehicles above it in the diagram?MTS 3033 - OBJECT ORIENTED PROGRAMMING
51