From: Quoc Nguyen on
Hi everyone,

I am new to object-oriented programming. Please help.

My class is defined as follows:

-------------------------
(myclass.m)

classdef myclass
properties
a = [];
end

methods
function obj = myclass(n)
obj.a = n;
end
function y = inc(obj)
obj.a = obj.a + 1;
y = obj.a;
end
end
end
-------------
(workspace)

>> x = myclass(1);
>> y = inc(x)

y = 2

>> x.a

ans = 1

---------

In the m-code above, I expect that x.a is equal to 2 after executing y = inc(x).

How can I modify the code so that whenever the method inc() is executed, the property a is also updated according to the command "obj.a = obj.a + 1;" ?

Thanks