Add 2 Numbers in a class
88 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Govind Sankar Madhavan Pillai Ramachandran Nair
am 24 Jul. 2023
Beantwortet: Yash
am 9 Sep. 2024
So I am trying to learn OOP in MATLAB. I am a beginner in MATLAB and trying to learn stuff. So I decided to create this simple addition class where it has a function that takes in 2 inputs and returns the output as addition of two numbers. Before you answer this can be done as a simple function, yes I know that, but the objective is to learn OOP. So thats why I am creating a class. And I wrote this programm.
classdef Add
%ADD Summary of this class goes here
% Detailed explanation goes here
properties(Access = private)
Value
end
methods
function obj = addNumbers(Nr1,Nr2)
%ADD Construct an instance of this class
% Detailed explanation goes here
obj.Value = Nr1 + Nr2;
end
end
end
Then in the Command Window I created an object and tried to call the function addNumbers.
x = Add;
x.addNumbers(1,2);
But the output is
Error using Add/addNumbers
Too many input arguments.
So I havent understood OOP correctly. Could you help me understand where I am wrong and correct me. Thank you.
0 Kommentare
Akzeptierte Antwort
Raheel Naveed
am 24 Jul. 2023
The constructor function (the function that has the same name as the class) should have the same name as the class.
So, it should be Add instead of addNumbers.
Try replacing it as
classdef Add
properties(Access = private)
Value
end
methods
function obj = Add(Nr1, Nr2) % Changed
obj.Value = Nr1 + Nr2;
end
function val = getValue(obj)
val = obj.Value;
end
end
end
In Command Window, or other file you may use this commands to see the output
addObj = Add(3, 5)
sumValue = addObj.getValue();
% disp(sumValue); % Displays 8
disp('The sum is '+ string(sumValue));
3 Kommentare
Govind Sankar Madhavan Pillai Ramachandran Nair
am 25 Jul. 2023
Bearbeitet: Govind Sankar Madhavan Pillai Ramachandran Nair
am 25 Jul. 2023
Raheel Naveed
am 25 Jul. 2023
Using a Construtor for general purpose function (like sum,power,mean etc) is more fast and effective.
Because, it does not require intialization as other functions. (I hope you know this before)
Below is the code for function other than this.
classdef Add
properties(Access = private)
Value
end
methods
function obj=addNumbers(obj,Nr1,Nr2)
obj.Value=Nr1+Nr2;
end
function val = getValue(obj)
val = obj.Value;
end
end
end
In Command Window
x=Add();
x=x.addNumbers(3,5);
sumValue=x.getValue();
disp(sumValue)
I wish you all the best in your journey of mastering OOPs.
Weitere Antworten (2)
Steven Lord
am 24 Jul. 2023
This syntax:
x = Add;
x.addNumbers(1,2);
does not call the addNumbers method of the Add class with two inputs. It calls that method with three inputs. It is almost always equivalent to the following call (and the cases where they can differ are a bit of an advanced maneuver and aren't relevant for this discussion.)
addNumbers(x, 1, 2)
If I were to write up a class to add two numbers, rather than having the class be just effectively a delegator to a method (that has little or no interaction with the class itself) I'd write the class to represent a number. In fact the documentation has at least a few examples of this type of class. I'd recommend you read through the BasicClass example on this documentation page. Note that this class doesn't do everything you might expect; for example it would be nice to be able to add 1 and a BasicClass object without having the user explicitly convert 1 into a BasicClass object. The polynomial example on this documentation page talks about how to enable that type of mixed arithmetic operation to work via a small amount of extra work on the part of the class author.
1 Kommentar
Siehe auch
Kategorien
Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!