Introduction:
In this articals we will look how to use overloading binary operator in vb.net.operator overloading is one of the many existing features of object oriented programming.operator overloading gives us syntactical convenience,its also help us greatly generate more readable and intuitive code in a number of sistuations these include:
Mathematical or physical modeling where we use classes to represent object such as coordinates materices,vector and so on.
Financial programs where a class represents an amount of money.
Text manipulations where classes are used to represent string and sentences.
For Example:
This program overloads the binary plus operator to add two complex numbers of type:
x = a + jb
Module Module1
Class Complex
Private x As [Double]
Private y As [Double]
Public Sub New()
End Sub
Public Sub New(ByVal real As [Double], ByVal image As [Double])
x = real
y = image
End Sub
Public Shared Operator +(ByVal c1 As complex, ByVal c2 As complex) As complex
Dim c3 As New Complex()
c3.x = c1.x + c2.x
c3.y = c1.y + c2.y
Return (c3)
End Operator
Public Sub Display()
Console.Write(x)
Console.Write("+j" & Convert.ToString(y))
Console.WriteLine()
End Sub
End Class
Sub Main()
Dim a As Complex, b As Complex, c As Complex
a = New Complex(2.5, 3.5)
b = New Complex(1.6, 2.7)
c = a + b
Console.Write("a=")
a.Display()
Console.Write("b=")
b.Display()
Console.Write("c=")
c.Display()
End Sub
End Module


Join the conversation! Your thoughts help the community grow.