Start Back Next End
  
ZBasic Language Reference
112
ZBasic Microcontrollers
Sub _Create(ByVal s as Byte = 0, ByVal c as Color = Red)
m_size = s
m_color = c
End Sub
Sub Identify()
Debug.Print "I'm an A, size="; m_size
End Sub
Private:
Dim m_size as Byte
Dim m_color as Color
End Class
' Define a new class derived from an existing class.
Class B Extends A
Public:
' Define a constructor invoking a base class constructor.
Sub _Create(ByVal s as Byte = 0, ByVal c as Color = Red, _
ByVal w as Byte = 1)
Call _parent._Create(s, c)
m_weight = w
End Sub
Sub ShowWeight()
Debug.Print "weight = "; m_weight
End Sub
Private:
Dim m_weight as Byte
End Class
Dim a1 as A = _Create(5, A::Green)
Dim b1 as B = _Create(10, B::Red, 100)
Sub Main()
Call a1.Identify()
Call b1.Identify()
Call b1.ShowWeight()
End Sub
This example illustrates how the derived class B inherits some of its functionality from its base class A. 
The object b1 has a method named Identify() even though it is not explicitly present in the definition
of class B; it inherits that method from its base class.  This example also shows how to invoke a particular
base class constructor in the body of the constructor of a derived class - the _parent prefix is used to
refer to a constructor in the parent class.  The base class constructor invocation, if present, must be the
first statement in the constructor.  If you don’t explicitly invoke a base class constructor, the compiler will
automatically include code that invokes the default constructor of the base class.  You may also invoke a
constructor of particular ancestor class instead of a constructor of the base class.
It is important to be aware that the private methods and data members of the base class are not
accessible to the derived class.  Because it is often useful for derived classes and base classes to share
some method and data members that are not publically available, ZBasic supports a third visibility
attribute called Protected.  Base class methods and data members that have the Protected visibility
attribute are not accessible to code outside of the class methods but they are accessible to code in the
methods of a derived class. You can use the Protected keyword within a class definition anywhere that
you can use the Public and Private keywords, including in section labels.
If you compile and run the example code above, you’ll note that the invocation of b1.Identify()
displays the same output as when a1.Identify() is invoked.  It is likely that this behavior is not really
what is wanted.  Rather, it is more likely that the objects would identify themselves differently.  This can
Previous page Top Next page