®
Click below to go directly to a specific section:
The following table will briefly explain how Matlab® creates an object, object constructor, object data and methods as well as compare it to the strategies used in C++. The examples used in this section match the code in the source section of this page.
Object Component | Matlab® Implementation Method | C++ Comparison |
---|---|---|
Declaration | Creation of a sub-directory beginning with an
"at"(@) symbol accessible
through the search path with a name matching that of the object name. e.g. C:\Matlab\@simp_eq |
class simp_eq { . . . . }; |
Constructor | A file named after the object, containing a function named after the
object. e.g. File: C:\Matlab\@simp_eq\simp_eq.m Contains: function obj=simp_eq(..param..) |
public: simp_eq(..param..); |
Member Data | All data created in the constructor is private and accessable only by
member functions. e.g. obj.domain = []; |
Data can be made public or private. Private data only accessible by member functions. |
Member Methods | A file with name matching method name, located in object directory.
Accepts the object as a parameter in order to access member data. If function not
located in object directory, then member data is not accessible. e.g. File: C:\Matlab\@simp_eq\show.m Contains: function show(se) *** se is a simp_eq object |
public: void show(); |
Overloaded Operators | All MATLAB symbolic operators such as + and - are represented with their
appropriate name: plus and minus. When a statement uses an expression such as
1+1, MATLAB invokes the plus function for integer values. Overloading operators is
thus very easy. e.g. File: C:\Matlab\@simp_eq\plus.m Contains: function out=plus(eq1, eq2) |
public: simp_eq operator+(simp_eq&); |
Using the object once created | 1. Create the object 2. Call a method e.g. >> my_eq = simp_eq('sin', linspace(-pi, pi, 100)); >> show(my_eq); |
1. Create the object 2. Call a method e.g. simp_eq my_eq('sin', domain); my_eq.show(); |
This first segment of code is the simp_eq.m file located in the @simp_eq directory. This contains all of the object constructor information.
function s = simp_eq(varargin) % Name: simp_eq % Location: <directory in path>/@simp_eq % Purpose: 1. Generic object constructor % 2. Specific constructor % 3. Copy constructor % Pre: No Parameters - Default constructor code used % 1st Parameter - Simple function type (e.g. 'sin') % 2nd Parameter - Function domain (default -1 to 1 by 10 % Object as Parameter - Copy constructor % Post: Return a created object % % If no parameters, then default constructor if nargin == 0 % Default to sin function s.func = ['sin']; % Fill the domain with the numbers -1 to 1 by 2/9 s.domain = linspace(-1, 1, 10); % Register this variable as a simp_eq class s = class(s, 'simp_eq'); % Else if the parameter is a simp_eq, then copy constructor elseif isa(varargin{1}, 'simp_eq') % Copy the variable s = a; % Otherwise, use a specific constructor else % If only one parameter passed if nargin == 1 s.func = [varargin{1}]; s.domain = linspace(-1, 1, 10); % If both parameters passed else s.func = [varargin{1}]; s.domain = varargin{2}; end % Register this variable as a simp_eq class s = class(s, 'simp_eq'); end
This second segment of code is the show.m file located in the @simp_eq directory. This contains the object method show which will plot the simple equation.
function show(s) % Name: show % Location: <directory in path>/@simp_eq % Purpose: Plot the simple function % Pre: N/A % Post: Displays screen error if no function is set % Displays plot of function otherwise % % Create a new figure window figure; % Initialize the yvals to zero yvals = zeros(1,length(s.domain)); % Determine the number of functions for this equation [neq, w] = size(s.func); % Evaluate the function over its domain for i=1:neq yvals = yvals + feval(s.func(i,:), s.domain); end % Plot the function plot(s.domain', yvals'); % Make the shape of the axis square with sides equal axis('square', 'equal'); % Set the title of the title(strcat('Plot of y = ', s.func, ' x')); % Set the title of the plot titl = 'Plot of y = '; for i=1:neq if (i == neq) titl=strcat(titl, ' ', s.func(i,:), 'x'); else titl=strcat(titl, ' ', s.func(i,:), 'x +'); end end title(titl);
This third segment of code is the plus.m file located in the @simp_eq directory. This contains the overloaded object method plus (binary) which will combine two equations to form a new equation with a common domain
function out = plus(eq1, eq2) % Name: plus % Location: <directory in path>/@simp_eq % Purpose: Make a new simp_eq by combining the two equations % Pre: Two simp_eq's are passed to this function % Post: New simp_eq object created with plus % % Construct a new simp_eq from the functions of eq1 and eq2 % As well as the common domain of the two functions out = simp_eq([eq1.func; eq2.func], ... intersect(eq1.domain, eq2.domain));
>> se = simp_eq('sin', linspace(-pi, pi, 100);
>> show(se);
>> se2 = simp_eq('cos', linspace(-pi, pi, 100);
>> sincosse = se + se2;
>>
This program requires version 5.1 of Matlab®
This program has been tested, it works perfectly without any errors or warnings.
Last modified: 12:55 AM on 12/10/1997