How to resolve the algorithm Polymorphic copy step by step in the OxygenBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Polymorphic copy step by step in the OxygenBasic 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 OxygenBasic programming language

Source code in the oxygenbasic programming language

'======
class T
'======

float vv

method constructor(float a=0) {vv=a}
method destructor    {}
method copy as T     {new T ob : ob<=vv : return ob}
method mA() as float {return vv*2}
method mB() as float {return vv*3}

end class


'======
class S
'======

has T

method mB() as float {return vv*4} 'ovveride

end class

'====
'TEST
'====

new T objA(10.5)

let objB = cast S objA.copy

print objA.mb 'result 31.5
print objB.mb 'result 42

del objA : del objB

  

You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the Erlang programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Axe programming language
You may also check:How to resolve the algorithm A+B step by step in the Rockstar programming language
You may also check:How to resolve the algorithm Quickselect algorithm step by step in the C programming language
You may also check:How to resolve the algorithm Meissel–Mertens constant step by step in the PARI/GP programming language