Home >>VB.Net Programs >VB.Net Program to Overload arithmetic operators

VB.Net Program to Overload arithmetic operators

Write a VB.Net Program to Overload arithmetic operators

Here, using the operator keyword, we can overload all arithmetic operations including plus (+), minus (-), multiplication (*), division (/), and remainder (Mod).

Program :

Below is the source code for overload the arithmetic operators. The program given is compiled and successfully executed.

'VB.net program to overload arithmetic operators.


Class Sample
    Dim num As Integer
    Sub SetValues(ByVal n As Integer)
        num = n
    End Sub
    Public Shared Operator +(ByVal S1 As Sample, ByVal S2 As Sample) As Sample
        Dim temp As New Sample()
        temp.num = S1.num + S2.num
        Return (temp)
    End Operator
    Public Shared Operator -(ByVal S1 As Sample, ByVal S2 As Sample) As Sample
        Dim temp As New Sample()
        temp.num = S1.num - S2.num
        Return (temp)
    End Operator
    Public Shared Operator *(ByVal S1 As Sample, ByVal S2 As Sample) As Sample
        Dim temp As New Sample()
        temp.num = S1.num * S2.num
        Return (temp)
    End Operator
    Public Shared Operator \(ByVal S1 As Sample, ByVal S2 As Sample) As Sample
        Dim temp As New Sample()
        temp.num = S1.num \ S2.num
        Return (temp)
    End Operator
    Public Shared Operator Mod(ByVal S1 As Sample, ByVal S2 As Sample) As Sample
        Dim temp As New Sample()
        temp.num = S1.num Mod S2.num
        Return (temp)
    End Operator
    Sub PrintValues()
        Console.WriteLine(vbTab & "Num: {0}", num)
    End Sub
End Class
Module Module1
    Sub Main()
        Dim obj1 As New Sample()
        Dim obj2 As New Sample()
        Dim obj3 As New Sample()
        obj1.SetValues(7)
        obj2.SetValues(2)
        Console.WriteLine("Obj1: ")
        obj1.PrintValues()
        Console.WriteLine("Obj2: ")
        obj2.PrintValues()
        obj3 = obj1 + obj2
        Console.WriteLine("Addition: ")
        obj3.PrintValues()
        obj3 = obj1 - obj2
        Console.WriteLine("Subtraction: ")
        obj3.PrintValues()
        obj3 = obj1 * obj2
        Console.WriteLine("Multiplication: ")
        obj3.PrintValues()
        obj3 = obj1 \ obj2
        Console.WriteLine("Division: ")
        obj3.PrintValues()
        obj3 = obj1 Mod obj2
        Console.WriteLine("Remainder: ")
        obj3.PrintValues()
    End Sub
End Module
	
Output:
Obj1:
Num: 7
Obj2:
Num: 2
Addition:
Num: 9
Subtraction:
Num: 5
Multiplication:
Num: 14
Division:
Num: 3
Remainder:
Num: 1
Explanation:

We have created a Sample class in the above program, which includes two SetValues(), PrintValues() methods to set and print the values of the data members of the class. Here, using operator keywords, we have introduced methods to overload all arithmetic operators.

After that, we built a Module1 module that contains the Main() method, and the program entry point is the Main() method. And, we created the two Sample Class objects and then all arithmetic operations are performed using overloaded operators and the result is written on the console screen.


No Sidebar ads