comp.lang.ada
 help / color / mirror / Atom feed
* child class of limited_controlled not inheriting data
@ 2016-08-05 18:59 b.mcguinness747
  2016-08-05 19:20 ` Jeffrey R. Carter
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: b.mcguinness747 @ 2016-08-05 18:59 UTC (permalink / raw)


I defined a base class as


with Ada.Float_Text_IO;
with Ada.Finalization;

package sofa.time.two_part_date is
  type Date is new Ada.Finalization.Limited_Controlled with private;

  function To_Date (days : Integer; fraction : Real) return Date;
  function To_Date (time_string : String)            return Date;

  function "<"  (left, right : Date) return Boolean;
  function "<=" (left, right : Date) return Boolean;
  function "="  (left, right : Date) return Boolean;
  function ">=" (left, right : Date) return Boolean;
  function ">"  (left, right : Date) return Boolean;

  procedure Add_Days (this : in out Date; days : in Integer; fraction : in Real);

  function Get_Day_Count (this : Date)    return Integer;
  function Get_Day_Fraction (this : Date) return Real;
  function To_Real (this : Date)          return Real;
  function To_String (this : Date)        return String;

private
  type Date is new Ada.Finalization.Limited_Controlled with record
    day_count    : Integer;
    day_fraction : Real;    -- Normalized to 0.0 <= x < 1.0
  end record;

  overriding procedure Initialize (this: in out Date);
  overriding procedure Finalize   (this: in out Date);

  procedure Normalize (day_count : in out Integer; day_fraction : in out Real);
end;


(It's Limited_Controlled because I want to force day_fraction to be normalized when objects of the class are created.)

Then I defined a child class as


with sofa.time.two_part_date;

package sofa.time.Julian_Day_Numbers is
  type Julian_Day_Number is new sofa.time.two_part_date.Date with null record;

  function To_Julian_Day_Number (time : Instant) return Julian_Day_Number;
  function To_Calendar_Date (this : Julian_Day_Number; calendar : Calendar_Type := GREGORIAN; zone : Time_Zone_Number := 0) return Instant;
  function To_Milliseconds (this : Julian_Day_Number) return Integer;

end sofa.time.Julian_Day_Numbers;


When I try to compile the child class, I get error messages such as

sofa-time-julian_day_numbers.adb:77:79: "day_count" is not a component of type "Julian_Day_Number" defined at sofa-time-julian_day_numbers.ads:4

indicating that the child class is not inheriting the data from the parent class.

What is wrong here?

Thanks.

--- Brian


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: child class of limited_controlled not inheriting data
  2016-08-05 18:59 child class of limited_controlled not inheriting data b.mcguinness747
@ 2016-08-05 19:20 ` Jeffrey R. Carter
  2016-08-05 19:36 ` Simon Wright
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Jeffrey R. Carter @ 2016-08-05 19:20 UTC (permalink / raw)


On 08/05/2016 11:59 AM, b.mcguinness747@gmail.com wrote:
> 
> What is wrong here?

The short answer is: you're using type extension.

Longer answer: Julian_Day_Numbers, not being a descendant of Two_Part_Date, does
not have visibility to the latter's private part.

-- 
Jeff Carter
"If you think you got a nasty taunting this time,
you ain't heard nothing yet!"
Monty Python and the Holy Grail
23

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: child class of limited_controlled not inheriting data
  2016-08-05 18:59 child class of limited_controlled not inheriting data b.mcguinness747
  2016-08-05 19:20 ` Jeffrey R. Carter
@ 2016-08-05 19:36 ` Simon Wright
  2016-08-05 19:37 ` Dmitry A. Kazakov
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Simon Wright @ 2016-08-05 19:36 UTC (permalink / raw)


b.mcguinness747@gmail.com writes:

> Then I defined a child class as
>
> with sofa.time.two_part_date;
>
> package sofa.time.Julian_Day_Numbers is
>   type Julian_Day_Number is new sofa.time.two_part_date.Date with null record;
>
>   function To_Julian_Day_Number (time : Instant) return Julian_Day_Number;
>   function To_Calendar_Date (this : Julian_Day_Number; calendar : Calendar_Type := GREGORIAN; zone : Time_Zone_Number := 0) return Instant;
>   function To_Milliseconds (this : Julian_Day_Number) return Integer;
>
> end sofa.time.Julian_Day_Numbers;
>
>
> When I try to compile the child class, I get error messages such as
>
> sofa-time-julian_day_numbers.adb:77:79: "day_count" is not a component
> of type "Julian_Day_Number" defined at sofa-time-julian_day_numbers.ads:4
>
> indicating that the child class is not inheriting the data from the
> parent class.

You would need to put the julian day stuff in a child package:

   package sofa.time.two_part_date.Julian_Day_Numbers ...

Otherwise there's no visibility of the private part of two_part_date.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: child class of limited_controlled not inheriting data
  2016-08-05 18:59 child class of limited_controlled not inheriting data b.mcguinness747
  2016-08-05 19:20 ` Jeffrey R. Carter
  2016-08-05 19:36 ` Simon Wright
@ 2016-08-05 19:37 ` Dmitry A. Kazakov
  2016-08-05 19:42 ` G.B.
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2016-08-05 19:37 UTC (permalink / raw)


On 2016-08-05 20:59, b.mcguinness747@gmail.com wrote:
> I defined a base class as
>
>
> with Ada.Float_Text_IO;
> with Ada.Finalization;
>
> package sofa.time.two_part_date is
>   type Date is new Ada.Finalization.Limited_Controlled with private;
>
>   function To_Date (days : Integer; fraction : Real) return Date;
>   function To_Date (time_string : String)            return Date;
>
>   function "<"  (left, right : Date) return Boolean;
>   function "<=" (left, right : Date) return Boolean;
>   function "="  (left, right : Date) return Boolean;
>   function ">=" (left, right : Date) return Boolean;
>   function ">"  (left, right : Date) return Boolean;
>
>   procedure Add_Days (this : in out Date; days : in Integer; fraction : in Real);
>
>   function Get_Day_Count (this : Date)    return Integer;
>   function Get_Day_Fraction (this : Date) return Real;
>   function To_Real (this : Date)          return Real;
>   function To_String (this : Date)        return String;
>
> private
>   type Date is new Ada.Finalization.Limited_Controlled with record
>     day_count    : Integer;
>     day_fraction : Real;    -- Normalized to 0.0 <= x < 1.0
>   end record;
>
>   overriding procedure Initialize (this: in out Date);
>   overriding procedure Finalize   (this: in out Date);
>
>   procedure Normalize (day_count : in out Integer; day_fraction : in out Real);
> end;
>
> (It's Limited_Controlled because I want to force day_fraction to be
> normalized when objects of the class are created.)

It must be Controlled if some complicated initialization required, and 
likely abstract, not Limited_Controlled.

> Then I defined a child class as
>
> with sofa.time.two_part_date;
>
> package sofa.time.Julian_Day_Numbers is
[...]
> When I try to compile the child class, I get error messages such as
>
> sofa-time-julian_day_numbers.adb:77:79: "day_count" is not a
> component  of type "Julian_Day_Number" defined at sofa-time-julian_day_numbers.ads:4
>
> indicating that the child class is not inheriting the data from the parent class.
>
> What is wrong here?

You didn't make day_count visible. There are three ways to do it, in the 
order of publicity:

1. Make it a public member

2. Make it available through getter/setter. You have Get_Day_Count, if 
that is, then use it in Julian_Day_Numbers.

3. Make Julian_Day_Numbers child package:

    sofa.time.two_part_date.Julian_Day_Numbers


-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: child class of limited_controlled not inheriting data
  2016-08-05 18:59 child class of limited_controlled not inheriting data b.mcguinness747
                   ` (2 preceding siblings ...)
  2016-08-05 19:37 ` Dmitry A. Kazakov
@ 2016-08-05 19:42 ` G.B.
  2016-08-05 20:26 ` b.mcguinness747
  2016-08-06 19:00 ` rieachus
  5 siblings, 0 replies; 8+ messages in thread
From: G.B. @ 2016-08-05 19:42 UTC (permalink / raw)


On 05.08.16 20:59, b.mcguinness747@gmail.com wrote:
> indicating that the child class is not inheriting the data from the parent class.

See Jeffrey Carter's response for the reason.
To make a type inherit the private data of a parent type,
a child package is one possibility, it establishes
visibility. Yours is a sibling package.

Minor nitpick: Ada has classes of types, and, in particular,
a T'Class type for naming class-wide O-O types, such as the
set of types all descending from T. So, "class" can be confusing
if talking about Ada types.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: child class of limited_controlled not inheriting data
  2016-08-05 18:59 child class of limited_controlled not inheriting data b.mcguinness747
                   ` (3 preceding siblings ...)
  2016-08-05 19:42 ` G.B.
@ 2016-08-05 20:26 ` b.mcguinness747
  2016-08-05 20:47   ` Dmitry A. Kazakov
  2016-08-06 19:00 ` rieachus
  5 siblings, 1 reply; 8+ messages in thread
From: b.mcguinness747 @ 2016-08-05 20:26 UTC (permalink / raw)


Thank you for your help.  Object-oriented code in Ada is a bit confusing, and it's sometimes difficult to find the information I need in the manual.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: child class of limited_controlled not inheriting data
  2016-08-05 20:26 ` b.mcguinness747
@ 2016-08-05 20:47   ` Dmitry A. Kazakov
  0 siblings, 0 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2016-08-05 20:47 UTC (permalink / raw)


On 2016-08-05 22:26, b.mcguinness747@gmail.com wrote:
> Thank you for your help. Object-oriented code in Ada is a bit
> confusing, and it's sometimes difficult to find the information I need
> in the manual.

Firstly there is nothing confusing in Ada's OO. In fact, it is much 
simpler than C++ OO.

Secondly the issue you have has nothing to do with OO. It is a 
visibility issue.

Thirdly it would be just same in C++ if you declared a private member 
and then tried to use in a derived class.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: child class of limited_controlled not inheriting data
  2016-08-05 18:59 child class of limited_controlled not inheriting data b.mcguinness747
                   ` (4 preceding siblings ...)
  2016-08-05 20:26 ` b.mcguinness747
@ 2016-08-06 19:00 ` rieachus
  5 siblings, 0 replies; 8+ messages in thread
From: rieachus @ 2016-08-06 19:00 UTC (permalink / raw)


On Friday, August 5, 2016 at 2:59:16 PM UTC-4, b.mcgui...@gmail.com wrote:
> I defined a base class as
... 
>   function To_Date (days : Integer; fraction : Real) return Date;
>   function To_Date (time_string : String)            return Date;
... 
> What is wrong here?

Actually you have the solution right there, except that you will want to override To_Date for your Julian date abstraction to correct the times.  Julian dates, at least in astronomical use, begin at noon GMT.  (I could chide you for not having a timezone parameter everywhere needed, but I am going to assume that you will add that after you get the basics working. ;-)

There is a bit of magic necessary in your overriding of To_Date.  You need to call the original function:

function To_Date(days : Integer; fraction : Real) return Julian_Date is
  Jdays: Integer := days;
  Jfraction: Real := fraction;
begin
  -- fix up Jdays and Jfraction here.
  return sofa.time.two_part_date.To_Date(Jdays,Jfraction) with null;
end To_Date;

I would probably use a fixed point type, Milliseconds, instead of your Float fraction, but even if I could convince you why it is better, it probably is not worth changing, unless you are pointing a really big telescope at asteroids.


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2016-08-06 19:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-05 18:59 child class of limited_controlled not inheriting data b.mcguinness747
2016-08-05 19:20 ` Jeffrey R. Carter
2016-08-05 19:36 ` Simon Wright
2016-08-05 19:37 ` Dmitry A. Kazakov
2016-08-05 19:42 ` G.B.
2016-08-05 20:26 ` b.mcguinness747
2016-08-05 20:47   ` Dmitry A. Kazakov
2016-08-06 19:00 ` rieachus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox