Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8454569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:03:43+00:00 2026-06-10T12:03:43+00:00

I am trying to understand tagged types and code extensions and reuse. I first

  • 0

I am trying to understand tagged types and code extensions and reuse. I first create a program conventional_method_number_of_averages which implements one method for doing some calculation. Then I use extension of the tagged type and create a second program alternative_method_number_of_averages which implements a different method for solving the same problem. The difference is that now I have an additional parametercalled overlap_fraction. Here is the simple program with the first method conventional_method_number_of_averages that works fine:

  1. number_of_averages.ads

    with Conventional_Method_Number_Of_Averages;
    package Number_Of_Averages renames Conventional_Method_Number_Of_Averages;
    
  2. conventional_method_number_of_averages.ads

    package Conventional_Method_Number_Of_Averages is
    
    type First_Method is tagged private;
    
    procedure Count_Averages (Any_Method : in out First_Method; Sampling_Frequency, FFT_Size, Time_Recorded: in Float);
    
    function Average_Is (Any_Method : in First_Method) return Float;
    
    private
    type First_Method is tagged
      record
         Number_Of_Averages : Float :=0.0;
       end record;
    
    end Conventional_Method_Number_Of_Averages;
    
  3. conventional_method_number_of_averages.adb

    package body Conventional_Method_Number_Of_Averages is
    
    procedure Count_Averages (Any_Method : in out First_Method; Sampling_Frequency, FFT_Size, Time_Recorded: in Float) is
    begin
        Any_Method.Number_Of_Averages := (((Sampling_Frequency * Time_Recorded) - FFT_Size)*(2.0/FFT_Size)) + 1.0;
    end Count_Averages;
    
    function Average_Is (Any_Method : in First_Method) return Float is
    begin
        return Any_Method.Number_Of_Averages;
    end;
    
    end Conventional_Method_Number_Of_Averages;
    
  4. and the test file test_number_of_averages.adb

    with Ada.Float_Text_IO;
    with Ada.Text_IO; use Ada.Text_IO;
    
    with Number_Of_Averages;
    with Conventional_Method_Number_Of_Averages;
    use type Number_Of_Averages.First_Method;
    procedure Test_Number_Of_Averages is
    
    Fs, Time_Duration, NFFT    : Float := 0.0;
    Averages                   : Float := 0.0;
    
    Good_Method : Conventional_Method_Number_Of_Averages.First_Method;
    
    begin
       Ada.Text_IO.Put("Enter the sampling frequency ");
       Ada.Float_Text_IO.Get (Item => Fs);
       Ada.Text_IO.New_Line (1);
       Ada.Text_IO.Put("Enter the time recorded ");
       Ada.Float_Text_IO.Get (Item => Time_Duration);
       Ada.Text_IO.New_Line (1);
       Ada.Text_IO.Put("Enter the FFT size ");
       Ada.Float_Text_IO.Get (Item => NFFT);
       Ada.Text_IO.Put_Line (Ada.Text_IO.Get_Line);
    
       Ada.Text_IO.New_Line(1);
       Ada.Text_IO.Put("Number of averages = ");
       Number_Of_Averages.Count_Averages(Good_Method, Fs, NFFT, Time_Duration);
    
       Averages := Conventional_Method_Number_Of_Averages.Average_Is(Good_Method);
       Ada.Float_Text_IO.Put (Item => Averages, Fore => 3, Aft  => 5, Exp  => 0);
    
    end Test_Number_Of_Averages;
    

The above works fine as it is.

Now if I create an alternative method for the same calculation, I have:

  1. its specification alternative_method_number_of_averages.ads

    with Conventional_Method_Number_Of_Averages;
    use Conventional_Method_Number_Of_Averages;
    
    package Alternative_Method_Number_Of_Averages is
    
    type Second_Method is new First_Method with private;
    
    --override this function
    procedure Count_Averages (Any_Method : in out Second_Method; Sampling_Frequency, FFT_Size, Time_Recorded, Overlap_Fraction: in Float);
    
    
    private
    type Second_Method is new First_Method with
      record
         Overlap_Fraction : Float :=0.5;
      end record;
    
    end Alternative_Method_Number_Of_Averages;
    

and

  1. its body alternative_method_number_of_averages.adb

    package body Alternative_Method_Number_Of_Averages is
    
    --override this function
    procedure Count_Averages (Any_Method : in out Second_Method; Sampling_Frequency, FFT_Size, Time_Recorded, Overlap_Fraction: in Float) is
    begin
       Any_Method.Number_Of_Averages := ((Sampling_Frequency * Time_Recorded) - FFT_Size) / (FFT_Size - Overlap_Fraction * FFT_Size) + 1.0;
    end Count_Averages;
    
    
    end Alternative_Method_Number_Of_Averages;
    

Then on compiling the specification file, I get the error message:

alternative_method_number_of_averages.ads: no selector “number_of_averages” for type “Second_Method” defined at alternative_method_of_averages.ads. The culprit line is:

     Any_Method.Number_Of_Averages := ((Sampling_Frequency * Time_Recorded) - FFT_Size) / (FFT_Size - Overlap_Fraction * FFT_Size) + 1.0;

So how to fix this?

In the end I would like to be able to have something like:

     Number_Of_Averages.Count_Averages(Alternative_Method, Fs, NFFT, Time_Duration, Overlap_fraction);

in the test file test_number_of_averages.adb, similarly as the working code

     Number_Of_Averages.Count_Averages(Good_Method, Fs, NFFT, Time_Duration);

Thanks a lot…

UPDATE

So now for the alternative method implementation, I have, renamed Alternative_Method_Number_Of_Averages.ads/adb to Conventional_Method_Number_Of_Averages-Alternative_Method_Number_Of_Averages.ads/adb. The test file is:

   with Ada.Float_Text_IO;
   with Ada.Text_IO; use Ada.Text_IO;

   with Number_Of_Averages;
   with Conventional_Method_Number_Of_Averages;
   use type Number_Of_Averages.First_Method;

   with Conventional_Method_Number_Of_Averages.Alternative_Method_Number_Of_Averages;

   procedure Test_Number_Of_Averages is

   Fs, Time_Duration, NFFT    : Float := 0.0;
   Averages1                   : Float := 0.0;
   Averages2                   : Float := 0.0;

   Good_Method                 : Conventional_Method_Number_Of_Averages.First_Method;
   Alternative_Method          : Conventional_Method_Number_Of_Averages.Alternative_Method_Number_Of_Averages.Second_Method;

   begin
   Ada.Text_IO.Put("Enter the sampling frequency ");
   Ada.Float_Text_IO.Get (Item => Fs);
   Ada.Text_IO.New_Line (1);
   Ada.Text_IO.Put("Enter the time recorded ");
   Ada.Float_Text_IO.Get (Item => Time_Duration);
   Ada.Text_IO.New_Line (1);
   Ada.Text_IO.Put("Enter the FFT size ");
   Ada.Float_Text_IO.Get (Item => NFFT);
   Ada.Text_IO.Put_Line (Ada.Text_IO.Get_Line);

   Ada.Text_IO.New_Line(1);
   Ada.Text_IO.Put("Number of averages = ");
   Number_Of_Averages.Count_Averages(Good_Method, Fs, NFFT, Time_Duration);

   Averages1 := Conventional_Method_Number_Of_Averages.Average_Is(Good_Method);
   Ada.Float_Text_IO.Put (Item => Averages1, Fore => 3, Aft  => 5, Exp  => 0);

   Ada.Text_IO.New_Line(1);
   Ada.Text_IO.Put("Number of averages = ");

   Number_Of_Averages.Alternative_Method_Number_Of_Averages.Count_Averages(Alternative_Method, Fs, NFFT, Time_Duration); 
   Averages2 := Conventional_Method_Number_Of_Averages.Alternative_Method_Number_Of_Averages.Average_Is(Alternative_Method); 
   Ada.Float_Text_IO.Put (Item => Averages2, Fore => 3, Aft  => 5, Exp  => 0);

   end Test_Number_Of_Averages;

With Fs = 48000, Time_Duration = 60, NFFT = 8192, both methods are returning 702.125. The second method though is always returning 702.125 irrespective of the Overlap_Fraction parameter. How to specify Overlap_Fraction in the calling function, while still keeping using it private i.e. by having the function use the value of Overlap_Fraction stored in the record.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:03:44+00:00Added an answer on June 10, 2026 at 12:03 pm

    In the body of Alternative_Method_Number_Of_Averages you are trying to use Number_Of_Averages, which is a private member of the parent type. And since it is private,
    you don’t have visibility of it.

    When you with a package you get visibility of the public part of the spec only.

    A child package will have visibility of the public part of the parent in its own public part, and visibility of the private part of the parent in its own private part.

    There’s also private childs, which have visibility of the whole parent in its own spec

    To get visibility of Number_Of_Averages, you can either move the full type declaration to the public part, or make Alternative_Method_Number_Of_Averages a child package of Conventional_Method_Number_Of_Averages.

    For more details, take a look at:
    http://en.wikibooks.org/wiki/Ada_Programming/Packages

    On another note, overriding occurs only when a type extension is implementing a subprogram with the same exact parameter profile as in the parent. In your example, Second_Method is overloading Count_Averages, ie, adding a new subprogram with the same name but different profile. Count_Averages is still inherited from the parent with the shorter parameter profile.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand the math of this code snippet. A token is provided which
Trying to understand the new async/await pattern, I have one question which I can't
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand, why my C++/Qt application creates 18 threads, while i don't create
Trying to understand this binding process of the WPF. See the code at the
After trying to understand why client code is not rendered in a page (injected
HI Trying to understand how __radd__ works. I have the code >>> class X(object):
I am trying understand why one of my printf statements fail. #1 below is
Trying to understand how the code below always brings up a number between 0-6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.