Download presentation
Presentation is loading. Please wait.
1
Object-Orientated Programming
2
Object-Orientated Programming
Windows programs are event–driven: nothing happens until the user moves the mouse, presses a key, selects from a menu, presses a command button, clicks on a tool bar
3
Object-Orientated Programming
Traditional programming methods and the languages that are used are not suitable for Windows environment Instead Object-Orientated Programming (OOP) languages such as Visual basic, Visual C++ and Delphi have been developed to write Windows applications
4
Objects and Classes The class is the blueprint that defines a related group of things The class sets down the facts and rules the group has in common but does not store specific data Once a class is defined in O.O.P. then instances of the class can be produced
5
Objects and Classes Task 1: I want you to find all the other animal members of your group Task 2: In your groups write down 5 common attributes associated with your groups “class” of animals Task 3: In your groups write down 5 common activities carried out within your “class” of animals
6
Objects and Classes The object is a member of the class. It takes the definition and adds data Objects (routines) are collections of attributes and activities (called methods in Delphi and Visual Basic) that need to be performed such as… Write code that executes as fast as possible Occupies as little memory as possible
7
Objects in Windows An object can be thought of as an independent procedure that contains both the instructions and data to perform some task and the code necessary to handle various messages it may receive A dialog box in Windows is physically different from any other object The code contains all the instructions necessary to draw the box on screen, display a text string in the title bar, and include a control-menu box in the top left-hand corner
8
Typical Facilities The dialog box has attributes which are given values to define its colour and thickness of its border The object also contains procedures which react to events which might occur, such as a user dragging the box or clicking the ‘Yes’ button What happens when a procedure is carried out? For example: other objects such as a message string and command buttons are displayed. They are separate objects with particular attributes and methods
9
Encapsulation In third-generation languages such as Pascal, the programmer can choose to write structured, modular programs and divide the code up into a number of procedures However, this is down to personal preference whether a task is broken up into one, two or three procedures Nevertheless…
10
Encapsulation Within Object-Orientated programming…
Encapsulation is the concept of keeping all the relevant parts together such as procedures and data An object usually represents some real concept such as dialog box, command button or list Must take into consideration the procedures that will affect the object
11
Encapsulation An object contains all that is required to make that particular aspect of the program carry out its tasks: The procedures of methods that are required to respond to events that may occur The data that is required by these procedures Note the data is only accessed through the object.
12
O.O.P. Benefits O.O.P. utilises member functions which means that only the object can access and control the data directly (Geography/History teacher scenario) Variables are retained throughout the duration of the program Whereas conventional programming uses routines which could accidentally access and alter the private data Especially in the case of global variables which could be mucked-up via other procedures With O.O.P. it’s difficult to alter/accidentally muck-up the already accomplished code If the data needs to be altered in anyway, only the object that controls the data needs to be modified Modifications can be carried out quickly and by one member of a programming team with the work being added to: inheriting base class characteristics
13
Inheritance Many objects are related to other objects in some way
Task 4: Chart Many objects are related to other objects in some way A sports car and a lorry are part of the Vehicle class Humans, tigers and whales are members of the Mammal Class You can define a new type of object by amending the definition of a similar object In object-orientated programming, this has the advantage of allowing the reuse of existing code, and is known as inheritance Taking and adding to the features of an existing class You cannot delete or change existing features and abilities
14
Mammal Class Human Class Tiger Class
Inheritance Mammal Class Correct Human Class Tiger Class Properties: Methods: Age Eat Gender Breathe Body Temp Sleep Properties: Methods: Age Eat Gender Breathe Body Temp Sleep Addition. Properties: Methods: Name Talk Address Properties: Methods: Age Eat Gender Breathe Body Temp Sleep Addition. Properties: Methods: Species Hunt Fur Colour
15
The man and the tiger will have to run the same way.
Inheritance Mammal Class If you are creating a computer game that requires a run method: it is best to put it into the man object or tiger object. Defining it within the mammal class is too restrictive. Incorrect: The Run method is too specific at this stage: humans run with two legs and tigers with four Human Class Tiger Class The man and the tiger will have to run the same way. Properties: Methods: Age Eat Gender Breathe Body Temp Run Properties: Methods: Age Eat Gender Breathe Body Temp Run Addition. Properties: Methods: Name Talk Address Properties: Methods: Age Eat Gender Breathe Body Temp Run Addition. Properties: Methods: Species Hunt Fur Colour
16
Polymorphism Two or more classes that are derived from the same base class are said to be polymorphic Polymorphism means that two objects may share many characteristics but have unique features of their own When new classes are derived from a base class they may redefine some of the base methods Polymorphism allows for objects of different classes to recognise and process the same messages, using either the same or different methods
17
Polymorphism Inherited Polymorphism is used when objects of different classes process the same message because they inherit it from a common ancestor In the HUMAN and TIGER example, the method for sleep could be defined the same way Independent Polymorphism is used when different classes use the same field or method name for different values or activities
18
Polymorphism Containment
We could define Run method for HUMANS which would outline the mechanics of running (good for a computer game), but this method would not work for the TIGER class, which uses four legs to run Therefore, the Run method cannot be put into the MAMMAL class but a Run method could be put into each of the HUMAN and TIGER classes Containment Objects can contain other objects You could create a Vehicle Class that contains objects such as ‘engine’ and ‘equipment’ A ‘House’ class could contain several ‘Room’ objects
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.