Example : Operator Overloading Examples in VB .Net program to show Binary Addition(+) Operator Overloading.
Class Example
Dim num1 As Integer
Dim num2 As Integer
Sub Input(ByVal val1 As Integer, ByVal val2 As Integer)
num1 = val1
num2 = val2
End Sub
Public Shared Operator +(ByVal Obj4 As Example, ByVal Obj5 As Example) As Example
Dim temp As New Example()
temp.num1 = Obj4.num1 + Obj5.num1
temp.num2 = Obj4.num2 + Obj5.num2
Return (temp)
End Operator
Sub Output()
Console.WriteLine(vbTab & “{0}”, num1)
Console.WriteLine(vbTab & “{0}”, num2)
End Sub
End Class
Module Module1
Sub Main()
Dim obj1 As New Example()
Dim obj2 As New Example()
Dim obj3 As New Example()
obj1.Input(100, 200)
obj2.Input(300, 400)
obj3 = obj1 + obj2
Console.WriteLine(“The Value of Object1 = “)
obj1.Output()
Console.WriteLine(“The Value of Object2 = “)
obj2.Output()
Console.WriteLine(“The Value of Object3 = “)
obj3.Output()
End Sub
End Module
Output:
The Value of Object1 =
100
200
The Value of Object2 =
300
400
The Value of Object3 =
400
600
Example : Operator Overloading Examples in VB .Net program to show Binary Subtraction(-) Operator Overloading.
Class Example
Dim num1 As Integer
Dim num2 As Integer
Sub Input(ByVal val1 As Integer, ByVal val2 As Integer)
num1 = val1
num2 = val2
End Sub
Public Shared Operator +(ByVal Obj4 As Example, ByVal Obj5 As Example) As Example
Dim temp As New Example()
temp.num1 = Obj4.num1 – Obj5.num1
temp.num2 = Obj4.num2 – Obj5.num2
Return (temp)
End Operator
Sub Output()
Console.WriteLine(vbTab & “{0}”, num1)
Console.WriteLine(vbTab & “{0}”, num2)
End Sub
End Class
Module Module1
Sub Main()
Dim obj1 As New Example()
Dim obj2 As New Example()
Dim obj3 As New Example()
obj1.Input(100, 200)
obj2.Input(300, 400)
obj3 = obj1 – obj2
Console.WriteLine(“The Value of Object1 = “)
obj1.Output()
Console.WriteLine(“The Value of Object2 = “)
obj2.Output()
Console.WriteLine(“The Value of Object3 = “)
obj3.Output()
End Sub
End Module
Output:
The Value of Object1 =
100
200
The Value of Object2 =
300
400
The Value of Object3 =
-200
-200
NB : Link for Operator Overloading Examples in C++.
0 Comments