From: Adam Beneschan <adam@irvine.com>
Subject: Re: Inheritance problem!
Date: Thu, 18 Dec 2008 08:31:12 -0800 (PST)
Date: 2008-12-18T08:31:12-08:00 [thread overview]
Message-ID: <5fa10603-24d1-4c4e-9974-b57c46455f24@w1g2000prm.googlegroups.com> (raw)
In-Reply-To: 9e295547-60dc-4e5e-ac2d-3534ea65ad01@r37g2000prr.googlegroups.com
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;
next prev parent reply other threads:[~2008-12-18 16:31 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-12-18 15:14 Inheritance problem! AndreiK
2008-12-18 16:31 ` Adam Beneschan [this message]
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
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox