a(3) = BasicClass(7);
roundOff(a)
ans =
2.8000 0.8700 7.0000
Overload Functions
Classes can implement existing functionality, such as addition, by defining a method with
the same name as the existing MATLAB function. For example, suppose that you want to
add two BasicClass objects. It makes sense to add the values of the ObjectValue
properties of each object.
Here is an overload of the MATLAB plus function. It defines addition for this class as
adding the property values:
method
function r = plus(o1,o2)
r = [o1.Value] + [o2.Value];
end
end
By implementing a method called plus, you can use the “+” operator with objects of
BasicClass.
a = BasicClass(pi/3);
b = BasicClass(pi/4);
a + b
ans =
1.8326
Related Information
For information on overloading functions, see “Overload Functions in Class Definitions”
on page 9-34.
For information on overloading operators, see “Operator Overloading” on page 17-47.
2 Basic Example