MATLAB Documentation for OOP
(For reference by students)


 

CONSTRUCTOR
Definition A method named the same as the class that consumes initialization data and produces the initial data structure
 
Example
function acct = BankAccount(data)
% BankAccount class constructor.
%   ba = BankAccount(amt) creates a bank account
%                             with balance amt
% could also be a copy contructor:
%   ba = BankAccount( oba ) copies the account oba
if nargin == 0                            *%*
   acct.balance = 0;
   acct = class(acct,'BankAccount');
elseif isa(data, 'BankAccount')           *^*
   acct = data;
else
   acct.balance = data;
   acct = class(acct,'BankAccount');
end
 
A constructor should take into account the different instances of variations being passed into the function.

* %* Provides for being called as just BankAccount without any data being passed into it.
*^*    A copy constructor where if the parameter is a BankAccount, just copy it

 


Got comments\feedback\suggestions\complaints\anything whatsoever? Shoot the webmaster an email!

Website created & maintained by Shardul Bhatia and Wen Eu Cheah