comp.lang.ada
 help / color / mirror / Atom feed
* Inheritance problem!
@ 2008-12-18 15:14 AndreiK
  2008-12-18 16:31 ` Adam Beneschan
  0 siblings, 1 reply; 13+ messages in thread
From: AndreiK @ 2008-12-18 15:14 UTC (permalink / raw)


Hello everybody!

I am quite new in Ada programming and I am trying to write simple (at
the moment) OO program, but I have a problem with inheritance of the
private type. I am using the GNAT+GPS (2008) and learning Ada by John
Barnes "Ada 2005".

The idea of the program is next: we have tagged private types Vector
and Matrix. The Matrix inheritances all components and functions from
Vector. Difference only in constructor: Mat is for Matrix and Vec is
for Vector.

In the test program the errors occures when I try to call inherited
function, in the example it is Cell(...) and if I try to use function
Put(This: Vect.Vector'Class).

I don't understand where the mistake is?

Thanks everybody for a help!

The code of the test program:

----------------------------------------
--   test.adb
----------------------------------------
with Ada.Text_IO;
with Vectors.Matrices;

procedure test is

   package TIO renames Ada.Text_IO;
   package FIO is new Ada.Text_IO.Float_IO(Float);

   package Vect is new Vectors(Float); --use Vect;
   package Matr is new Vect.Matrices;-- use Matr;
   M1: Matr.Matrix := Matr.Mat(4, 3, 1.0);
   V1: Vect.Vector := Vect.Vec(3, 2.0);

   --  Procedure, which intended to print out the Vector or Matrix
objects.
   --
   procedure Put(This: in Vect.Vector'Class) is
   begin
      TIO.New_Line;
      TIO.New_Line;
      for J in 0 .. This.Size(0)-1 loop
         TIO.Put("|");
         for K in 0 .. This.Size(1)-1 loop
            FIO.Put( Item => This.Cell(J, K),
                    Fore => 2,
                    Aft  => 3,
                    Exp  => 3);
            TIO.Put(" ");
         end loop;
         TIO.Put("|");
         TIO.New_Line;
      end loop;
      null;
   end;

begin


   FIO.Put(M1.Cell(0,0));
   -- test.adb:37:14: error:
   -- no selector "Cell" for private type "Matrix"
   -- defined at vectors-matrices.ads:6, instance at line 10

   Put(M1);
   -- test.adb:39:08: error: expected type "Vector'class"
   -- defined at vectors.ads:12, instance at line 9

   -- test.adb:39:08: error: found private type "Matrix"
   -- defined at vectors-matrices.ads:6, instance at line 10

   -- test.adb:39:08: error:   ==> in call to "Put" at line 14

   Put(V1);
   -- No Errors for Vector V1.

   FIO.Put(V1.Cell(0,0));
   -- No Errors for Vector V1.

   TIO.New_Line;

end test;

----------------------------------------
--  vectors.ads
----------------------------------------
generic
   type Real is digits <>;

package Vectors is
   pragma Preelaborate;

   subtype Dim_Type is Natural;
   type Sizes_Arr is array(Integer range <>) of Dim_Type;

   --
   --     The type <Vector> is private
   --
   type Vector is tagged private;
   pragma Preelaborable_Initialization (Vector);

   --
   --  Vec - the constructor of the entity of type Vector.
   --
   function Vec(Elem_Num: Dim_Type; Fill_by_Value: Real'Base := 0.0)
return Vector;

   --
   --  Function Cell returns the value of the element at position
Index
   --  of the object of the type Vector.
   --
   function Cell(
                 This : in Vector;
                 Row_Index: in Natural;
                 Col_Index: in Natural
                ) return Real'Base;

   function Row_Len(This: in Vector) return Natural;
   function Col_Len(This: in Vector) return Natural;

   --
   --  Function Size returns the array (1D) of the dimension's sizes
of the object
   --  of the type Vector.
   --
   function Size(This: in Vector) return Sizes_Arr;

private

   type Elements_Array is array (Natural range <>) of Real'Base;
   type Elements_Access is not null access Elements_Array;

   type Vector is tagged
      record
         Elements_ac        : Elements_Access;
         Count              : Natural := 0;
         Sizes              : Sizes_Arr(0..1);
         Transposed         : Boolean := False;
         Busy               : Natural := 0;
      end record;

end Vectors;

----------------------------------------
-- vectors-matrices.ads
----------------------------------------
generic

package Vectors.Matrices is
   pragma Preelaborate;

   type Matrix is tagged private;
   pragma Preelaborable_Initialization (Matrix);

   function Mat(Row_Num,Col_Num: in Dim_Type; Fill_by_Value:
Real'Base := 0.0) return Matrix;

private

   type Matrix is new Vector with null record;

end Vectors.Matrices;



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

* Re: Inheritance problem!
  2008-12-18 15:14 Inheritance problem! AndreiK
@ 2008-12-18 16:31 ` Adam Beneschan
  2008-12-19 21:34   ` Maciej Sobczak
  0 siblings, 1 reply; 13+ messages in thread
From: Adam Beneschan @ 2008-12-18 16:31 UTC (permalink / raw)


On Dec 18, 7:14 am, AndreiK <andrei.krivos...@gmail.com> wrote:
> Hello everybody!
>
> I am quite new in Ada programming and I am trying to write simple (at
> the moment) OO program, but I have a problem with inheritance of the
> private type. I am using the GNAT+GPS (2008) and learning Ada by John
> Barnes "Ada 2005".
>
> The idea of the program is next: we have tagged private types Vector
> and Matrix. The Matrix inheritances all components and functions from
> Vector. Difference only in constructor: Mat is for Matrix and Vec is
> for Vector.
>
> In the test program the errors occures when I try to call inherited
> function, in the example it is Cell(...) and if I try to use function
> Put(This: Vect.Vector'Class).
>
> I don't understand where the mistake is?

The program isn't compiling because Matrix is a private type.  In
Vectors. Matrices (and therefore also in every instance of it), Matrix
is declared as private.  In the PRIVATE part, Matrix is declared as a
"new Vector"; however, other packages that use Vectors.Matrices don't
get to know that Matrix is actually a "Vector".  So they won't know
that the Cell function can be used on it, since Cell is for vectors,
but they don't know that a Matrix is a Vector.  And they won't be able
to use Put on it, because Put is defined for objects of types in
Vector'Class, but they don't know that a Matrix is a Vector and so
they won't know that Put can be used on it.

Most likely, what you want is for packages that use Vectors.Matrices
to know that a Matrix is a kind of Vector, but you want to keep all
the other implementation details hidden.  If that's the case, then
replace

   type Matrix is tagged private;

with

   type Matrix is new Vector with private;

                             -- Adam



>
> Thanks everybody for a help!
>
> The code of the test program:
>
> ----------------------------------------
> --   test.adb
> ----------------------------------------
> with Ada.Text_IO;
> with Vectors.Matrices;
>
> procedure test is
>
>    package TIO renames Ada.Text_IO;
>    package FIO is new Ada.Text_IO.Float_IO(Float);
>
>    package Vect is new Vectors(Float); --use Vect;
>    package Matr is new Vect.Matrices;-- use Matr;
>    M1: Matr.Matrix := Matr.Mat(4, 3, 1.0);
>    V1: Vect.Vector := Vect.Vec(3, 2.0);
>
>    --  Procedure, which intended to print out the Vector or Matrix
> objects.
>    --
>    procedure Put(This: in Vect.Vector'Class) is
>    begin
>       TIO.New_Line;
>       TIO.New_Line;
>       for J in 0 .. This.Size(0)-1 loop
>          TIO.Put("|");
>          for K in 0 .. This.Size(1)-1 loop
>             FIO.Put( Item => This.Cell(J, K),
>                     Fore => 2,
>                     Aft  => 3,
>                     Exp  => 3);
>             TIO.Put(" ");
>          end loop;
>          TIO.Put("|");
>          TIO.New_Line;
>       end loop;
>       null;
>    end;
>
> begin
>
>    FIO.Put(M1.Cell(0,0));
>    -- test.adb:37:14: error:
>    -- no selector "Cell" for private type "Matrix"
>    -- defined at vectors-matrices.ads:6, instance at line 10
>
>    Put(M1);
>    -- test.adb:39:08: error: expected type "Vector'class"
>    -- defined at vectors.ads:12, instance at line 9
>
>    -- test.adb:39:08: error: found private type "Matrix"
>    -- defined at vectors-matrices.ads:6, instance at line 10
>
>    -- test.adb:39:08: error:   ==> in call to "Put" at line 14
>
>    Put(V1);
>    -- No Errors for Vector V1.
>
>    FIO.Put(V1.Cell(0,0));
>    -- No Errors for Vector V1.
>
>    TIO.New_Line;
>
> end test;
>
> ----------------------------------------
> --  vectors.ads
> ----------------------------------------
> generic
>    type Real is digits <>;
>
> package Vectors is
>    pragma Preelaborate;
>
>    subtype Dim_Type is Natural;
>    type Sizes_Arr is array(Integer range <>) of Dim_Type;
>
>    --
>    --     The type <Vector> is private
>    --
>    type Vector is tagged private;
>    pragma Preelaborable_Initialization (Vector);
>
>    --
>    --  Vec - the constructor of the entity of type Vector.
>    --
>    function Vec(Elem_Num: Dim_Type; Fill_by_Value: Real'Base := 0.0)
> return Vector;
>
>    --
>    --  Function Cell returns the value of the element at position
> Index
>    --  of the object of the type Vector.
>    --
>    function Cell(
>                  This : in Vector;
>                  Row_Index: in Natural;
>                  Col_Index: in Natural
>                 ) return Real'Base;
>
>    function Row_Len(This: in Vector) return Natural;
>    function Col_Len(This: in Vector) return Natural;
>
>    --
>    --  Function Size returns the array (1D) of the dimension's sizes
> of the object
>    --  of the type Vector.
>    --
>    function Size(This: in Vector) return Sizes_Arr;
>
> private
>
>    type Elements_Array is array (Natural range <>) of Real'Base;
>    type Elements_Access is not null access Elements_Array;
>
>    type Vector is tagged
>       record
>          Elements_ac        : Elements_Access;
>          Count              : Natural := 0;
>          Sizes              : Sizes_Arr(0..1);
>          Transposed         : Boolean := False;
>          Busy               : Natural := 0;
>       end record;
>
> end Vectors;
>
> ----------------------------------------
> -- vectors-matrices.ads
> ----------------------------------------
> generic
>
> package Vectors.Matrices is
>    pragma Preelaborate;
>
>    type Matrix is tagged private;
>    pragma Preelaborable_Initialization (Matrix);
>
>    function Mat(Row_Num,Col_Num: in Dim_Type; Fill_by_Value:
> Real'Base := 0.0) return Matrix;
>
> private
>
>    type Matrix is new Vector with null record;
>
> end Vectors.Matrices;




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

* Re: Inheritance problem!
  2008-12-18 16:31 ` Adam Beneschan
@ 2008-12-19 21:34   ` Maciej Sobczak
  2008-12-19 22:37     ` Adam Beneschan
  2008-12-20  0:02     ` Georg Bauhaus
  0 siblings, 2 replies; 13+ messages in thread
From: Maciej Sobczak @ 2008-12-19 21:34 UTC (permalink / raw)


On 18 Gru, 17:31, Adam Beneschan <a...@irvine.com> wrote:

Adam, you have focused on strictly Ada syntax aspect of the problem,
but your answer will not help - Matrix is *not* a Vector and all
efforts to hammer this broken idea out into code are futile and are
doomed to fail, sooner or later.

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com

Database Access Library for Ada: www.inspirel.com/soci-ada



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

* Re: Inheritance problem!
  2008-12-19 21:34   ` Maciej Sobczak
@ 2008-12-19 22:37     ` Adam Beneschan
  2008-12-20  0:02     ` Georg Bauhaus
  1 sibling, 0 replies; 13+ messages in thread
From: Adam Beneschan @ 2008-12-19 22:37 UTC (permalink / raw)


On Dec 19, 1:34 pm, Maciej Sobczak <see.my.homep...@gmail.com> wrote:
> On 18 Gru, 17:31, Adam Beneschan <a...@irvine.com> wrote:
>
> Adam, you have focused on strictly Ada syntax aspect of the problem,
> but your answer will not help

Yes it will---you did read the first sentence of his post, didn't
you?  First things first.  Teaching good design principles is
important, but it doesn't do any good if the person doesn't know how
to write a compilable program.

                                 -- Adam




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

* Re: Inheritance problem!
  2008-12-19 21:34   ` Maciej Sobczak
  2008-12-19 22:37     ` Adam Beneschan
@ 2008-12-20  0:02     ` Georg Bauhaus
  2008-12-20  0:33       ` Jeffrey R. Carter
  1 sibling, 1 reply; 13+ messages in thread
From: Georg Bauhaus @ 2008-12-20  0:02 UTC (permalink / raw)


Maciej Sobczak wrote:
> Matrix is *not* a Vector

Why is a Matrix not a Vector? Isn't a matrix in some
vector space?



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

* Re: Inheritance problem!
  2008-12-20  0:02     ` Georg Bauhaus
@ 2008-12-20  0:33       ` Jeffrey R. Carter
  2008-12-20  8:28         ` Dmitry A. Kazakov
  2008-12-22 17:10         ` hesobreira
  0 siblings, 2 replies; 13+ messages in thread
From: Jeffrey R. Carter @ 2008-12-20  0:33 UTC (permalink / raw)


Georg Bauhaus wrote:
> 
> Why is a Matrix not a Vector? Isn't a matrix in some
> vector space?

A vector is a matrix with one column.

-- 
Jeff Carter
"Go and boil your bottoms."
Monty Python & the Holy Grail
01



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

* Re: Inheritance problem!
  2008-12-20  0:33       ` Jeffrey R. Carter
@ 2008-12-20  8:28         ` Dmitry A. Kazakov
  2008-12-21  1:18           ` Jeffrey R. Carter
  2008-12-22 17:10         ` hesobreira
  1 sibling, 1 reply; 13+ messages in thread
From: Dmitry A. Kazakov @ 2008-12-20  8:28 UTC (permalink / raw)


On Sat, 20 Dec 2008 00:33:26 GMT, Jeffrey R. Carter wrote:

> Georg Bauhaus wrote:
>> 
>> Why is a Matrix not a Vector? Isn't a matrix in some
>> vector space?
> 
> A vector is a matrix with one column.

A number is a matrix with one column and one row...

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



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

* Re: Inheritance problem!
  2008-12-20  8:28         ` Dmitry A. Kazakov
@ 2008-12-21  1:18           ` Jeffrey R. Carter
  2008-12-21  8:29             ` Dmitry A. Kazakov
  0 siblings, 1 reply; 13+ messages in thread
From: Jeffrey R. Carter @ 2008-12-21  1:18 UTC (permalink / raw)


Dmitry A. Kazakov wrote:
> 
> A number is a matrix with one column and one row...

No, a number is the value in the 1x1 matrix. You can transpose a matrix. You 
can't transpose a number.

-- 
Jeff Carter
"I've got to stay here, but there's no reason
why you folks shouldn't go out into the lobby
until this thing blows over."
Horse Feathers
50



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

* Re: Inheritance problem!
  2008-12-21  1:18           ` Jeffrey R. Carter
@ 2008-12-21  8:29             ` Dmitry A. Kazakov
  2008-12-21 17:05               ` Jeffrey R. Carter
  0 siblings, 1 reply; 13+ messages in thread
From: Dmitry A. Kazakov @ 2008-12-21  8:29 UTC (permalink / raw)


On Sun, 21 Dec 2008 01:18:32 GMT, Jeffrey R. Carter wrote:

> Dmitry A. Kazakov wrote:
>> 
>> A number is a matrix with one column and one row...
> 
> No, a number is the value in the 1x1 matrix. You can transpose a matrix. You 
> can't transpose a number.

(It was a joke. Such statements are meaningless without further
explanations)

 But you surely can transpose 1x1 matrices. Assuming "is-a matrix" means
injective mapping keeping matrix operations, here is transposing numbers:

Number  ---> Matrix
                     |
                     | Transpose
                    V
Number  <--- Matrix

When you wanted to consider numbers as elements of matrices, then the above
would be illegal, but you could do the same with vectors (block matrices).
The point is that it is not a property of vector or number to be something
else, but merely a matter of view.

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



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

* Re: Inheritance problem!
  2008-12-21  8:29             ` Dmitry A. Kazakov
@ 2008-12-21 17:05               ` Jeffrey R. Carter
  0 siblings, 0 replies; 13+ messages in thread
From: Jeffrey R. Carter @ 2008-12-21 17:05 UTC (permalink / raw)


Dmitry A. Kazakov wrote:
> 
> (It was a joke. Such statements are meaningless without further
> explanations)

Sorry. I don't generally like smileys, but maybe one was appropriate here. In 
that case, I retract my serious comments. Substitute some humorous comments instead.

-- 
Jeff Carter
"You tiny-brained wipers of other people's bottoms!"
Monty Python & the Holy Grail
18



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

* Re: Inheritance problem!
  2008-12-20  0:33       ` Jeffrey R. Carter
  2008-12-20  8:28         ` Dmitry A. Kazakov
@ 2008-12-22 17:10         ` hesobreira
  2008-12-22 19:46           ` Jeffrey R. Carter
  1 sibling, 1 reply; 13+ messages in thread
From: hesobreira @ 2008-12-22 17:10 UTC (permalink / raw)


I would say the opposite: the vector is a matrix with of one line or
column :-)

Hugo

On Dec 20, 1:33 am, "Jeffrey R. Carter"
<spam.jrcarter....@spam.acm.org> wrote:
> Georg Bauhaus wrote:
>
> > Why is a Matrix not a Vector? Isn't a matrix in some
> > vector space?
>
> A vector is a matrix with one column.
>
> --
> Jeff Carter
> "Go and boil your bottoms."
> Monty Python & the Holy Grail
> 01




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

* Re: Inheritance problem!
  2008-12-22 17:10         ` hesobreira
@ 2008-12-22 19:46           ` Jeffrey R. Carter
  2008-12-23 13:06             ` Maciej Sobczak
  0 siblings, 1 reply; 13+ messages in thread
From: Jeffrey R. Carter @ 2008-12-22 19:46 UTC (permalink / raw)


hesobreira wrote:
> I would say the opposite: the vector is a matrix with of one line or
> column :-)

Yes. A vector is a matrix with one column. You can transpose a vector to get a 
matrix with one row, sometimes called a row vector.

-- 
Jeff Carter
"The time has come to act, and act fast. I'm leaving."
Blazing Saddles
36



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

* Re: Inheritance problem!
  2008-12-22 19:46           ` Jeffrey R. Carter
@ 2008-12-23 13:06             ` Maciej Sobczak
  0 siblings, 0 replies; 13+ messages in thread
From: Maciej Sobczak @ 2008-12-23 13:06 UTC (permalink / raw)


On 22 Gru, 20:46, "Jeffrey R. Carter"

> Yes. A vector is a matrix with one column. You can transpose a vector to get a
> matrix with one row, sometimes called a row vector.

I think it might be interesting to read chapters 21.6 to 21.11 (*all*
of them), starting here:

http://www.parashift.com/c++-faq-lite/proper-inheritance.html#faq-21.6

It was written for C++ programmers, but it does not matter here,
because the problem is of general nature and the whole discussion can
be trivially expressed in terms of Ada.

"I don't care what your inheritance problem is, but all —yes all — bad
inheritances boil down to the Circle-is-not-a-kind-of-Ellipse
example."

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com

Database Access Library for Ada: www.inspirel.com/soci-ada




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

end of thread, other threads:[~2008-12-23 13:06 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-18 15:14 Inheritance problem! AndreiK
2008-12-18 16:31 ` Adam Beneschan
2008-12-19 21:34   ` Maciej Sobczak
2008-12-19 22:37     ` Adam Beneschan
2008-12-20  0:02     ` Georg Bauhaus
2008-12-20  0:33       ` Jeffrey R. Carter
2008-12-20  8:28         ` Dmitry A. Kazakov
2008-12-21  1:18           ` Jeffrey R. Carter
2008-12-21  8:29             ` Dmitry A. Kazakov
2008-12-21 17:05               ` Jeffrey R. Carter
2008-12-22 17:10         ` hesobreira
2008-12-22 19:46           ` Jeffrey R. Carter
2008-12-23 13:06             ` Maciej Sobczak

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