In Ada, child packages are a powerful feature that allows you to extend the functionality of a parent package. They help in organizing code into a hierarchical structure, making it more modular and easier to manage. This section will cover the basics of child packages, how to create them, and their benefits.

Key Concepts

  1. Parent Package: The main package that can be extended by child packages.
  2. Child Package: A package that extends the functionality of a parent package.
  3. Visibility: Child packages have access to the public entities of their parent packages.

Creating Child Packages

Syntax

To create a child package, you need to follow a specific naming convention and structure. Here is the basic syntax:

-- Parent package specification
package Parent_Package is
    -- Public declarations
end Parent_Package;

-- Parent package body
package body Parent_Package is
    -- Implementation of public declarations
end Parent_Package;

-- Child package specification
package Parent_Package.Child_Package is
    -- Public declarations specific to the child package
end Parent_Package.Child_Package;

-- Child package body
package body Parent_Package.Child_Package is
    -- Implementation of public declarations
end Parent_Package.Child_Package;

Example

Let's create a simple example to illustrate the concept of child packages.

Parent Package

-- File: parent_package.ads
package Parent_Package is
    procedure Greet;
end Parent_Package;

-- File: parent_package.adb
package body Parent_Package is
    procedure Greet is
    begin
        Ada.Text_IO.Put_Line("Hello from Parent Package!");
    end Greet;
end Parent_Package;

Child Package

-- File: parent_package.child_package.ads
package Parent_Package.Child_Package is
    procedure Greet_Child;
end Parent_Package.Child_Package;

-- File: parent_package.child_package.adb
package body Parent_Package.Child_Package is
    procedure Greet_Child is
    begin
        Ada.Text_IO.Put_Line("Hello from Child Package!");
    end Greet_Child;
end Parent_Package.Child_Package;

Using Child Packages

To use the child package, you need to with and use both the parent and child packages in your main program.

-- File: main.adb
with Parent_Package;
with Parent_Package.Child_Package;
use Parent_Package;
use Parent_Package.Child_Package;

procedure Main is
begin
    Greet;          -- Calls the procedure from Parent_Package
    Greet_Child;    -- Calls the procedure from Child_Package
end Main;

Benefits of Child Packages

  1. Modularity: Child packages help in breaking down large packages into smaller, more manageable units.
  2. Encapsulation: They allow for better encapsulation of related functionalities.
  3. Reusability: Code in child packages can be reused across different parts of the application.
  4. Maintainability: Easier to maintain and update specific parts of the code without affecting the entire package.

Practical Exercise

Exercise

  1. Create a parent package named Math_Operations with a procedure Add that adds two integers and prints the result.
  2. Create a child package named Math_Operations.Advanced with a procedure Multiply that multiplies two integers and prints the result.
  3. Write a main program to call both Add and Multiply procedures.

Solution

Parent Package

-- File: math_operations.ads
package Math_Operations is
    procedure Add(X, Y : Integer);
end Math_Operations;

-- File: math_operations.adb
package body Math_Operations is
    procedure Add(X, Y : Integer) is
    begin
        Ada.Text_IO.Put_Line("Sum: " & Integer'Image(X + Y));
    end Add;
end Math_Operations;

Child Package

-- File: math_operations.advanced.ads
package Math_Operations.Advanced is
    procedure Multiply(X, Y : Integer);
end Math_Operations.Advanced;

-- File: math_operations.advanced.adb
package body Math_Operations.Advanced is
    procedure Multiply(X, Y : Integer) is
    begin
        Ada.Text_IO.Put_Line("Product: " & Integer'Image(X * Y));
    end Multiply;
end Math_Operations.Advanced;

Main Program

-- File: main.adb
with Math_Operations;
with Math_Operations.Advanced;
use Math_Operations;
use Math_Operations.Advanced;

procedure Main is
begin
    Add(3, 4);          -- Calls the Add procedure from Math_Operations
    Multiply(3, 4);     -- Calls the Multiply procedure from Math_Operations.Advanced
end Main;

Summary

In this section, we learned about child packages in Ada, how to create them, and their benefits. Child packages allow for better modularity, encapsulation, reusability, and maintainability of code. We also went through a practical example and exercise to reinforce the concepts. In the next section, we will explore private types and private packages, which further enhance the encapsulation capabilities in Ada.

© Copyright 2024. All rights reserved