MATLAB Object-Oriented Programming

(Joyce) #1

Class Constructor Methods


In this section...
“Purpose of Class Constructor Methods” on page 9-21
“Basic Structure of Constructor Methods” on page 9-21
“Guidelines for Constructors” on page 9-23
“Default Constructor” on page 9-23
“When to Define Constructors” on page 9-24
“Related Information” on page 9-24
“Initializing Objects in Constructor” on page 9-24
“No Input Argument Constructor Requirement” on page 9-25
“Subclass Constructors” on page 9-26
“Implicit Call to Inherited Constructor” on page 9-29
“Errors During Class Construction” on page 9-29
“Output Object Suppressed” on page 9-30

Purpose of Class Constructor Methods


A constructor method is a special function that creates an instance of the class. Typically,
constructor methods accept input arguments to assign the data stored in properties and
return an initialized object.

For a basic example, see “Create a Simple Class” on page 2-2.

All MATLAB classes have a default constructor method. This method returns an object of
the class that is created with no input arguments. A class can define a constructor method
that overrides the default constructor. An explicitly defined constructor can accept input
arguments, initialize property values, call other methods, and perform other operations
necessary to create objects of the class.

Basic Structure of Constructor Methods


Constructor methods can be structured into three basic sections:


  • Pre-initialization — Compute arguments for superclass constructors.


Class Constructor Methods
Free download pdf