user466534
user466534

Reputation:

creating rational class constructor in matlab

suppose we have following class,i want to declare rational number class in matlab,i am beginner of object oriented programming in matlab languages and i want to learn basics

classdef ratnum %rational number class
    properties (Access=protected)
        n %numerator
        d %denomerator
    end
   methods
       function r=ratnum(numerator,denomenator)
           r.n=numerator;
           r.d=denomenator;
       end
   end


end

how can i create constructor with specific values in matlab main part?should i use name of class ?thanks in advance

Upvotes: 0

Views: 177

Answers (2)

Dev-iL
Dev-iL

Reputation: 24169

To instantiate an object of this class you can use: num1 = ratnum(2,3)

Since MATLAB doesn't have method overloading that is based on the amount of passed inputs, you could use nargin to select the correct scenario, as follows:

classdef ratnum %rational number class
    properties (Access=protected)
        n %//numerator
        d %//denominator
    end
   methods
       function r = ratnum(numerator,denominator)
           switch nargin
               case 2           
               r.n=numerator;
               r.d=denominator;
               case 0
               %//whatever you want the defaults to be
           end
       end

   end

end

A simple debug trick is to do num1_str=struct(num1) which allows you to see the contents of the object. However, you should create some public methods to get the values (instead of turning the object to a struct every time).


To overload the default summation of MATLAB you need to understand that whenever you write a+b it is automatically translated into plus(a,b). When defining custom classes with custom summation you should make a folder that has the name @classname (in your case @ratnum) and in it:

  • ratnum.m: the class definition file (which is the code you wrote)
  • a file named plus.m which looks something like:

.

function sum = plus(ratnum1,ratnum2)
    ratnum1 = ratnum(ratnum1);
    ratnum2 = ratnum(ratnum2);

    sum = (...
          ratnum1.r*ratnum2.d + ...
          ratnum2.r*ratnum1.d )/ ...
          (ratnum1.d * ratnum2.d);
end

Then, when you use + to add ratnums it will use the correct plus function.

Here's some helpful reading: MATLAB Documntation: Implementing Operators for Your Class


To call class methods, even from within other class methods, you must always write the class name first: ratnum.sum(ratnum1). Here's an example:

classdef ratnum %rational number class
    properties (Access=public)
        n %//numerator
        d %//denominator
    end
   methods (Access = public)
       function r = ratnum(numerator,denominator)
           switch nargin
               case 2           
               r.n=numerator;
               r.d=denominator;
               case 0
               %whatever you want the defaults to be
           end
       end

   end

   methods (Access = public, Static)
      function out = sum(ratnum) 
        out = ratnum.n + ratnum.d;
      end      
   end
end

then:

>> a = ratnum(1,1)

a = 

  ratnum with properties:

    n: 1
    d: 1

>> ratnum.sum(a)

ans =

     2

Upvotes: 1

CitizenInsane
CitizenInsane

Reputation: 4855

If you want to overload constructor with many different forms you'll have to initialize missing parameters with default value (or use varargin for more complex overloads):

function [r] = ratnum(num, den, varargin)

    % Showing initializing missing parameters
    if (nargin < 2), den = 42; end
    if (nargin < 1), num = 7; end

    % Showing working with varargin
    if (nargin == 3)
        ...
    elseif((nargin > 1) && (ischar(varargin{1}))
        ...
    else
        ...
    end

end

If you want to create named initializers for clarification of their meaning, you'll have to do it with Static methods:

methods (Static)
    function [r] = TwoThird()
       r = ratnum(2, 3);
    end
    function [r] = Half()
       r = ratnum(1, 2);
    end
end

Which can be used like this:

dummy = ratnum.Half(); % Ratio is 1/2
dummy = ratnum.TwoThird(); % Ratio is 2/3
dummy = ratnum(42, 666); % Ratio can be any custom one

Upvotes: 0

Related Questions