MATLAB Object-Oriented Programming

(Joyce) #1
end
end
end
end

The ProjectEngineer class requires a cell array of names, a numeric array of phone
extensions, and a cell array of billing rates for each engineer in the team.

nm = {'Fred','Nancy','Claudette'};
px = [8112,8113,8114];
rt = {'C2','B1','A2'};
tm = ProjectEngineer(nm,px,rt)

tm =

1x3 ProjectEngineer array with properties:

Rate
Name
PhoneX

Potential Error


The TeamMembers constructor initializes the object array with this statement:

obj = repelem(obj,1,n);

Because the obj argument to repelem is a ProjectEngineer object, the array
returned is of the same class.

Without this statement, the TeamMembers constructor would create default objects to fill
in array elements in the for loop. The resulting heterogeneous array would be of the
class of the common superclass (TeamMembers in this case). If the superclass returns this
heterogeneous array to the subclass constructor, it is a violation of the rule that class
constructors must preserve the class of the returned object.

MATLAB issues this error:
When constructing an instance of class 'ProjectEngineer', the constructor must
preserve the class of the returned object.
Error in ProjectEngineer (line 8)
obj = obj@TeamMembers(varargin{1:2});

10 Object Arrays

Free download pdf