A few problems I noticed:
- you have no constructor
- you do not derive from the
handle
base class
The constructor is not strictly necessary, but very useful to get to know for when you really want to start developing larger classes. It is used to initialize the obj
object, which gets passed around to each and every method. It is quite similar to Python's self
, or C++'s this
.
So, your corrected class:
classdef Cat < handle
properties
meowCount = 0;
end
methods
function obj = Cat()
% all initializations, calls to base class, etc. here,
end
function Meow(obj)
disp('meowww');
obj.meowCount = obj.meowCount + 1;
end
end
end
Demonstration:
>> C = Cat;
>> C.Meow;
meowww
>> C.meowCount
1