Upload
stella-perrett
View
214
Download
0
Tags:
Embed Size (px)
Citation preview
Object Oriented Software
Development2009-2010
Object Oriented PrinciplesSupplementary Lecture: Week 2
Brian Farrimond
Derived from Computing Concepts course for IT students
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
3
A Microsoft Word object Demo….
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
5
Objects can hold informationAn object has data or attributes (state) An implementation of a protocol
(functionality expressed as responses to messages)
6
Objects respond to messagesAn object has data or attributes (state) An implementation of a protocol
(functionality expressed as responses to messages)
7
Objects ……An object has data or attributes (state) An implementation of a protocol
(functionality expressed as responses to messages)
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
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:
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
11
Its fleece as white as snow
Exercise 4
12
Exercise 5 What is the only way we can change
the state of an object?
13
Picture objects Demo… Exercise 6
14
Table objects Exercise 6
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
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
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
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.
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
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:
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
22
Object diagrams We can represent objects showing their state
and protocol in an object diagram
23
Object diagrams Here is a particular green object named Pie1
in the top left corner facing east.
24
Exercise 14
25
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:
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
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.
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++
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++
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”)
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)
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)
34
Sending messages to an object
textBlock1.setFont(“Courier New”)
Receiver of
message
The full
stop
The message
35
General Message sends
Receiver of
message
Object . Message
The message
An Object “DOT” a message name
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
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.
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?
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
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
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()
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
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”);
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