© n barnes 2004 object-orientated programming (oop) unit 4 computing

39
© N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

Upload: job-mcdowell

Post on 18-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Object-orientated Programming (OOP)

Unit 4 Computing

Page 2: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Programming Languages

All computer languages can be put in one of the following categories:

• Low Level Languages

• High Level Languages

Page 3: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Low Level Languages

• are machine orientated – an assembly language program written on one machine will not work on any other type of machine i.e. not portable.

• one assembly language instruction = one machine code instruction

• use mnemonics

Page 4: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

High Level Languages

• are not machine orientated i.e. they are portable.

• have statements that are close to English.

• one statement = many machine code instructions

• are problem orientated – different hlls have structures and facilities appropriate for a particular type of problem.

Page 5: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Match the descriptions to the correct type of high level language• a sequence of instructions which are

executed in a programmer-defined order

• a set of facts & rules which are used to answer questions

• includes real-time facilities, interaction with hardware interfaces and supports concurrent programming

• execution of code depends upon an event e.g. user clicking a mouse button

• Event-driven• Imperative• Declarative• Languages

for real-time embedded systems

• Procedural

Page 6: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Event-driven

• Programs written in an imperative language start at the beginning and run until they finish in a programmer-defined order.

• Programs written for a Windows-like environment need to be event-driven – nothing happens until the user does something.

• Object-Orientated Programming (OOP) languages were developed for writing event-driven programs as traditional hlls were not suitable for this.

Page 7: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Classes

• Objects and classes are the two key ideas• A class is a collection of data structures and the

methods that can be used with these data structures

• Methods are sometimes also called activities/procedures/code/functions/behaviours

• Sometimes instead of data structures the terms properties/characteristics/record/attributes might be used.

Page 8: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Classes Example 1

• For example the mammal class might have attributes height, weight, and age; and have methods eat, drink, breathe and sleep (a slightly simplified example).

Page 9: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Classes Example 2

• The camera class might have attributes shutter speed, aperture, flash setting; and have methods take picture, turn flash on, turn flash off, adjust aperture, adjust shutter speed.

Page 10: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Objects

• An object is an instance of a class

• An object is a collection of data structures and methods

• Rover the Dog is an object of class mammal

• The Canon S50 is an object of class Camera

• Mr T is an object of class mammal

Page 11: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Objects continued…

• A dialogue box class would have attributes including position and size.

• Methods would include OnOK, OnCancel, OnClose – describing what to do when the user clicks on the OK or Cancel buttons, or on the X.

This is an instance of the dialogue box class i.e. an

object

Page 12: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Objects v Classes

• A class can be thought of as a blueprint for an object.

• This blueprint can then be used to create one object or thousands of objects.

• A class does not represent an object; it represents all the information that a typical object should have as well as the methods that it should have.

Page 13: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Object-orientated programming

• To write a program using an OOP you first define the different classes.

• Next you create objects (called instantiation – similar to declaring variables).

• That’s it.• When the program is run events will cause

objects to interact with each other via messages and cause things to happen.

Page 14: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Encapsulation

• is defined as the joining together of methods and data into objects.

• all objects have private and public areas.• the data structures are put in the private

area – this means that they can only be used within this object (like a local variable) i.e. by the object’s methods.

• the methods are put in the public area so that they can be called by other objects.

Page 15: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Defining a class• We are now going to

define the class for the camera class.

• The first thing you have to do is to say that you are defining a class and to give it a name.

• You then need to state what methods

• and attributes there are.

camera = classprivate:

shutterspeed : real;aperture : real;flashsetting : integer;

public:takepicture;turnflashon;turnflashoff;adjustaperture;adjustshutterspeed;

end;

Page 16: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Instantiation

Now that you have defined a class you can instantiate objects. This is very similar to declaring variables in a high level programming language.

• canonS50 : camera;

• mycamera : camera;

Page 17: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Exercise 11) Define the mammal class,

as described in the picture on the right.

2) Declare the following objects: Lassie, Cletus, Skippy the Bush Kangaroo.

3) Define a class TV – TVs can be turned on/off, change channel and adjust volume; they have attributes on?, volumelevel and channel.

Page 18: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Exercise 1 Question 1 answer

mammal = class

private:

age : integer;

height : integer;

weight : integer;

public:

sleep;

eat;

breathe;

drink;

end

Page 19: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Exercise 1 Question 2 answer

Lassie : mammal;

Cletus : mammal;

Skippy the Bush Kangaroo : mammal;

Page 20: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Exercise 1 Question 3 answer

TV = classprivate:

on? : boolean;volume : integer;channel : integer;

public:turn on/off;changeChannel;adjustVolume;

end

Page 21: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Inheritance 1

• Object-orientated programming allows for classes to be defined in terms of other classes.

• For example bears, cats, mice and humans are all subclasses of the mammal class.

• The mammal class is called the Base Class or SuperClass.

Page 22: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Inheritance 2

• Each subclass inherits state and methods from the Base Class.

• Humans, mice, cats and bears all inherit some attributes from the mammal class: age, gender, height in cm, weight in kg.

• They also inherit some behaviours from the mammal class: eat, drink, sleep and breathe.

Page 23: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Inheritance 3

• Subclasses can also have their own attributes and methods that they have not inherited.

• For instance bears can also do stuff in woods and break into cars. All bears have a name.

Page 24: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Exercise 2

1) Write a class definition of the bear class, that inherits from the mammal class (defined in Exercise 1). Hint: You do not need to restate everything that is inherited from the mammal class.

2) Instantiate the following objects of class bear: Rupert, Winnie, Paddington.

Page 25: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Exercise 2 answers

bear = class derived from mammalprivate:

name : string;public:

do_stuff_in_woods;break_into_cars;

end

winnie : bear;rupert : bear;paddington: bear;

Page 26: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Inheritance Diagrams

Inheritance can also be represented diagrammatically.

Bear Cat

Mammal Notice that the arrows point upwards

Page 27: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Exercise 3

1) Draw an inheritance diagram for bikes, tandems, mountain bikes and racing bikes all inherit from the bike super class.

2) Draw an inheritance diagram for reptiles: lizards and snakes inherit from reptile; pythons and cobras inherit from snake; geckos and chameleons from lizard.

Page 28: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Exercise 3 Question 1 answer

Bike

Tandem Mountain Bike

Racing Bike

Page 29: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Exercise 3 Question 2 answer

Reptile

Snake

Cobra Python

Lizard

Gecko Chameleon

Page 30: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Polymorphism

• Two classes are said to be polymorphic if they have some of the same data structures or behaviours.

• There are two types of polymorphism – independent and inherited.

Page 31: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Inherited polymorphism

• Here are the bear and mammal classes from earlier.

• If we also have a human class, that is derived from the mammal class, then the human and bear classes will have inherited polymorphism on the eat, breathe, sleep and drink methods; the age, weight, height and gender attributes.

Page 32: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Independent polymorphism

• Bear and human classes (as shown on the right) also have independent polymorphism on the name attribute.

• This is when two classes have some of the same behaviours or data structures but these have not been inherited.

human = class derived from mammal

private:

name : string;

public:

talk;

end;

Page 33: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Independent polymorphism 2

Tigers and Fish might both have a swim method. This could not be inherited from an animal base class as they both have to swim in very different ways. Therefore, this is another example of independent polymorphism.

Page 34: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Containment

• Containment is the idea that an object can contain other objects.

• A house object could have two door objects and six window objects.

• A dialogue box object could have an OK button object and a cancel button object.

Page 35: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Messages

• In an object-orientated program objects are instantiated and then events cause them to communicate with each other by sending messages.

• To look at how messages work we will be using two classes: vet and cat.

Page 36: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Vet class

The vet class inherits from the human class. Vets also have the methods

• drive car• send dog to ‘sleep’

(the best thing for all dogs)

• castrate cat

Page 37: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Cat class

• The cat class is derived from the mammal class.

• Cats also have the methods: purr, hack up fur ball and receive surgery.

• Cats have the attributes: name, fur colour and number of testicles.

Page 38: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Example continued

• We are going to send a message between Tiddles the Cat and John the Vet.

• Tiddles has been a naughty boy and his owner would like to change the value of his number of testicles attribute to 0.

• You cannot just say: tiddles.NumberOfTesticles = 0 as the data is kept in the private area of an object.

Page 39: © N Barnes 2004 Object-orientated Programming (OOP) Unit 4 Computing

© N Barnes 2004

Example continued 2

• The owner has to take Tiddles to the vet.

• The castrate cat method of John the Vet will then send a message to the receive surgery method of Tiddles the Cat.

• The receive surgery method will then change the number of testicles attribute of Tiddles the Cat from two to zero.