A Guide to MATLAB Object-Oriented Programming

Chapter 11: Constructor Redux

In the first part of this book, objects were constructed in the most basic way because no arguments were passed into the constructor. With a no-argument constructor, all objects are constructed using the same initial values. For the Part 1 cShape class, this basic approach worked because Part 1 focused primarily on encapsulation mechanics. Now that we understand encapsulation, we will turn our attention to inheritance and the development of class hierarchies. With the development of class hierarchies, we also need a richer set of construction options.

For example, if we want cShape to serve as a parent for cStar and cSquare, the constructors for cStar and cSquare need to initialize mPoints with different values. The best time to perform the initialization is during construction, and a constructor that accepts arguments is the best way to tailor the construction process. Instead of relying on hard-coded values, constructor arguments are used to initialize private variables. As with any function, we can pass any number of arguments into the constructor through varargin. The number of arguments along with their types can then be used to select the appropriate initialization commands. Different classes have different construction requirements. In this chapter, we develop an extendable organization we can use to implement general-purpose constructors.

11.1 SPECIFYING INITIAL VALUES

Two initial-value cases are so common that they have special names. The no-argument constructor is called the default constructor. We already know...

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Math Calculation Software
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.