Add 2 Numbers in a class

87 views (last 30 days)
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.

Accepted Answer

Raheel Naveed
Raheel Naveed on 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 Comments
Govind Sankar Madhavan Pillai Ramachandran Nair
So I need to use a constructor. What if I need to use a normal function to add two numbers instead of a constructor. How do I do that? Thank you. Let me guess. Is it this :
obj = addnumbers(obj, Nr1, Nr2)
obj.value = Nr1+ Nr2;
end
Ok, I tried this way and I am getting solution as Add with no properties. So i am wrong.
Raheel Naveed
Raheel Naveed on 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.

Sign in to comment.

More Answers (2)

Steven Lord
Steven Lord on 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 Comment
Govind Sankar Madhavan Pillai Ramachandran Nair
Thank you, I will try to go through the page to understand MATLAB oop more. I know LABVIEW oop, C++ and C# OOP. But MATLAB OOP is new and so I need to understand that better.

Sign in to comment.


Yash
Yash on 9 Sep 2024
addtion of two number program3

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!