I’m having some difficulties getting my head around inheritance in Ada, and with some syntax.
My goal is to derive from an abstract type with a record, and use a different data type in the record field. Here’s what I’ve been able to compile:
type Base is abstract new Parent.ParentType with record
X:Access_Type;
end record
type Child is new Base with record
n:Integer;
end record;
But I don’t want to have this additional n field, I’d like to have X be an integer in the child type. I can’t get the compiler to be happy with it. Something like the following is what I want:
type Base is abstract new Parent.ParentType with tagged record
X:Access_Type;
end record;
type Child is new Base with record
X:Integer;
end record;
Unfortunately, I can’t figure out how to tag the base type which I think would allow me to reassign the X field. (Without tagging, the compiler complains of conflicting declarations.)
Can someone shed some light on this? I’m quite new to OO programming in general and I’m finding Ada’s type approach more confusing than the usual class approach.
Basehas to be tagged, because you can’t sayis abstract new Parent.Parent_TypeunlessParent.Parent_Typeis tagged, and that means that any derived type such asBasemust be too.The problem is that, as you have it, any code that can see
Childcould see twoXs; the one inBaseand the one inChild. The compiler won’t let you be ambiguous; when others read your code and seeMy_Child.X, how will they know whichXyou meant?One way round this would be to make the full declaration of
Baseprivate, so that there’s only one visible possibility forMy_Child.X: