Was > Old McDonnald Had A Farm E - I - E - I _ O!
Probably the single most used example in textbooks that teach OOP is something having to do with animals. For example, the term animal is a rather general term. However, there are some characteristics that all animal have such as ability to move and eat. If you become more specific and consider for example horses or sheep, their behaviors are more specific in terms of how they move and what they eat. Below is a class 'Animal' which is further specialized with additional inheriting classes 'horse' and 'sheep'. These latter classes inherit and override the behaviors of 'animal'.
Below is the same program In PowerBASIC Windows 9.0
Here is the vb.net program along with command line compilation string that I'm trying to translate. Not sure of everything yet. Just feeling my way! I should give propper credit for the below program that I implemented in the above two PowerBASIC programs. I got the idea from a book I have by Mike Gunderloy on VB.NET and ADO.NET. In that book he used Visual Studio to create a Windows program where the output went into a listbox. I redid the program somewhat but just did command line compilation using Notepad to write the program in VB.NET. Below is the program. If you have the .NET runtime on your machine (you probably do) you can compile this at the command prompt. The vb.net compiler is vbc.exe and your PATHs are probably set to its location. Change vbAnimal.txt to Animal.vb.
Probably the single most used example in textbooks that teach OOP is something having to do with animals. For example, the term animal is a rather general term. However, there are some characteristics that all animal have such as ability to move and eat. If you become more specific and consider for example horses or sheep, their behaviors are more specific in terms of how they move and what they eat. Below is a class 'Animal' which is further specialized with additional inheriting classes 'horse' and 'sheep'. These latter classes inherit and override the behaviors of 'animal'.
Code:
#Compile Exe '''Console Compiler 5.0 #Dim All Interface ICare : Inherit IUnknown 'According to Mr. Zale, an interface is a "set of methods Method Groom() As String 'and properties which are implemented on an object. You Method Deworm() As String 'might think of it as a list of DECLARE statements where End Interface 'the sequence of the Declares must be preserved. Remember, 'the interface is just the definition, not the actual code. '...An interesting note is that one particular interface Class Animal 'definition may become a part of several different classes Instance m_strName As String 'and objects. In fact, the internal code for an interface 'in CLASS A may be entirely different from the internal code Interface IAnimal : Inherit IUnknown 'for the same interface in CLASS B. Method names, para- Property Get ItsName() As String 'meters, and return values must be identical, but the Property=m_strName 'internal code could vary significantly". End Property Property Set ItsName(Byval strName As String) m_strName=strName End Property 'Note that this class 'Animal' is a base class some of whose 'Methods are overridden in derived classes. While in this Method Move() As String 'case the methods of this class are useful in and of Method = m_strName & " Walked Across The Farm." End Method 'themselves, that is not always the case. However, it can 'be useful to have a base class to provide default behavior Method Eat() As String 'if a method is not implemented in a derived class. In this Method = m_strName & " Ate." 'program the MakeSound() Method is implemented in the "Horse' End Method 'class but not in the Sheep class. Note the effect in the 'output below. Method MakeSound() As String Method="A Girrrrrh Animal Sound!" End Method End Interface End Class Class Horse Interface IHorse : Inherit Animal, IAnimal 'This class - Horse, as well as the Override Property Set ItsName(Byval strName As String) 'Sheep class implement the interface MyBase.ItsName="The Horse" 'ICare. The implementation is End Property 'different here though because one 'cares for a horse somewhat differently Override Method Eat() As String 'than for a sheep. Note the use of Method=Me.ItsName & " Ate Some Oats." 'what is termed an implicit class End Method 'pointer - Me. In OOP C family 'languages you'll see the use of the Override Method MakeSound() As String 'term 'this'. Its usually optional Method=Me.ItsName & " Made A Whinny Sound!" 'to use it but to make the code End Method 'clearer I tend to use it in places End Interface 'I really don't need t. Interface ICare : Inherit IUnknown 'Note that this code won't compile Method Groom() As String 'without the 'Override' term in front Method="You Brush The Horse." 'of the three overridden Methods. You End Method 'don't have to provide an override for 'all the methods in the base class. Method Deworm() As String 'Here though, we overrode the Annimal Method="You Give The Horse A Dose Of Dewormer." 'MakeSound() Method. End Method End Interface End Class Class Sheep Interface ISheep : Inherit Animal, IAnimal 'Note that in this 'Sheep' class we Override Property Set ItsName(Byval strName As String) 'didn't provide a unique and specific MyBase.ItsName="The Sheep" 'Override for the base class MakeSound() End Property 'Method, so if MakeSound() is called on 'a Sheep object the base class method Override Method Eat() As String 'will be called (see output below). Method=MyBase.ItsName & " Ate Some Grass." End Method End Interface Interface ICare : Inherit IUnknown Method Groom() As String Method="You Shear The Sheep." End Method Method Deworm() As String Method="You Give The Sheep A Dose Of Dewormer." End Method End Interface End Class Function PBMain() Local a_animal As IAnimal Local a_horse As IHorse, horse_care As ICare Local a_sheep As ISheep, sheep_care As ICare Let a_animal = Class "Animal" a_animal.ItsName="The Animal" Print a_animal.ItsName Print a_animal.Move() Print a_animal.Eat() Print a_animal.MakeSound() Print Let a_horse = Class "Horse" : Let horse_care = Class "Horse" a_horse.ItsName="The Horse" Print a_horse.ItsName Print a_horse.Move() Print a_horse.Eat() Print a_horse.MakeSound() Print horse_care.Groom() Print horse_care.Deworm() Print Let a_sheep = Class "Sheep" : Let sheep_care = Class "Sheep" a_sheep.ItsName="The Sheep" Print a_sheep.ItsName Print a_sheep.Move() Print a_sheep.Eat() Print a_sheep.MakeSound() Print sheep_care.Groom() Print sheep_care.Deworm() Waitkey$ PBMain=0 End Function 'Output: '======================================== 'The Animal 'The Animal Walked Across The Farm. 'The Animal Ate. 'A Girrrrrh Animal Sound! ' 'The Horse 'The Horse Walked Across The Farm. 'The Horse Ate Some Oats. 'The Horse Made A Whinny Sound! 'You Brush The Horse. 'You Give The Horse A Dose Of Dewormer. ' 'The Sheep 'The Sheep Walked Across The Farm. 'The Sheep Ate Some Grass. 'A Girrrrrh Animal Sound! 'You Shear The Sheep. 'You Give The Sheep A Dose Of Dewormer.
Code:
#Compile Exe 'PowerBASIC Windows Compiler 9.0 #Include "Win32api.inc" Type WndEventArgs wParam As Long lParam As Long hWnd As Dword hInst As Dword End Type Declare Function FnPtr(wea As WndEventArgs) As Long Type MessageHandler wMessage As Long dwFnPtr As Dword End Type Global MsgHdlr() As MessageHandler Interface ICare : Inherit IUnknown 'According to Mr. Zale, an interface is a "set of methods Method Groom() As String 'and properties which are implemented on an object. You Method Deworm() As String 'might think of it as a list of DECLARE statements where End Interface 'the sequence of the Declares must be preserved. Remember, 'the interface is just the definition, not the actual code. '...An interesting note is that one particular interface Class Animal 'definition may become a part of several different classes Instance m_strName As String 'and objects. In fact, the internal code for an interface 'in CLASS A may be entirely different from the internal code Interface IAnimal : Inherit IUnknown 'for the same interface in CLASS B. Method names, para- Property Get ItsName() As String 'meters, and return values must be identical, but the Property=m_strName 'internal code could vary significantly". End Property Property Set ItsName(Byval strName As String) m_strName=strName End Property 'Note that this class 'Animal' is a base class some of whose 'Methods are overridden in derived classes. While in this Method Move() As String 'case the methods of this class are useful in and of Method = m_strName & " Walked Across The Farm." End Method 'themselves, that is not always the case. However, it can 'be useful to have a base class to provide default behavior Method Eat() As String 'if a method is not implemented in a derived class. In this Method = m_strName & " Ate." 'program the MakeSound() Method is implemented in the "Horse' End Method 'class but not in the Sheep class. Note the effect in the 'output below. Method MakeSound() As String Method="A Girrrrrh Animal Sound!" End Method End Interface End Class Class Horse Interface IHorse : Inherit Animal, IAnimal 'This class - Horse, as well as the Override Property Set ItsName(Byval strName As String) 'Sheep class implement the interface MyBase.ItsName="The Horse" 'ICare. The implementation is End Property 'different here though because one 'cares for a horse somewhat differently Override Method Eat() As String 'than for a sheep. Note the use of Method=Me.ItsName & " Ate Some Oats." 'what is termed an implicit class End Method 'pointer - Me. In OOP C family 'languages you'll see the use of the Override Method MakeSound() As String 'term 'this'. Its usually optional Method=Me.ItsName & " Made A Whinny Sound!" 'to use it but to make the code End Method 'clearer I tend to use it in places End Interface 'I really don't need t. Interface ICare : Inherit IUnknown 'Note that this code won't compile Method Groom() As String 'without the 'Override' term in front Method="You Brush The Horse." 'of the three overridden Methods. You End Method 'don't have to provide an override for 'all the methods in the base class. Method Deworm() As String 'Here though, we overrode the Annimal Method="You Give The Horse A Dose Of Dewormer." 'MakeSound() Method. End Method End Interface End Class Class Sheep Interface ISheep : Inherit Animal, IAnimal 'Note that in this 'Sheep' class we Override Property Set ItsName(Byval strName As String) 'didn't provide a unique and specific MyBase.ItsName="The Sheep" 'Override for the base class MakeSound() End Property 'Method, so if MakeSound() is called on 'a Sheep object the base class method Override Method Eat() As String 'will be called (see output below). Method=MyBase.ItsName & " Ate Some Grass." End Method End Interface Interface ICare : Inherit IUnknown Method Groom() As String Method="You Shear The Sheep." End Method Method Deworm() As String Method="You Give The Sheep A Dose Of Dewormer." End Method End Interface End Class Function fnWndProc_OnPaint(pea As WndEventArgs) As Long Local a_animal As IAnimal Local a_horse As IHorse, horse_care As ICare Local a_sheep As ISheep, sheep_care As ICare Local lpPaint As PAINTSTRUCT Local hDC As Long hDC=BeginPaint(pea.hWnd,lpPaint) Let a_animal = Class "Animal" a_animal.ItsName="The Animal" Call TextOut(hDC,0,0,a_animal.ItsName,Len(a_animal.ItsName)) Call TextOut(hDC,0,15,a_animal.Move(),Len(a_animal.Move())) Call TextOut(hDC,0,30,a_animal.Eat(),Len(a_animal.Eat())) Call TextOut(hDC,0,45,a_animal.MakeSound(),Len(a_animal.MakeSound())) Let a_horse = Class "Horse" : Let horse_care = Class "Horse" a_horse.ItsName="The Horse" Call TextOut(hDC,0,75,a_horse.ItsName,Len(a_horse.ItsName)) Call TextOut(hDC,0,90,a_horse.Move(),Len(a_horse.Move())) Call TextOut(hDC,0,105,a_horse.Eat(),Len(a_horse.Eat())) Call TextOut(hDC,0,120,a_horse.MakeSound(),Len(a_horse.MakeSound())) Call TextOut(hDC,0,135,horse_care.Groom(),Len(horse_care.Groom())) Call TextOut(hDC,0,150,horse_care.Deworm(),Len(horse_care.Deworm())) Let a_sheep = Class "Sheep" : Let sheep_care = Class "Sheep" a_sheep.ItsName="The Sheep" Call TextOut(hDC,0,180,a_sheep.ItsName,Len(a_sheep.ItsName)) Call TextOut(hDC,0,195,a_sheep.Move(),Len(a_sheep.Move())) Call TextOut(hDC,0,210,a_sheep.Eat(),Len(a_sheep.Eat())) Call TextOut(hDC,0,225,a_sheep.MakeSound(),Len(a_sheep.MakeSound())) Call TextOut(hDC,0,240,sheep_care.Groom(),Len(sheep_care.Groom())) Call TextOut(hDC,0,255,sheep_care.Deworm(),Len(sheep_care.Deworm())) Call EndPaint(pea.hWnd,lpPaint) fnWndProc_OnPaint=0 End Function Function fnWndProc_OnClose(wea As WndEventArgs) As Long Call PostQuitMessage(0) fnWndProc_OnClose=0 End Function Function fnWndProc(ByVal hWnd As Long,ByVal wMsg As Long,ByVal wParam As Long,ByVal lParam As Long) As Long Register i As Long For i=0 To 1 If wMsg=MsgHdlr(i).wMessage Then Local wea As WndEventArgs Local iReturn As Long wea.hWnd=hWnd: wea.wParam=wParam: wea.lParam=lParam Call Dword MsgHdlr(i).dwFnPtr Using FnPtr(wea) To iReturn fnWndProc=iReturn Exit Function End If Next i fnWndProc=DefWindowProc(hWnd,wMsg,wParam,lParam) End Function Sub AttachMessageHandlers() ReDim MsgHdlr(1) As MessageHandler 'Associate Windows Message With Message Handlers MsgHdlr(0).wMessage=%WM_PAINT : MsgHdlr(0).dwFnPtr=CodePtr(fnWndProc_OnPaint) MsgHdlr(1).wMessage=%WM_CLOSE : MsgHdlr(1).dwFnPtr=CodePtr(fnWndProc_OnClose) End Sub Function WinMain(ByVal hIns As Long,ByVal hPrev As Long,ByVal lpCL As Asciiz Ptr,ByVal iShow As Long) As Long Local hWnd As Dword Local winclass As WndClassEx Local szAppName As Asciiz * 16 Local Msg As tagMsg szAppName="Animals" Call AttachMessageHandlers() winclass.cbSize=SizeOf(winclass) winclass.style=%CS_HREDRAW Or %CS_VREDRAW winclass.lpfnWndProc=CodePtr(fnWndProc) winclass.cbClsExtra=0 winclass.cbWndExtra=0 winclass.hInstance=hIns winclass.hIcon=LoadIcon(%NULL, ByVal %IDI_APPLICATION) winclass.hCursor=LoadCursor(%NULL, ByVal %IDC_ARROW) winclass.hbrBackground=GetStockObject(%WHITE_BRUSH) winclass.lpszMenuName=%NULL winclass.lpszClassName=VarPtr(szAppName) winclass.hIconSm=LoadIcon(hIns, ByVal %IDI_APPLICATION) RegisterClassEx winclass hWnd=CreateWindowEx(0,szAppName,"Old McDonnald Had A Farm...",%WS_OVERLAPPEDWINDOW,200,100,345,320,0,0,hIns,ByVal 0) Call ShowWindow(hWnd,iShow) While GetMessage(Msg,%NULL,0,0) TranslateMessage Msg DispatchMessage Msg Wend Function=msg.wParam End Function
Code:
'vb.net program. this is a GUI output program. Below is command line compile 'string for anyone with .NET runtime installed. just open command line window and paste the string in starting with 'vbc' 'vbc /r:System.dll,System.Drawing.dll,System.Windows.Forms.dll /t:winexe Animal.vb Imports System Imports System.Drawing Imports System.Windows.Forms Interface Care Function Groom() As String Function Deworm() As String End Interface Public Class Animal Private mstrName As String Public Property Name() As String Get Name = mstrName End Get Set(ByVal Value As String) mstrName = Value End Set End Property Public Function Move() As String Move = mstrName & " Walked Across The Farm." End Function Overridable Function Eat() As String Eat = mstrName & " Ate." End Function End Class Public Class Horse Inherits Animal Implements Care Sub New() MyBase.Name = "The Horse" End Sub Overrides Function Eat() As String Eat = MyBase.Name & " Ate Some Oats." End Function Function Groom() As String Implements Care.groom Groom = "You Brush The Horse." End Function Function Deworm() As String Implements Care.Deworm Deworm = "You Give The Horse A Dose Of Dewormer." End Function End Class Public Class Sheep Inherits Animal Implements Care Sub New() MyBase.Name = "The Sheep" End Sub Overrides Function Eat() As String Eat = MyBase.Name & " Ate Some Grass." End Function Function Groom() As String Implements Care.groom Groom = "You Shear The Sheep." End Function Function Deworm() As String Implements Care.Deworm Deworm = "You Give The Sheep A Dose Of Dewormer." End Function End Class Class frmMain Inherits Form Sub New() Me.Text = "A Walk About The Farm" Me.BackColor = SystemColors.Window Me.ForeColor = SystemColors.WindowText End Sub Protected Overrides Sub OnPaint(pea As PaintEventArgs) Dim a As New Animal Dim h As New Horse Dim s As New Sheep Dim br As Brush br = new SolidBrush(Me.ForeColor) a.Name="The Animal" pea.Graphics.DrawString(a.Name,Me.Font,br,0,0) 'animal pea.Graphics.DrawString(a.Move(),Me.Font,br,0,15) 'animal pea.Graphics.DrawString(a.Eat(),Me.Font,br,0,30) 'animal pea.Graphics.DrawString(h.Move(),Me.Font,br,0,60) 'horse pea.Graphics.DrawString(h.Eat(),Me.Font,br,0,75) 'horse pea.Graphics.DrawString(h.Groom(),Me.Font,br,0,90) 'horse pea.Graphics.DrawString(h.Deworm(),Me.Font,br,0,105) 'horse pea.Graphics.DrawString(s.Move(),Me.Font,br,0,135) 'sheep pea.Graphics.DrawString(s.Eat(),Me.Font,br,0,150) 'sheep pea.Graphics.DrawString(s.Groom(),Me.Font,br,0,165) 'sheep pea.Graphics.DrawString(s.Deworm(),Me.Font,br,0,180) 'sheep End Sub End Class Module MainModule Public Sub Main() Application.Run(new frmMain()) End Sub End Module
Comment