Home >>VB.Net Built-in Functions >VB.Net Program to Overload exponential (^) Operator

VB.Net Program to Overload exponential (^) Operator

Write a VB.Net Program to Overload exponential (^) Operator

Here, to calculate the power of a given number, we can overload the exponential (^) operator using the operator method.

Program :

Below is the source code for overloading the exponential (^) operator. The program given is compiled and successfully executed.

'VB.net program to overload exponential "^" operator.


Class Sample
    Dim X As Integer
    Sub SetX(ByVal val As Integer)
        X = val
    End Sub
    Public Shared Operator ^(ByVal S1 As Sample, ByVal S2 As Sample) As Integer
        Dim temp As Integer
        temp = S1.X ^ S2.X
        Return temp
    End Operator
End Class
Module Module1
    Sub Main()
        Dim obj1 As New Sample()
        Dim obj2 As New Sample()
        Dim result As Integer
        obj1.SetX(10)
        obj2.SetX(3)
        result = obj1 ^ obj2
        Console.WriteLine("Result: {0}", result)
    End Sub
End Module
	
Output:
Result: 1000

Explanation:

In the above program, we have generated a Sample class that contains a SetX() method to set the value of members of the data. We have added a technique here to overload the exponential (^) operator.

After that, we built a Module1 module that contains the Main() method, and the program entry point is the Main() method. And, we generated two Sample Class objects and used the SetX() method to set the value of the data member and then perform an exponential operation between the objects.


No Sidebar ads