ZetCode

Ada operator overloading

last modified November 23, 2026

Operators are functions whose names happen to contain symbols. Defining them for a record type lets the rest of the program add and compare values instead of manipulating their components. The examples here build a small money type one operator at a time.

A plus operator

The type Amount stores a whole number of cents, which avoids rounding. The function "+" adds two amounts and returns a new one, and Image turns the cents into a string with a decimal point.

coins.ads
package Coins is
   type Amount is record
      Cents : Integer := 0;
   end record;

   function "+" (Left, Right : Amount) return Amount;
   function Image (A : Amount) return String;
end Coins;

The declaration of "+" sits in the same package specification as the type, so it is a primitive operation of Amount. Image is an ordinary function that keeps the output readable; it is not an operator.

coins.adb
with Ada.Strings;
with Ada.Strings.Fixed;

package body Coins is
   use Ada.Strings;
   use Ada.Strings.Fixed;

   function "+" (Left, Right : Amount) return Amount is
   begin
      return (Cents => Left.Cents + Right.Cents);
   end "+";

   function Image (A : Amount) return String is
      Value : constant Natural := abs A.Cents;
      Sign  : constant String := (if A.Cents < 0 then "-" else "");
      Rest  : constant String :=
        (if Value mod 100 < 10 then "0" else "")
        & Trim (Natural'Image (Value mod 100), Left);
   begin
      return Sign & Trim (Natural'Image (Value / 100), Left) & "." & Rest;
   end Image;

end Coins;

The body of "+" returns an aggregate with the sum of the cents. Image splits the absolute value into dollars and cents, Trim removes the leading space of 'Image, and a zero is inserted in front of a one-digit cent value.

coins_main.adb
with Ada.Text_IO;
with Coins;
use Ada.Text_IO;
use Coins;

procedure Coins_Main is
   Coffee : constant Amount := (Cents => 370);
   Cake   : constant Amount := (Cents => 250);
   Total  : constant Amount := Coffee + Cake;
begin
   Put_Line ("coffee " & Image (Coffee));
   Put_Line ("cake   " & Image (Cake));
   Put_Line ("total  " & Image (Total));
end Coins_Main;

The clause use Coins makes the operator directly visible, so Coffee + Cake is accepted; without it the call would have to be written Coins."+" (Coffee, Cake). The total is 370 + 250 = 620 cents, printed as 6.20.

$ gnatmake -gnat2022 -gnatwa coins_main.adb
$ ./coins_main
coffee 3.70
cake   2.50
total  6.20

Subtraction and equality

Subtraction follows the same pattern. Equality is more interesting: every record type already has a predefined "=" that compares all components, but a declaration of "=" hides it. Ada then declares a matching "/=" automatically, and it returns the negation of the new equality.

coins.ads
package Coins is
   type Amount is record
      Cents : Integer := 0;
   end record;

   function "+" (Left, Right : Amount) return Amount;
   function "-" (Left, Right : Amount) return Amount;
   function "=" (Left, Right : Amount) return Boolean;
   function Image (A : Amount) return String;
end Coins;

The specification now declares "-" and "=" next to "+". The equality compares the cents, which is exactly what two amounts should be equal by.

coins.adb
with Ada.Strings;
with Ada.Strings.Fixed;

package body Coins is
   use Ada.Strings;
   use Ada.Strings.Fixed;

   function "+" (Left, Right : Amount) return Amount is
   begin
      return (Cents => Left.Cents + Right.Cents);
   end "+";

   function "-" (Left, Right : Amount) return Amount is
   begin
      return (Cents => Left.Cents - Right.Cents);
   end "-";

   function "=" (Left, Right : Amount) return Boolean is
   begin
      return Left.Cents = Right.Cents;
   end "=";

   function Image (A : Amount) return String is
      Value : constant Natural := abs A.Cents;
      Sign  : constant String := (if A.Cents < 0 then "-" else "");
      Rest  : constant String :=
        (if Value mod 100 < 10 then "0" else "")
        & Trim (Natural'Image (Value mod 100), Left);
   begin
      return Sign & Trim (Natural'Image (Value / 100), Left) & "." & Rest;
   end Image;

end Coins;

The body of "-" subtracts the cents and the body of "=" compares them. Because "=" is declared, the compiler also provides "/=".

coins_main.adb
with Ada.Text_IO;
with Coins;
use Ada.Text_IO;
use Coins;

procedure Coins_Main is
   Coffee : constant Amount := (Cents => 370);
   Cake   : constant Amount := (Cents => 250);
   Total  : constant Amount := Coffee + Cake;
   Change : constant Amount := Total - Coffee;
begin
   Put_Line ("coffee " & Image (Coffee));
   Put_Line ("cake   " & Image (Cake));
   Put_Line ("total  " & Image (Total));
   Put_Line ("change " & Image (Change));
   Put_Line ("same   " & Boolean'Image (Change = Cake));
   Put_Line ("other  " & Boolean'Image (Change /= Cake));
end Coins_Main;

Change is 6.20 - 3.70 = 2.50, which equals Cake. The two comparisons therefore print TRUE and FALSE.

$ gnatmake -gnat2022 -gnatwa coins_main.adb
$ ./coins_main
coffee 3.70
cake   2.50
total  6.20
change 2.50
same   TRUE
other  FALSE

Ordering

Relational operators are functions too. A loop that looks for the largest amount only needs "<"; the expression Highest < P is a call of that function. Declaring "<" does not affect the other relations of the type.

coins.ads
package Coins is
   type Amount is record
      Cents : Integer := 0;
   end record;

   function "+" (Left, Right : Amount) return Amount;
   function "-" (Left, Right : Amount) return Amount;
   function "=" (Left, Right : Amount) return Boolean;
   function "<" (Left, Right : Amount) return Boolean;
   function Image (A : Amount) return String;
end Coins;

The new declaration compares two amounts. Its body is a single comparison of the cents.

coins.adb
with Ada.Strings;
with Ada.Strings.Fixed;

package body Coins is
   use Ada.Strings;
   use Ada.Strings.Fixed;

   function "+" (Left, Right : Amount) return Amount is
   begin
      return (Cents => Left.Cents + Right.Cents);
   end "+";

   function "-" (Left, Right : Amount) return Amount is
   begin
      return (Cents => Left.Cents - Right.Cents);
   end "-";

   function "=" (Left, Right : Amount) return Boolean is
   begin
      return Left.Cents = Right.Cents;
   end "=";

   function "<" (Left, Right : Amount) return Boolean is
   begin
      return Left.Cents < Right.Cents;
   end "<";

   function Image (A : Amount) return String is
      Value : constant Natural := abs A.Cents;
      Sign  : constant String := (if A.Cents < 0 then "-" else "");
      Rest  : constant String :=
        (if Value mod 100 < 10 then "0" else "")
        & Trim (Natural'Image (Value mod 100), Left);
   begin
      return Sign & Trim (Natural'Image (Value / 100), Left) & "." & Rest;
   end Image;

end Coins;

The body returns Left.Cents < Right.Cents. The operator is used by the loop exactly like the predefined one.

coins_main.adb
with Ada.Text_IO;
with Coins;
use Ada.Text_IO;
use Coins;

procedure Coins_Main is
   Coffee  : constant Amount := (Cents => 370);
   Cake    : constant Amount := (Cents => 250);
   Total   : constant Amount := Coffee + Cake;
   Change  : constant Amount := Total - Coffee;
   Prices  : constant array (1 .. 4) of Amount :=
     [(Cents => 250), (Cents => 370), (Cents => 120), (Cents => 500)];
   Highest : Amount := Prices (Prices'First);
begin
   Put_Line ("coffee " & Image (Coffee));
   Put_Line ("cake   " & Image (Cake));
   Put_Line ("total  " & Image (Total));
   Put_Line ("change " & Image (Change));
   Put_Line ("same   " & Boolean'Image (Change = Cake));
   Put_Line ("other  " & Boolean'Image (Change /= Cake));
   for P of Prices loop
      if Highest < P then
         Highest := P;
      end if;
   end loop;
   Put_Line ("highest " & Image (Highest));
end Coins_Main;

The array of prices is an Ada 2022 aggregate written with brackets. Highest starts as the first price, and the loop replaces it whenever a larger amount appears. The largest price is 5.00.

$ gnatmake -gnat2022 -gnatwa coins_main.adb
$ ./coins_main
coffee 3.70
cake   2.50
total  6.20
change 2.50
same   TRUE
other  FALSE
highest 5.00

Unary operators and scaling

An operator name can be overloaded by its profile. Unary minus has one parameter and binary minus has two, so both can be called "-". Multiplication of an amount by an integer factor is another ordinary function, with the amount first and the factor second.

coins.ads
package Coins is
   type Amount is record
      Cents : Integer := 0;
   end record;

   function "+" (Left, Right : Amount) return Amount;
   function "-" (Left, Right : Amount) return Amount;
   function "-" (A : Amount) return Amount;
   function "*" (A : Amount; Factor : Integer) return Amount;
   function "=" (Left, Right : Amount) return Boolean;
   function "<" (Left, Right : Amount) return Boolean;
   function Image (A : Amount) return String;
end Coins;

The specification now declares both forms of "-" and a "*" that takes an Amount and an Integer.

coins.adb
with Ada.Strings;
with Ada.Strings.Fixed;

package body Coins is
   use Ada.Strings;
   use Ada.Strings.Fixed;

   function "+" (Left, Right : Amount) return Amount is
   begin
      return (Cents => Left.Cents + Right.Cents);
   end "+";

   function "-" (Left, Right : Amount) return Amount is
   begin
      return (Cents => Left.Cents - Right.Cents);
   end "-";

   function "-" (A : Amount) return Amount is
   begin
      return (Cents => -A.Cents);
   end "-";

   function "*" (A : Amount; Factor : Integer) return Amount is
   begin
      return (Cents => A.Cents * Factor);
   end "*";

   function "=" (Left, Right : Amount) return Boolean is
   begin
      return Left.Cents = Right.Cents;
   end "=";

   function "<" (Left, Right : Amount) return Boolean is
   begin
      return Left.Cents < Right.Cents;
   end "<";

   function Image (A : Amount) return String is
      Value : constant Natural := abs A.Cents;
      Sign  : constant String := (if A.Cents < 0 then "-" else "");
      Rest  : constant String :=
        (if Value mod 100 < 10 then "0" else "")
        & Trim (Natural'Image (Value mod 100), Left);
   begin
      return Sign & Trim (Natural'Image (Value / 100), Left) & "." & Rest;
   end Image;

end Coins;

The unary body negates the cents; the scaling body multiplies them. No conversion is needed, because both results are amounts.

coins_main.adb
with Ada.Text_IO;
with Coins;
use Ada.Text_IO;
use Coins;

procedure Coins_Main is
   Change : constant Amount := (Cents => 250);
   Refund : constant Amount := -Change;
   Box    : constant Amount := Change * 3;
begin
   Put_Line ("refund  " & Image (Refund));
   Put_Line ("boxes   " & Image (Box));
   Put_Line ("negated " & Boolean'Image (-Change = Refund));
end Coins_Main;

The refund of 2.50 becomes -2.50, and three times 2.50 is 7.50. The last line compares the negated amount with the refund and prints TRUE.

$ gnatmake -gnat2022 -gnatwa coins_main.adb
$ ./coins_main
refund  -2.50
boxes   7.50
negated TRUE

An applied example: a basket

The basket program uses the Coins package unchanged from the previous section. An Item pairs a fixed-length name with an amount, and the aggregate lists four items. The loop adds each price to the total with the overloaded "+".

ledger_main.adb
with Ada.Text_IO;
with Coins;
use Ada.Text_IO;
use Coins;

procedure Ledger_Main is
   type Item is record
      Name  : String (1 .. 8);
      Price : Amount;
   end record;

   Basket : constant array (1 .. 4) of Item :=
     [(Name => "coffee  ", Price => (Cents => 370)),
      (Name => "cake    ", Price => (Cents => 250)),
      (Name => "juice   ", Price => (Cents => 320)),
      (Name => "cookie  ", Price => (Cents => 180))];

   Total  : Amount := (Cents => 0);
   Refund : constant Amount := (Cents => 250);
begin
   for B of Basket loop
      Put_Line (B.Name & Image (B.Price));
      Total := Total + B.Price;
   end loop;
   Put_Line ("total   " & Image (Total));
   Put_Line ("refund  " & Image (Refund));
   Put_Line ("to pay  " & Image (Total - Refund));
end Ledger_Main;

The total is 11.20. After the refund of 2.50 the customer pays 8.70. The program never mentions cents in its arithmetic: it adds, subtracts, and prints amounts, and the operators keep the representation hidden.

$ gnatmake -gnat2022 -gnatwa ledger_main.adb
$ ./ledger_main
coffee  3.70
cake    2.50
juice   3.20
cookie  1.80
total   11.20
refund  2.50
to pay  8.70

Next steps

The next article adds preconditions, postconditions, invariants, and predicates to subprograms and types. Those aspects are checked by the compiler when assertions are enabled, and they document the rules that an operator such as "+" expects.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have declared "+", "-", "=", "<", and "*" for a record type, used them in expressions, relied on the automatically declared "/=", and built a basket whose arithmetic reads like the problem it solves. The procedures and functions article explains the profiles that operators overload on.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.