Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Old McDonnald Had A Farm E - I - E - I _ O!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Old McDonnald Had A Farm E - I - E - I _ O!

    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'.

    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.
    Below is the same program In PowerBASIC Windows 9.0

    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
    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.

    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
    Attached Files
    Last edited by Fred Harris; 15 Aug 2008, 10:22 PM. Reason: Add Considerable Stuff To Program Now That I'm A Master Of PowerObjects with 4 Hours of Experience!
    Fred
    "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

    #2
    Technically, animals don't have to move.

    I believe the true defining characteristic is that they don't make their own food. They have to get it from the environment (something other than light).

    <smile>

    JS
    John,
    --------------------------------
    John Strasser
    Phone: 480 - 273 - 8798

    Comment


      #3
      Some Animals Don't Move!

      Technically, animals don't have to move.
      I stand corrected, and shall defer to your superior taxonomic knowledge!

      I’d think an animal that didn’t move would be sort of a protoplasmic blob. Therefore, I’ve injected a base class of ‘Blob’ before all the others from which ‘MovableAnimal’, “Horse’, and ‘Sheep’ inherit. The output from the modified program is as follows…

      Code:
      An Immovable Blob
      I Can't Move!  I'm Just An An Immovable Blob!
      Bring Me Something To Eat!  I Can't Move!  I'm Just An Immovable Blob!
      I'm A Talking Immovable Blob!
      
      A Movable Animal
      Since I'm A Movable Animal, I Can Scurry/Slither/Fly About(as the case may be) In Search Of Food!
      A Movable Animal Moved About And Ate Whatever It Is That Movable Animals Eat!
      A Movable Animal Made a Moving Animal Sound
      
      A Running Horse
      A Running Horse Can Run Across The Prairie!
      A Running Horse Ate Some Oats As It Moved About Across The Prairie.
      A Running Horse Made A Whinny Sound As It Ran!
      You Brush The Horse.
      You Give The Horse A Dose Of Dewormer.
      
      The Sheep
      The Sheep Can Amble About Eating Grass!
      The Sheep Ate Some Grass.
      The Sheep Went Baaaaaah!
      You Shear The Sheep.
      You Give The Sheep A Dose Of Dewormer.
      Here is the modified code...

      Code:
      #Compile Exe
      #Dim All
      
      Interface ICare : Inherit IUnknown
        Method Groom() As String
        Method Deworm() As String
      End Interface
      
      
      Class Blob
        Instance m_strName As String
      
        Interface IBlob : Inherit IUnknown
          Property Get ItsName() As String
            Property=m_strName
          End Property
      
          Property Set ItsName(Byval strName As String)
            m_strName=strName
          End Property
      
          Method Move() As String
            Method = "I Can't Move!  I'm Just An " & m_strName & "!"
          End Method
      
          Method Eat() As String
            Method = "Bring Me Something To Eat!  I Can't Move!" & "  I'm Just " & m_strName & "!"
          End Method
      
          Method MakeSound() As String
            Method="I'm A Talking Immovable Blob!"
          End Method
        End Interface
      End Class
      
      
      Class MovableAnimal
        Interface IMovableAnimal : Inherit Blob, IBlob
          Override Property Set ItsName(Byval strName As String)
            MyBase.ItsName=strName
          End Property
      
          Override Method Move() As String
            Method="Since I'm " & Me.ItsName() & ", I Can Scurry/Slither/Fly About" & _
            "(as the case may be) In Search Of Food!"
          End Method
      
          Override Method Eat() As String
             Method=Me.ItsName & " Moved About And Ate Whatever It Is That Movable Animals Eat!"
          End Method
      
          Override Method MakeSound() As String
            Method=Me.ItsName & " Made a Moving Animal Sound"
          End Method
        End Interface
      End Class
      
      
      Class Horse
        Interface IHorse : Inherit MovableAnimal, IMovableAnimal
          Override Property Set ItsName(Byval strName As String)
            MyBase.ItsName=strName
          End Property
      
          Override Method Move() As String
            Method=Me.ItsName() & " Can Run Across The Prairie!"
          End Method
      
          Override Method Eat() As String
             Method=Me.ItsName & " Ate Some Oats As It Moved About Across The Prairie."
          End Method
      
          Override Method MakeSound() As String
            Method=Me.ItsName & " Made A Whinny Sound As It Ran!"
          End Method
        End Interface
        
        Interface ICare : Inherit IUnknown
          Method Groom() As String
            Method="You Brush The Horse."
          End Method
                                                                  
          Method Deworm() As String
            Method="You Give The Horse A Dose Of Dewormer."
          End Method
        End Interface
      End Class
      
      
      Class Sheep
        Interface ISheep : Inherit MovableAnimal, IMovableAnimal
          Override Property Set ItsName(Byval strName As String)
            MyBase.ItsName=strName
          End Property
          
          Override Method Move() As String
            Method=Me.ItsName() & " Can Amble About Eating Grass!"
          End Method
                                                                                 
          Override Method Eat() As String
             Method=MyBase.ItsName & " Ate Some Grass."
          End Method
          
          Override Method MakeSound() As String
            Method=Me.ItsName & " Went Baaaaaah!"
          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() As Long
        Local a_blob As IBlob
        Local a_movable_animal As IMovableAnimal
        Local a_horse As IHorse, horse_care As ICare
        Local a_sheep As ISheep, sheep_care As ICare
        
        Console Set Screen 25,100
        Let a_blob = Class "Blob"
        a_blob.ItsName="An Immovable Blob"
        Print a_blob.ItsName()
        Print a_blob.Move()
        Print a_blob.Eat()
        Print a_blob.MakeSound()
        Print
      
        Let a_movable_animal = Class "MovableAnimal"
        a_movable_animal.ItsName="A Movable Animal"
        Print a_movable_animal.ItsName()
        Print a_movable_animal.Move()
        Print a_movable_animal.Eat()
        Print a_movable_animal.MakeSound()
        Print
      
        Let a_horse = Class "Horse"
        a_horse.ItsName="A Running Horse"
        Print a_horse.ItsName()
        Print a_horse.Move()
        Print a_horse.Eat()
        Print a_horse.MakeSound()
        Let horse_care = Class "Horse"
        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
      Fred
      "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

      Comment


        #4
        Modified for PBWIN10.04 works with J. Roca includes. Thank you Fred!

        Code:
        #COMPILE EXE
        #DIM ALL
        'Fred Harris on 18 AUG 2008
        'https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/39206-old-mcdonnald-had-a-farm-e-i-e-i-_-o?p=456128#post456128
        
        INTERFACE ICare : INHERIT IUNKNOWN
          METHOD Groom() AS STRING
          METHOD Deworm() AS STRING
        END INTERFACE
        
        
        CLASS Blob
          INSTANCE m_strName AS STRING
        
          INTERFACE IBlob : INHERIT IUNKNOWN
            PROPERTY GET ItsName() AS STRING
              PROPERTY=m_strName
            END PROPERTY
        
            PROPERTY SET ItsName(BYVAL strName AS STRING)
              m_strName=strName
            END PROPERTY
        
            METHOD Move() AS STRING
              METHOD = "I Can't Move!  I'm Just An " & m_strName & "!"
            END METHOD
        
            METHOD Eat() AS STRING
              METHOD = "Bring Me Something To Eat!  I Can't Move!" & "  I'm Just " & m_strName & "!"
            END METHOD
        
            METHOD MakeSound() AS STRING
              METHOD="I'm A Talking Immovable Blob!"
            END METHOD
          END INTERFACE
        END CLASS
        
        
        CLASS MovableAnimal
          INTERFACE IMovableAnimal : INHERIT Blob, IBlob
            OVERRIDE PROPERTY SET ItsName(BYVAL strName AS STRING)
              MyBase.ItsName=strName
            END PROPERTY
        
            OVERRIDE METHOD Move() AS STRING
              METHOD="Since I'm " & Me.ItsName() & ", I Can Scurry/Slither/Fly About" & _
              "(as the case may be) In Search Of Food!"
            END METHOD
        
            OVERRIDE METHOD Eat() AS STRING
               METHOD=Me.ItsName & " Moved About And Ate Whatever It Is That Movable Animals Eat!"
            END METHOD
        
            OVERRIDE METHOD MakeSound() AS STRING
              METHOD=Me.ItsName & " Made a Moving Animal Sound"
            END METHOD
          END INTERFACE
        END CLASS
        
        
        CLASS Horse
          INTERFACE IHorse : INHERIT MovableAnimal, IMovableAnimal
            OVERRIDE PROPERTY SET ItsName(BYVAL strName AS STRING)
              MyBase.ItsName=strName
            END PROPERTY
        
            OVERRIDE METHOD Move() AS STRING
              METHOD=Me.ItsName() & " Can Run Across The Prairie!"
            END METHOD
        
            OVERRIDE METHOD Eat() AS STRING
               METHOD=Me.ItsName & " Ate Some Oats As It Moved About Across The Prairie."
            END METHOD
        
            OVERRIDE METHOD MakeSound() AS STRING
              METHOD=Me.ItsName & " Made A Whinny Sound As It Ran!"
            END METHOD
          END INTERFACE
        
          INTERFACE ICare : INHERIT IUNKNOWN
            METHOD Groom() AS STRING
              METHOD="You Brush The Horse."
            END METHOD
        
            METHOD Deworm() AS STRING
              METHOD="You Give The Horse A Dose Of Dewormer."
            END METHOD
          END INTERFACE
        END CLASS
        
        
        CLASS Sheep
          INTERFACE ISheep : INHERIT MovableAnimal, IMovableAnimal
            OVERRIDE PROPERTY SET ItsName(BYVAL strName AS STRING)
              MyBase.ItsName=strName
            END PROPERTY
        
            OVERRIDE METHOD Move() AS STRING
              METHOD=Me.ItsName() & " Can Amble About Eating Grass!"
            END METHOD
        
            OVERRIDE METHOD Eat() AS STRING
               METHOD=MyBase.ItsName & " Ate Some Grass."
            END METHOD
        
            OVERRIDE METHOD MakeSound() AS STRING
              METHOD=Me.ItsName & " Went Baaaaaah!"
            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() AS LONG
          LOCAL a_blob AS IBlob
          LOCAL a_movable_animal AS IMovableAnimal
          LOCAL a_horse AS IHorse, horse_care AS ICare
          LOCAL a_sheep AS ISheep, sheep_care AS ICare
          LOCAL ePrint AS STRING
        
          'Console Set Screen 25,100
          LET a_blob = CLASS "Blob"
          a_blob.ItsName="An Immovable Blob"
          ePrint = ePrint & a_blob.ItsName() & $CRLF
          ePrint = ePrint & a_blob.Move() & $CRLF
          ePrint = ePrint & a_blob.Eat() & $CRLF
          ePrint = ePrint & a_blob.MakeSound() & $CRLF
          ePrint = ePrint & $CRLF
        
          LET a_movable_animal = CLASS "MovableAnimal"
          a_movable_animal.ItsName="A Movable Animal"
          ePrint = ePrint & a_movable_animal.ItsName() & $CRLF
          ePrint = ePrint & a_movable_animal.Move() & $CRLF
          ePrint = ePrint & a_movable_animal.Eat() & $CRLF
          ePrint = ePrint & a_movable_animal.MakeSound() & $CRLF
          ePrint = ePrint & $CRLF
        
          LET a_horse = CLASS "Horse"
          a_horse.ItsName="A Running Horse"
          ePrint = ePrint & a_horse.ItsName() & $CRLF
          ePrint = ePrint & a_horse.Move() & $CRLF
          ePrint = ePrint & a_horse.Eat() & $CRLF
          ePrint = ePrint & a_horse.MakeSound() & $CRLF
          LET horse_care = CLASS "Horse"
          ePrint = ePrint & horse_care.Groom() & $CRLF
          ePrint = ePrint & horse_care.Deworm() & $CRLF
          ePrint = ePrint & $CRLF
        
          LET a_sheep = CLASS "Sheep" : LET sheep_care = CLASS "Sheep"
          a_sheep.ItsName="The Sheep"
          ePrint = ePrint & a_sheep.ItsName & $CRLF
          ePrint = ePrint & a_sheep.Move() & $CRLF
          ePrint = ePrint & a_sheep.Eat() & $CRLF
          ePrint = ePrint & a_sheep.MakeSound() & $CRLF
          ePrint = ePrint & sheep_care.Groom() & $CRLF
          ePrint = ePrint & sheep_care.Deworm() & $CRLF
          ePrint = ePrint & $CRLF
        
          ? ePrint
        
          'Waitkey$
        
          PBMAIN=0
        END FUNCTION

        Comment

        Working...
        X
        😀
        🥰
        🤢
        😎
        😡
        👍
        👎