Professional Documents
Culture Documents
Contents
1 Introduction 2
5 Inheritance 8
1
ES2D7 - Object Orientated Approaches
1 Introduction
The objectives of this lab are:
• to use existing classes in Matlab
• to define your own simple class with methods and properties
• to understand the concepts of public and private access to class mem-
bers (methods/properties)
• to implement composition and inheritance relationships using Matlab
code
• to practice drawing SysML diagrams to represent software
Some hints for this lab:
• Where possible try to avoid copy and pasting code from this lab-
sheet - it is much better to type the code out yourself, as you will notice
all the details.
• It is a good idea to carry out the steps in this lab in either a script or
a live script.
clear all;
a = 3;
b = ‘cat’;
c = tf([1],[1 1]);
whos;
Page 2
ES2D7 - Object Orientated Approaches
You should see the output shown in Figure 1, which shows the class of each
variable. Variable a is an instance of the class ‘double’. Variable c is an
instance of the transfer function class ‘tf’. These objects also have methods,
which can be seen (for the transfer function) by typing: methods(c), which
should display a long list of methods. In previous modules, you have used
the command step(c), which draws the response of a system. Without
realising it, you were already running a method from the tf class.
Figure 1:
Page 3
ES2D7 - Object Orientated Approaches
To create an instance of this class and access a property, we use the following
code:
We will now add another method DispValue to our class which shows
1
for some built-in Matlab classes, it will only allow you to use the second approach to
access methods.
Page 4
ES2D7 - Object Orientated Approaches
function DispValue(obj)
disp(sprintf(‘MainValue: %d’,obj.MainValue));
end
Page 5
ES2D7 - Object Orientated Approaches
Now imagine that you are working in a team and that each person is
responsible for creating a separate class. At the moment, it’s not clear
where the ‘error’ has come from. Did the person who wrote the class make
a mistake, or did the person who used the class make a mistake? A strong
argument for using classes is that we can avoid this behaviour by ensuring
that users of a class can only access certain methods or properties. If a
method or property can be only accessed from within the class, then this
is known as a private property or method. A public property or method
can be accessed from within and outside the class. This approach to
controlling class membership access is known as data hiding and reduces
overall complexity by ensuring that the interfaces are tightly controlled.
Create a copy of your class and change the name, so that we can make some
adjustments. Set the access to MainValue as private - see below. If you now
try to change this property from a Matlab script, an error will appear.
Page 6
ES2D7 - Object Orientated Approaches
Figure 2:
Page 7
ES2D7 - Object Orientated Approaches
1. The bank account should store the following information: the bal-
ance (in pounds), the account number and the name of the account
holder.
2. Create a method to set the bank balance to the specified value:
SetBalance(amount).
3. Create a method to display the bank balance: DispBalance()
4. Create a method which returns the bank balance: GetBalance().
5. Create a method: Withdraw(amount), which reduces the balance
of the account by the amount passed to the Withdraw method.
6. Create a method: PayIn(amount), which increases the balance of
the account by the amount passed to the PayIn method.
7. Modify your method Withdraw so that the amount withdrawn
cannot be greater than £10,000.
8. Modify your method PayIn so that a negative number is not ac-
cepted.
9. Create a method PayInterest() to pay 4% interest.
5 Inheritance
We will now look at how one class can be derived from another class. In
Matlab this is specified on the first line of the class definition using the <
symbol:
Page 8
ES2D7 - Object Orientated Approaches
This code describes a new class2 which inherits all the properties and
methods from class1. Class 2 also has a new property:Location.
– The new class should also have the name of the child’s par-
ent/guardian
– The new class should also have the age of the account holder (the
child) - use variable child age
– Modify the PayInterest method so that the interest is only paid
if the account holder is less than 18 years old.
Page 9