We are going to build a distance class here that includes feet and inches. We will add two distances here.Program :
Below is the source code for creating a class to add two distances. The program given is compiled and successfully executed.
'VB.Net program to create a class to
'read and add two distance.
Class Distance Private feet As Integer Private inch As Integer Sub SetDist(ByVal f As Integer, ByVal i As Integer) feet = f inch = i End Sub Sub PrintDist() Console.WriteLine(vbTab & "Feet: " & feet) Console.WriteLine(vbTab & "Inch: " & inch) End Sub Function AddDist(ByVal D As Distance) As Distance Dim temp As New Distance() temp.feet = feet + D.feet temp.inch = inch + D.inch If (temp.inch >= 12) Then temp.feet = temp.feet + 1 temp.inch = temp.inch - 12 End If AddDist = temp End Function End Class Module Module1 Sub Main() Dim D1 As New Distance() Dim D2 As New Distance() Dim D3 As New Distance() D1.SetDist(5, 7) D2.SetDist(6, 6) D3 = D1.AddDist(D2) Console.WriteLine("Distance1: ") D1.PrintDist() Console.WriteLine("Distance2: ") D2.PrintDist() Console.WriteLine("Distance3: ") D3.PrintDist() End Sub End Module
We created a Module1 module in the program above that includes the Main() function. Here, we created a distance class that contains two feet and inches of data members. We created three SetDist(), PrintDist(), and AddDist methods in the Distance class ().
To set the value of data members, the SetDist() method is used. To print the distance on the console screen, the PrintDist() method is used. The method AddDist() is used to add two distances and return the distance added to the calling function.
The function Main() is an entry point for the program. Here, we created three D1, D2, and D3 objects.
Then read User Distance and then use the AddDist() method to add distances and then print the distances on the console screen.