How to resolve the algorithm Polymorphic copy step by step in the BBC BASIC programming language
How to resolve the algorithm Polymorphic copy step by step in the BBC BASIC programming language
Table of Contents
Problem Statement
An object is polymorphic when its specific type may vary. The types a specific value may take, is called class. It is trivial to copy an object if its type is known: Here x is not polymorphic, so y is declared of same type (int) as x. But if the specific type of x were unknown, then y could not be declared of any specific type. The task: let a polymorphic object contain an instance of some specific type S derived from a type T. The type T is known. The type S is possibly unknown until run time. The objective is to create an exact copy of such polymorphic object (not to create a reference, nor a pointer to). Let further the type T have a method overridden by S. This method is to be called on the copy to demonstrate that the specific type of the copy is indeed S.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Polymorphic copy step by step in the BBC BASIC programming language
Source code in the bbc programming language
INSTALL @lib$ + "CLASSLIB"
REM Create parent class T:
DIM classT{array#(0), setval, retval}
DEF classT.setval (n%,v) classT.array#(n%) = v : ENDPROC
DEF classT.retval (n%) = classT.array#(n%)
PROC_class(classT{})
REM Create class S derived from T, known only at run-time:
RunTimeSize% = RND(100)
DIM classS{array#(RunTimeSize%)}
PROC_inherit(classS{}, classT{})
DEF classS.retval (n%) = classS.array#(n%) ^ 2 : REM Overridden method
PROC_class(classS{})
REM Create an instance of class S:
PROC_new(myobject{}, classS{})
REM Now make a copy of the instance:
DIM mycopy{} = myobject{}
mycopy{} = myobject{}
PROC_discard(myobject{})
REM Test the copy (should print 123^2):
PROC(mycopy.setval)(RunTimeSize%, 123)
result% = FN(mycopy.retval)(RunTimeSize%)
PRINT result%
END
You may also check:How to resolve the algorithm Truncate a file step by step in the Erlang programming language
You may also check:How to resolve the algorithm Call an object method step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Conway's Game of Life step by step in the Erlang programming language
You may also check:How to resolve the algorithm Combinations step by step in the BQN programming language
You may also check:How to resolve the algorithm Wireworld step by step in the Python programming language