From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,1a1baf807a7fe9e2 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!w1g2000prm.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Inheritance problem! Date: Thu, 18 Dec 2008 08:31:12 -0800 (PST) Organization: http://groups.google.com Message-ID: <5fa10603-24d1-4c4e-9974-b57c46455f24@w1g2000prm.googlegroups.com> References: <9e295547-60dc-4e5e-ac2d-3534ea65ad01@r37g2000prr.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1229617872 16999 127.0.0.1 (18 Dec 2008 16:31:12 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 18 Dec 2008 16:31:12 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: w1g2000prm.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:4010 Date: 2008-12-18T08:31:12-08:00 List-Id: On Dec 18, 7:14 am, AndreiK 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 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;