It is not as complicated as it thought is was. But 2 questions for the example code below:
1. I initially used base.constructor(...) instead of base(...) in the extended class. But the manual says it is bad. Talking about corrupt vtable pointers (not sure what that means). It is really bad? If so, why is it allowed and/or should the compiler not give a warning?
2. The explicit use of base for member access seems wise to me. Else the variables (mass and radius in this case) are like magic variables and one has to search for the declaration (could be some global variable as well). Any other (disagreeing) opinions on this?
Code: Select all
const as single PI = 4 * atn(1)
'-------------------------------------------------------------------------------
type solar_object
dim as single mass 'kg
dim as single radius 'm
declare constructor(mass as single = -1, radius as single = -1)
declare sub showProperties()
end type
constructor solar_object(mass as single, radius as single)
this.mass = mass
this.radius = radius
end constructor
sub solar_object.showProperties()
print "Mass: " + str(mass)
print "Radius: " + str(radius)
end sub
'-------------------------------------------------------------------------------
type planet extends solar_object
dim as string nameStr
dim as single density 'kg/m3
declare constructor(nameStr as string, mass as single, radius as single)
declare sub showProperties()
end type
constructor planet(nameStr as string, mass as single, radius as single)
base.constructor(mass, radius) '<-- or base(mass, radius) ???
this.nameStr = nameStr
end constructor
sub planet.showProperties()
print "Name: " + nameStr
base.showProperties()
dim as single volume = (4 / 3) * PI * base.radius ^ 2 'm3
density = base.mass / base.radius '<-- use of base to clarify where the variables are declared
print "Density: " + str(density)
end sub
'-------------------------------------------------------------------------------
dim as planet earth = planet("Earth", 5.97e+24, 6.37e+6)
earth.showProperties()