Assignment No. – 03
Program on Class & Object :-
Public MustInherit Class Class1
Public n As Integer
Sub New()
Console.WriteLine("Inside Abstract CLass1 Constructor...")
Console.Write("Enter a number = ")
n = CInt(Console.ReadLine)
End Sub
MustOverride Sub display()
End Class
Public Class Class2 : Inherits Class1
Public Overrides Sub display()
Console.WriteLine("n= " + n.ToString())
End Sub
End Class
Module AbstractClassExample
Sub Main()
Dim obj As Class1
Dim obj1 As New Class2
obj = obj1
obj.display()
Console.ReadLine()
End Sub
End Module
Output :-
Inside Abstract CLass1 Constructor...
Enter a number = 2
n=2
Program on Inheritance :-
' Base class
Class Rectangle
Protected width As Double
Protected length As Double
Public Sub New(ByVal l As Double, ByVal w As Double)
length = l
width = w
End Sub
Public Function GetArea() As Double
Return (width * length)
End Function
Public Overridable Sub Display()
Console.WriteLine("Length: {0}", length)
Console.WriteLine("Width: {0}", width)
Console.WriteLine("Area: {0}", GetArea())
End Sub
'end class Rectangle
End Class
'Derived class
Class Tabletop : Inherits Rectangle
Private cost As Double
Public Sub New(ByVal l As Double, ByVal w As Double)
MyBase.New(l, w)
End Sub
Public Function GetCost() As Double
Dim cost As Double
cost = GetArea() * 70
Return cost
End Function
Public Overrides Sub Display()
MyBase.Display()
Console.WriteLine("Cost: {0}", GetCost())
End Sub
'end class Tabletop
End Class
Class RectangleTester
Shared Sub Main()
Dim t As Tabletop = New Tabletop(4.5, 7.5)
t.Display()
Console.ReadKey()
End Sub
End Class
Output :-
Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5
No comments:
Post a Comment