|
Overloading a Procedure
|
|
|
A program involves a great deal of names that
represent variables and procedures of various kinds. The compiler does not
allow two variables to have the same name in the same procedure (or in the
same scope). Although
two procedures should have unique names in the same program, you are
allowed
to use the same name for different procedures of the same program following
certain rules.
|
The ability to have various procedures with the same name in
the same program is referred to as overloading. The most important rule
about procedure overloading is to make sure that each one of these
procedures has a different number or different type(s) of arguments.
Practical
Learning: Overloading a Procedure
|
|
The moment of inertia is the ability of a beam to
resist bending. It is calculated with regard to the cross section of the
beam. Because it depends on the type of section of the beam, its
calculation also depends on the type of section of the beam. In this
exercise, we will review different formulas used to calculate the moment
of inertia. Since this exercise is for demonstration purposes, you do not
need to be a Science Engineering major to understand it.
- Start Microsoft Visual Basic and create a Console Application named
MomentOfInertia1
- In the Solution Explorer, right-click Module1.vb and click Rename
- Type MomentOfInertia.vb and press Enter
- To calculate the moment of inertia of a rectangle, change the file
as follows:
Module MomentOfInertia
' Moment of Inertia
' Rectangle
Private Function MomentOfInertia(ByVal b As Double, _
ByVal h As Double) As Double
Return b * h * h * h / 3
End Function
Public Sub Main()
Dim Base As Double, Height As Double
Base = InputBox("Enter the base of the Rectangle")
Height = InputBox("Enter the height of the Rectangle")
MsgBox("Moment of inertia with regard to the X axis" & vbCrLf & _
"I = " & CStr(MomentOfInertia(Base, Height)) & "mm")
End Sub
End Module
|
- Execute the application
- Enter the base as 3.25
- Enter the height as 2.85
- Close the DOS window and return to your programming environment
- Here are the formulas to calculate the moment of
inertia for a semi-circle:
A circle, and thus a semi-circle, requires only a radius. Since the
other version of the MomentOfInertia() function requires two arguments, we
can overload it by providing only one argument, the radius. |
To calculate the moment of inertia of a rectangle, change the file
as follows:
Module MomentOfInertia
' Moment of Inertia
' Rectangle
Private Function MomentOfInertia(ByVal b As Double, _
ByVal h As Double) As Double
Return b * h * h * h / 3
End Function
' Semi-Circle
Function MomentOfInertia(ByVal R As Double) As Double
Const PI As Double = 3.14159
Return R * R * R * R * PI / 8
End Function
Public Sub Main()
' Dim Base As Double, Height As Double
Dim Radius As Double
' Base = InputBox("Enter the base of the Rectangle:")
' Height = InputBox("Enter the height of the Rectangle")
' MsgBox("Moment of inertia with regard to the X axis" & vbCrLf & _
' "I = " & CStr(MomentOfInertia(Base, Height)) & "mm")
Radius = InputBox("Enter the radius of the semi-circle:")
MsgBox("Moment of inertia of a semi-circle with " & _
"regard to the X axis:" & vbCrLf & _
"I = " & CStr(MomentOfInertia(Radius)) & "mm")
End Sub
End Module
|
- Execute the application
- Enter the radius as 6.35
- Close the DOS window and return to your programming environment
|
|