You are on page 1of 9

ES2D7 System and Software Engineering

Principles - Object Orientated Approaches


Dr Thomas Popham
February 11, 2021

Contents
1 Introduction 2

2 Access existing classes in Matlab 2


2.1 Everything in Matlab is a class . . . . . . . . . . . . . . . . . 2
2.2 Checking the class membership of a variable . . . . . . . . . . 3

3 Defining a simple class using Matlab 3

4 Understanding public/private access to class properties and


methods 5

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.

2 Access existing classes in Matlab


2.1 Everything in Matlab is a class
During this section you will see how the concept of classes is fundamental to
programming. Although you will carry out this lab using Matlab, the same
principles apply in most major programming languages including C++,
Python and Java. In this first exercise we will see that every variable is an
instance of a class. Try the following code:

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:

2.2 Checking the class membership of a variable


We can check in Matlab whether a variable belongs to a certain class using
the command isa. So to check whether variable a is a double (it should be),
we use: isa(a,‘double’). The function should return 1 to indicate that the
answer is true - if you try isa(b,‘double’), Matlab should return a zero.

3 Defining a simple class using Matlab


To define a new class, you need to create a separate file for the class. The
filename of the class must match the name of the class. We will create a
class called ‘MyFirstClass’. The code for this class is shown below. Create
a script using this code and save the file as ‘MyFirstClass.m’.

Page 3
ES2D7 - Object Orientated Approaches

classdef MyFirstClass < handle


blank-line
properties
MainValue;
end
blank-line
methods
function add(obj,inputArg)
obj.MainValue = obj.MainValue + inputArg;
end
blank-line
end
end

To create an instance of this class and access a property, we use the following
code:

% creating an instance of MyFirstClass


mfc = MyFirstClass;
blank-line
% set a property of the class
mfc.MainValue = 1;
blank-line
% read a property of the class
mfc.MainValue

There are two ways of calling a method belonging to a Matlab class. We


can either use the dot notation and type: mfc.add(4); or we can call the
method like a normal function: add(mfc,4);. If we choose this second
approach, the first item that we must pass is the object itself. Both ways of
calling a class method will have the same result 1 .

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

MainValue. To do this, add the following code in the methods section of


your class definition:

function DispValue(obj)
disp(sprintf(‘MainValue: %d’,obj.MainValue));
end

sprintf is a function which allows you to create custom messages that


combine written text and variables. More specifically, sprintf returns a
character vector that is defined by the arguments passed to the sprintf
function. The disp is a function which displays the character vector. Test
this new method to show that it works properly.

Learning Problem 1: Adding a method to a class

• Add a method to MyFirstClass which performs a minus operation (sim-


ilar to the ‘add’ method).

• Perform some tests to show that your new method works.

Learning Problem 2: SysML block diagram

• Draw the SysML block diagram for the MyFirstClass.

4 Understanding public/private access to


class properties and methods
Using your code from the previous section set MainValue to a non-numerical
value i.e. mfc.MainValue = ‘hello’. If you now call the method
DispValue() you will get a (probably) unexpected result. There is an
unexpressed assumption in this class that MainValue should always be a
number and should not be set as a character.

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.

To use an analogy, if you take a flight from Heathrow to New York as


a passenger, you should only be able to access certain ‘methods’ of the
aeroplane e.g. play movie. Most of the aeroplane functionality is hidden
from you (e.g. turning off engines) because exposing you to this interface is
likely to result in disaster. This same principle applies in object-orientated
programming - information should be on a need-to-know basis.

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.

classdef MyFirstClass2 < handle blank-line


properties (SetAccess = private)
MainValue
end
The downside of doing this is that we now need to provide another method
which allows users of our class to change the value of MainVariable. However
by writing a specific method, we can make sure that supplied value meets
our expectations. Figure 2 shows the code that allows a user of this class
to change MainValue. Note that the method checks whether the supplied
‘newvalue’ is a double or not. If newvalue is not a double, an error message
is shown and the script is stopped. Implement this method and check that
it only functions as expected.

Page 6
ES2D7 - Object Orientated Approaches

Figure 2:

Page 7
ES2D7 - Object Orientated Approaches

Learning Problem 3: Creating a class for a bank account

• Create a class BankAccount in Matlab which models a bank account


by including the requirements listed below.

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.

• Draw the SysML block diagram for this class

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

classdef class2 < class1


properties
Location
end
blank-line
methods
end
end

This code describes a new class2 which inherits all the properties and
methods from class1. Class 2 also has a new property:Location.

Learning Problem 4: Practicing Inheritance

• Create a class ChildSavingsAccount in Matlab which is derived from


BankAccount

– 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.

• Draw the SysML block diagram for this class

Page 9

You might also like