Top Banner
Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts course for IT students
44

Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

Mar 31, 2015

Download

Documents

Stella Perrett
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

Object Oriented Software

Development2009-2010

Object Oriented PrinciplesSupplementary Lecture: Week 2

Brian Farrimond

Derived from Computing Concepts course for IT students

Page 2: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

2

What is an object? An object is a piece of software that can receive

and act on messages. The piece of software that is an object is a model of

an entity, either physical, conceptual or software.

•Physical entity

•Conceptual entity

•Software entity

Truck

Chemical Process

This is a Microsoft Word document block, bold and italicised and centred in a

box layout

Page 3: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

3

A Microsoft Word object Demo….

Page 4: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

4

A Microsoft Word object Demo…. The block of text is a software object It can receive and act on messages In Windows, we use mouse clicks on buttons

etc to send messages. The set of messages an object can

understand is called its protocol Messages can be “simple” requiring no extra

information e.g. Make bold – Exercise 1 Messages can be “complicated” requiring

extra information e.g. Set font size – Exercise 1

Page 5: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

5

Objects can hold informationAn object has data or attributes (state) An implementation of a protocol

(functionality expressed as responses to messages)

Page 6: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

6

Objects respond to messagesAn object has data or attributes (state) An implementation of a protocol

(functionality expressed as responses to messages)

Page 7: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

7

Objects ……An object has data or attributes (state) An implementation of a protocol

(functionality expressed as responses to messages)

Page 8: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

8

The state of an object The information an object holds at any

one time is known as its state. What kind of information does a block of

text hold - Exercise 2 What is the state of the following block of

text? - Exercise 3

Mary had a little lamb

Page 9: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

9

Object attributes Each piece of information an object

holds (state) is called an attribute Some of the attributes of a block of text

are:

Page 10: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

10

Object attributes Each piece of information an object

holds (state) is called an attribute Some of the attributes of a block of text are:

Text Colour Justification Font type Font size Bold Italic Underline Strikethrough

Page 11: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

11

Its fleece as white as snow

Exercise 4

Page 12: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

12

Exercise 5 What is the only way we can change

the state of an object?

Page 13: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

13

Picture objects Demo… Exercise 6

Page 14: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

14

Table objects Exercise 6

Page 15: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

15

Conclusions Block of text, picture and table are

different kinds of objects. They have some similarities but

some differences: in the way they respond to messages in the information they hold

Page 16: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

16

Conclusions We borrow from the world of nature

where we recognise different kinds of animals

They have some similarities but some differences: Cats and dogs both have four legs Cats purr, dogs do not

Page 17: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

17

Conclusions In biology we refer to species – Dog, Cat,

Mouse etc Rover and Spot are examples of the Dog

species Felix and Tiddles are examples of the Cat

species In object oriented terms we say that

Rover and Spot are instances of the Dog class Felix and Tiddles are instances of the Cat class

An object is an instance of a class

Page 18: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

18

Objects belong to classes All objects are of some class of

objects. E.g. “That red Nissan Primera in my driveway is a car.”

In object oriented language we say: That red Nissan Primera in my driveway is an instance of class car.

Page 19: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

19

Objects belong to classes In object oriented programming

we write code to create classes. The code for each class we write identifies: Attributes - the information about

objects in the class we want to use Methods - how the objects respond to

messages they receive

Page 20: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

20

Identifying attributes A class is to model books, storing the

books’ title, author and retail price. Suggest a suitable set of attributes.

Rules for names No spaces – you can join words together Lower case apart from initial letters of second

word, third word etc Attributes are:

Page 21: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

21

Identifying attributes A class is to model books, storing the

books’ title, author and retail price. Suggest a suitable set of attributes.

Rules for names No spaces – you can join words together Lower case apart from initial letters of second

word, third word etc Attributes are:

title author retailPrice

Page 22: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

22

Object diagrams We can represent objects showing their state

and protocol in an object diagram

Page 23: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

23

Object diagrams Here is a particular green object named Pie1

in the top left corner facing east.

Page 24: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

24

Exercise 14

Page 25: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

25

Page 26: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

26

Identifying attributes A class is to model customers, storing

name, address, current balance, overdraft limit.

Rules for names No spaces – you can join words together Lower case apart from initial letters of second word, third word etc

Attributes are:

Page 27: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

27

Identifying attributes A class is to model customers, storing

name, address, current balance, overdraft limit.

Rules for names No spaces – you can join words together Lower case apart from initial letters of second word, third word etc

Attributes are: name address currentBalance overdraftLimit

Page 28: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

28

Identifying the protocol Messages return an answer Messages need a name Messages may carry information

Complicated messages like setting Font size

Protocol needs to document these in a signature.

Page 29: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

29

Messages return an answer Messages need a name Messages may carry information

A message signature:void setFont(String fontName)

Answer type

Message name

Parameter list containing information

Identifying the protocol in C++

Page 30: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

30

Messages return an answer Messages need a name Messages may carry information

A message signature:String getAddress()

Answer type

Message name

Parameter list containing information

(empty in this case)

Identifying the protocol in C++

Page 31: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

31

A message signature:void setFont(String fontName)

Using the protocol

Message name Parameter list

containing information(the name of the desired

font)

Answer type(void means

no answer will be given)

Example messages:

setFont(“Times New Roman”)

setFont(“Arial”)

Page 32: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

32

A message signature:void setFontSize(int fontSize)

Using the protocol

Message name Parameter list containing

information(the desired size of the

font)

Answer type(void means

no answer will be given)

Example messages:

setFontSize(12)

setFontSize(32)

Page 33: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

33

A message signature:void addTeam(String name, int points)

Using the protocol

Message name Parameter list containing

information(the desired team name and number of points)

Answer type(void means

no answer will be given)

Example messages:

addTeam(“Tranmere Rovers”, 23)

addTeam(“Chester”, 32)

Page 34: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

34

Sending messages to an object

textBlock1.setFont(“Courier New”)

Receiver of

message

The full

stop

The message

Page 35: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

35

General Message sends

Receiver of

message

Object . Message

The message

An Object “DOT” a message name

Page 36: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

36

Attribute names in C++ In C++, attributes are referred to as

member variables Consequently, the convention is to

begin their names with m_ E.g. m_name, m_dataOfBirth

Page 37: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

37

Example on Page 58 - 59 A C++ software developer has been

commissioned to create a hospital administration application in which wards are to be modelled. He will need to be able to store for each ward the following information: the name of the ward, the name of the ward sister, the total number of beds on the ward and the number of patients currently on the ward.

Page 38: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

38

Attributes He will need to be able to store for each

ward the following information: the name of the ward, the name of the ward sister, the total number of beds on the ward and the number of patients currently on the ward.

Attributes?

Page 39: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

39

Attributes He will need to be able to store for each

ward the following information: the name of the ward, the name of the ward sister, the total number of beds on the ward and the number of patients currently on the ward.

Attributes? m_wardName m_wardSisterName m_numberBeds m_numberPatients

Page 40: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

40

Protocol He will want to send messages to the ward

object to make it carry out the following actions: set the total number of beds in the ward to a given value admit a patient to the ward discharge a patient from the ward set the name of the ward sister to a given value reply with the name of the ward sister

Protocol

Page 41: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

41

Protocol He will want to send messages to the ward object to

make it carry out the following actions: set the total number of beds in the ward to a given value admit a patient to the ward discharge a patient from the ward set the name of the ward sister to a given value reply with the name of the ward sister

Protocolvoid setNumberBeds(int numberOfBeds)void admitPatient()void dischargePatient()void setWardSisterName(String name)String getWardSisterName()

Page 42: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

42

Using the protocol Suppose the user of the hospital administration

program carries out the following sequence of actions on an object of this class called Ward1.

Admit a patient Discharge a patient Make the total number of beds 30

Set the ward sister name to Mary Jones

Page 43: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

43

Using the protocol Suppose the user of the hospital administration program

carries out the following sequence of actions on an object of this class called Ward1.

Admit a patient Discharge a patient Make the total number of beds 30 Set the ward sister name to Mary Jones

Ward1.admitPatient(); Ward1.dischargePatient(); Ward1.setNumberBeds(30); Ward1.setWardSisterName(“Mary Jones”);

Page 44: Object Oriented Software Development 2009-2010 Object Oriented Principles Supplementary Lecture: Week 2 Brian Farrimond Derived from Computing Concepts.

44

Heading towards C++ C++ programming is mainly about

creating classes of objects The C++ code that is executed

when an object is created or when a message arrives creates and manipulates other objects