Passing an object from a method function to another function

I'm having trouble passing an object from a method function to another function.
I want to pass the obj.federaltax and obj.provincialTax to my calc_totalTax func to calcluate the totalTax but all i'm getting is an empty value for totalTax.
classdef calcTax
properties
income = input('enter annual income: ');
federalTax;
provincialTax;
totalTax;
end
function obj = calc_federalTax(obj)
if obj.income <= 46605
obj.federalTax = obj.income*15/100;
else
obj.federalTax = obj.income*15/100;
end
end
function obj = calc_provincialTax(obj)
if obj.income <= 42960
obj.provincialTax = obj.income*5.05/100;
else
obj.provincialTax = obj.income*9.15/100;
end
end
function obj = calc_totalTax(obj)
obj.totalTax = obj.provincialTax + obj.federalTax
disp(totalTax)
end
end
end

 Respuesta aceptada

Nowhere in the code did you call your federal and provincial tax calculation methods. There are at least two possible solutions:
  • Call those calculation methods in your total tax calculation method.
function obj = calc_totalTax(obj)
obj = calc_provincialTax(obj);
obj = calc_federalTax(obj);
obj.totalTax = obj.provincialTax + obj.federalTax
disp(totalTax)
end
  • Make the three tax-related properties Dependent properties whose values are computed when you ask for them using the value of the non-Dependent income property. See this documentation page for a short example. In the example the Balance property is not stored in the object; it's computed using the values of the DollarAmount and Current properties that are stored in the object.

2 comentarios

My bad I forgot to include my main program code -
o = calcTax;
o.calc_federalTax;
o.calc_provincialTax;
o.calc_totalTax;
That code executes the calc_federalTax method on the copy of the object o that was passed into it, updates the properties of that copy, then promptly throws that copy in the trash because you don't assign the output of the function call to a variable.
Now that I think about it a bit more carefully, I'm not sure it make sense to store the value of the calculated tax as part of the object. I'd probably do something like:
classdef calcTax
properties
income = input('enter annual income: ');
end
% I assume you intended to have a methods keyword here
% lined up with the next-to-last end keyword in this code
function calculatedTax = calc_federalTax(obj)
if obj.income <= 46605
calculatedTax = obj.income*15/100;
else
calculatedTax = obj.income*15/100;
end
end
function calculatedTax = calc_provincialTax(obj)
if obj.income <= 42960
calculatedTax = obj.income*5.05/100;
else
calculatedTax = obj.income*9.15/100;
end
end
function calculatedTax = calc_totalTax(obj)
calculatedTax = obj.calc_provincialTax + obj.calc_federalTax;
disp(calculatedTax)
end
end
end
Call it:
myTaxForm = calcTax();
myTotalTax = calc_totalTax(myTaxForm) % or
myTotalTax = myTaxForm.calc_totalTax;
If you do need to update the object, either call the calc_*Tax methods with an output or make it a handle class. Using your original class the former would be:
o = calcTax;
o = o.calc_federalTax;
o = o.calc_provincialTax;
o = o.calc_totalTax;
For the latter make it a handle class and eliminate the output arguments from each of your methods. Search the documentation for "handle class" for more information.
classdef calcTax < handle
...
function calc_federalTax(obj)
...
end
...
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Preguntada:

mzw
el 26 de Mzo. de 2019

Comentada:

el 26 de Mzo. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by