In this

**matlab object oriented programming tutorial**we explain what is function and operator overloading in Matlab and how it works with examples of each.
Function Overloading and Operator Overloading is a general technique in object oriented programming languages such as C++ and not just Matlab programming language. Overloading a function or operator means to extend or modify the function of the function or function of the operator.

For example, in matlab the disp() is a inbuilt function used for displaying text and data. But we can redefine what disp() function does by using overloading OOP functionality in Matlab. Since disp is a function this overloading technique is called function overloading. We can change the disp() function to do addition or subtraction or any mathematical operation or way of displaying the text and data and so on.

Similarly the + operator is used to add two numbers. But by using object oriented programming one can redefine what the operator + does. For example you can change it so that it does division or multiplication instead of addition. Because of this the + operator is said to be overloaded. As you perhaps know plus() is a function that does the same thing as + operator. So you can redefine plus() function. This is shown down below with example code.

Similarly the + operator is used to add two numbers. But by using object oriented programming one can redefine what the operator + does. For example you can change it so that it does division or multiplication instead of addition. Because of this the + operator is said to be overloaded. As you perhaps know plus() is a function that does the same thing as + operator. So you can redefine plus() function. This is shown down below with example code.

But how will Matlab know whether to use the inbuilt disp() function or the user defined through class method disp() function? Matlab can distinguish which disp() is being referred by looking at the arguments.

Before we show the examples you might want to read the preceding Matlab Object Oriented Programming tutorials.

Now lets see how the function and operator overloading in Matlab OOP is done with examples.

Before we show the examples you might want to read the preceding Matlab Object Oriented Programming tutorials.

Now lets see how the function and operator overloading in Matlab OOP is done with examples.

### Example of Function Overloading in Matlab

As aforementioned disp() is inbuilt function in Matlab Software. In the following we illustrate how the disp function can be overloaded such that it does something else.

#### Example 1

The first example shows how to change the disp() function so that it subtracts two numbers instead of displaying some text and data.Consider the following user defined class called subtractor.

classdef subtractor properties x, y; end methods function obj = subtractor(val1, val2) if nargin > 0 obj.x = val1; obj.y = val2; end end function disp(obj) z = obj.x - obj.y; fprintf('z = %0.2f \n', z); end end end

The class substractor has two properties x and y. It has two methods- the first one is the constructor and the second one is the user defined disp method. The disp method instead of displaying text/data is used to subtract x and y.

The following shows this.

>> u = subtractor; >> u.x = 10; >> u.y = 5; >> u.disp z = 5.00

The u is an object of class subtractor. We assign value of 10 and 5 to x and y properties. Then use the disp method to subtract y from x. The result is z = 5.

How to verify that disp() has indeed changed. Using the disp() function as normally we would we can see that disp() will display the text. See below.

>> disp('it works!') it works!

whereas,

>> disp(u) z = 5.00

Hence the user defined overloaded disp and the matlab disp are completely different.

#### Example 2

Consider the following code we have redefined the disp() function for display of text and data but different from the matlab inbuilt disp function.

classdef player properties name; age; end methods function pObj = player(var1, var2) if nargin == 2 pObj.name = var1; pObj.age = var2; end end function [] = disp(pObj) if nargin > 0 fprintf('Name = %s \n', pObj.name); fprintf('Age = %d \n', pObj.age); end end end end

In the methods section you can see a disp function or method being declared as one of two player class method. The first method is the class constructor. The properties name and age are uninitialized. Now you can see now the object is passed to the disp method and used to display Name and Age properties of the object that will be created.

Now we create an object player1 of the class player. Then we use the user defined class person method disp to display the name and age properties of the player1 object. See this in the following code.

First we created an object player1 of class player. Then we assigned properties name and age to the new object player1. Then we used the new disp user defined method for class player to display the name and age properties.

Similarly we can overload operators in Matlab software. To illustrate this we will overload the + operator. Internally, when the Matlab software encounters the + plus operator it invokes the plus.m matlab file sending it the arguments to be added. This means overloading the + operator means to change the functionality of the plus.m file. We will overload the + operator by declaring a plus method that does subtraction instead of normal addition.

See the following code.

As you can see in the methods section we have user defined plus method which is equivalent to the + operator. This plus method changes the functionalationly of the + operator. Hence is an example of operator overloading.

See the result of using this code:

So in this way you can overload functions and operators in Matlab.

Now we create an object player1 of the class player. Then we use the user defined class person method disp to display the name and age properties of the player1 object. See this in the following code.

>> player1 = player; >> player1.name = 'jon'; >> player1.age = 20; >> player1.disp Name = jon Age = 20

First we created an object player1 of class player. Then we assigned properties name and age to the new object player1. Then we used the new disp user defined method for class player to display the name and age properties.

### Example of Operator Overloading in Matlab

Similarly we can overload operators in Matlab software. To illustrate this we will overload the + operator. Internally, when the Matlab software encounters the + plus operator it invokes the plus.m matlab file sending it the arguments to be added. This means overloading the + operator means to change the functionality of the plus.m file. We will overload the + operator by declaring a plus method that does subtraction instead of normal addition.

See the following code.

classdef subtractor properties x, y; end methods function obj = subtractor(val1, val2) if nargin > 0 obj.x = val1; obj.y = val2; end end function plus(obj) z = obj.x - obj.y; fprintf('z = %0.2f\n', z); end end end

As you can see in the methods section we have user defined plus method which is equivalent to the + operator. This plus method changes the functionalationly of the + operator. Hence is an example of operator overloading.

See the result of using this code:

>> v = subtractor; >> v.x = 20; >> v.y = 9; >> v.plus z = 11.00

So in this way you can overload functions and operators in Matlab.

## No Comment to " Function and Operator Overloading in Matlab OOP - Matlab OOP 9 "