Delphi .NET - Operator
Overloading
Example
- Sample source code
| Operator |
Category |
Declaration Signature |
Symbol Mapping |
| Implicit
|
Conversion |
Implicit(a : type) :
resultType; |
implicit typecast |
| Explicit
|
Conversion |
Explicit(a: type) :
resultType; |
explicit typecast |
| Negative
|
Unary |
Negative(a: type) :
resultType; |
- |
| Positive
|
Unary |
Positive(a: type):
resultType; |
+ |
| Inc |
Unary |
Inc(a: type) :
resultType; |
Inc
|
| Dec |
Unary |
Dec(a: type):
resultType |
Dec
|
| LogicalNot
|
Unary |
LogicalNot(a: type):
resultType; |
not
|
| BitwiseNot
|
Unary |
BitwiseNot(a: type):
resultType; |
not
|
| Trunc |
Unary |
Trunc(a: type):
resultType; |
Trunc
|
| Round |
Unary |
Round(a: type):
resultType; |
Round
|
| Equal |
Comparison |
Equal(a: type; b: type) :
Boolean; |
= |
| NotEqual
|
Comparison |
NotEqual(a: type; b:
type): Boolean; |
<>
|
| GreaterThan
|
Comparison |
GreaterThan(a: type; b:
type) Boolean; |
> |
| GreaterThanOrEqual
|
Comparison |
GreaterThanOrEqual(a:
type; b: type): resultType; |
>= |
| LessThan
|
Comparison |
LessThan(a: type; b:
type): resultType; |
< |
| LessThanOrEqual
|
Comparison |
LessThanOrEqual(a: type;
b: type): resultType; |
<= |
| Add |
Binary |
Add(a: type; b: type):
resultType; |
+ |
| Subtract
|
Binary |
Subtract(a: type; b:
type) : resultType; |
- |
| Multiply
|
Binary |
Multiply(a: type; b:
type) : resultType; |
* |
| Divide |
Binary |
Divide(a: type; b: type)
: resultType; |
/ |
| IntDivide
|
Binary |
IntDivide(a: type; b:
type): resultType; |
div
|
| Modulus |
Binary |
Modulus(a: type; b:
type): resultType; |
mod
|
| ShiftLeft
|
Binary |
ShiftLeft(a: type; b:
type): resultType; |
shl
|
| ShiftRight
|
Binary |
ShiftRight(a: type; b:
type): resultType; |
shr
|
| LogicalAnd
|
Binary |
LogicalAnd(a: type; b:
type): resultType; |
and
|
| LogicalOr
|
Binary |
LogicalOr(a: type; b:
type): resultType; |
or
|
| LogicalXor
|
Binary |
LogicalXor(a: type; b:
type): resultType; |
xor
|
| BitwiseAnd
|
Binary |
BitwiseAnd(a: type; b:
type): resultType; |
and
|
| BitwiseOr
|
Binary |
BitwiseOr(a: type; b:
type): resultType; |
or
|
| BitwiseXor
|
Binary |
BitwiseXor(a: type; b:
type): resultType; |
xor
|
program Matrici;
{
Delphi.NET supporta l'overloading dei seguenti operatori:
+, -, *, /, =, <>, >, >=, <, <=, inc, dec, xor, and, or, not,
div, mod, shl, shr, trunc, round, typecast implicito, typecast esplicito.
Il .NET Framework dispone di una classe Matrix (matrice affine 3x3),
non la useremo. Lo
scopo del seguente frammento di codice è mostrare come eseguire l'operator overloading
in Delphi.NET. L'esempio (puramente didattico) non è di
certo il massimo dell'eleganza ma rende molto l'idea!
Salvate questo listato nel file Matrici.dpr e compilatelo con
il comando: dccil Matrici.dpr
}
{$APPTYPE CONSOLE}
uses
System.Text;
type TMatrice = class // Classe Matrice (eredita da TObject)
strict private
Righe, Colonne: Integer;
public
Dati:array of array of double; // Pubblica x semplicità
constructor Create(R,C:integer;Casuale:Boolean); // Crea la matrice
destructor Destroy; override; // Elimina la matrice
function ToString:String; override; // Converte in stringa
class operator Add(a, b: TMatrice): TMatrice; // Somma
class operator Multiply(a, b: TMatrice): TMatrice; // Prodotto tra matrici
class operator Multiply(a: double; b: TMatrice): TMatrice; // Prodotto con scalare
class operator Multiply(a: TMatrice; b: double): TMatrice; // Prodotto con scalare
class operator Negative(a: TMatrice):TMatrice; // Inverte il segno degli elementi
// Altri metodi, operatori, etc.
end;
// Overloading dell'operatore + (Somma)
class operator TMatrice.Add(a, b: TMatrice): TMatrice;
var i,j:integer;
begin
// Verifica la possibilità di eseguire la somma:
if (A.Righe <> B.Righe) or (A.Colonne <> B.Colonne) then
raise EMathError.Create('Dimensioni errate! Impossibile eseguire la somma!');
Result:=TMatrice.Create(A.Righe,A.Colonne,FALSE);
for i:=0 to A.Righe-1 do
for j:=0 to A.Colonne-1 do
Result.Dati[i,j]:=A.Dati[i,j]+B.Dati[i,j]; // Calcola A+B
end;
// Prodotto tra matrici * (Prodotto)
class operator TMatrice.Multiply(a, b: TMatrice): TMatrice;
var i,j,k:integer;
begin
// Verifica la possibilità di eseguire il prodotto:
if (A.Colonne <> B.Righe) then
raise EMathError.Create('Dimensioni errate! Impossibile eseguire il prodotto!');
Result := TMatrice.Create(A.Righe, B.Colonne, FALSE);
for k:=0 to A.Colonne-1 do
for j:=0 to Result.Colonne-1 do
for i:=0 to Result.Righe-1 do
Result.Dati[i,j] := Result.Dati[i,j] + (A.Dati[i,k]*B.Dati[k,j]);
end;
// Scalare * Matrice
class operator TMatrice.Multiply(a: double; b: TMatrice): TMatrice;
var i,j:integer;
begin
Result:=TMatrice.Create(B.Righe,B.Colonne,FALSE);
for i:=0 to B.Righe-1 do
for j:=0 to B.Colonne-1 do
Result.Dati[i,j]:=a*B.Dati[i,j]; // a*B
end;
// Matrice * Scalare
class operator TMatrice.Multiply(a: TMatrice; b: double): TMatrice;
var i,j:integer;
begin
Result:=TMatrice.Create(A.Righe,A.Colonne,FALSE);
for i:=0 to A.Righe-1 do
for j:=0 to A.Colonne-1 do
Result.Dati[i,j]:=b*A.Dati[i,j]; // b*A
end;
// Operatore unario -
class operator TMatrice.Negative(a: TMatrice):TMatrice;
var i,j:integer;
begin
Result:=TMatrice.Create(A.Righe,A.Colonne,FALSE);
for i:=0 to A.Righe-1 do
for j:=0 to A.Colonne-1 do
Result.Dati[i,j] := - A.Dati[i,j]; // Inverti i segni--> -A
end;
constructor TMatrice.Create(r, c: integer;Casuale:Boolean); // Costruttore
var i,j:integer;
begin
inherited Create;
Righe := R; // Righe e Colonne sono variabili private
Colonne := C;
SetLength(Dati, Righe, Colonne); // Imposta la dimensione
// Matrice formata da valori casuali [0,100].
// Nell'esempio uso degli interi per maggiore chiarezza
if Casuale then // Utile per le prove...
for i:=0 to Righe - 1 do
for j:=0 to Colonne - 1 do
Dati[i,j]:=Random(101)
else // Crea una matrice nulla
for i:=0 to Righe - 1 do
for j:=0 to Colonne - 1 do
Dati[i,j]:=0; // Matrice formata da elementi nulli
end;
destructor TMatrice.Destroy; // Distruttore
begin
Dati := NIL;
inherited;
end;
var A,B,C:TMatrice; // Ho bisogno di 3 matrici
procedure CreaMatrici; // Crea 3 matrici random (3x3)
begin
A:=TMatrice.Create(3,3,TRUE); // TRUE--> Matrice CASUALE
B:=TMatrice.Create(3,3,TRUE);
C:=TMatrice.Create(3,3,TRUE);
end;
procedure CreaMatriciErrate;
begin
A:=TMatrice.Create(2,3,TRUE); // A appartiene a R^(2x3)
B:=TMatrice.Create(5,2,TRUE); // Non posso calcolare A+B!
end;
function TMatrice.ToString: String; // Stampa la matrice in modo "carino"
var Temp:StringBuilder;
i,j:integer;
begin
Temp := StringBuilder.Create;
Temp.Append(Environment.NewLine); // Nuova riga
for i:=0 to Righe-1 do
begin
for j:=0 to Colonne-1 do
Temp.Append(Convert.ToString(Dati[i,j]).PadLeft(6,' ')+' ');
// PadLeft formatta la stringa inserendo un numero opportuno di spazi
Temp.Append(Environment.NewLine); // Nuova riga
end;
Result := Temp.ToString; // Restituisci la matrice
end;
begin
CreaMatrici;
Console.WriteLine('A = '+A.ToString);
Console.WriteLine('B = '+B.ToString);
C:=A+B; // SOMMA
Console.WriteLine('A + B ='+C.ToString); // A+B
C:=2*B; // PRODOTTO SCALARE-MATRICE
Console.WriteLine('2 * B ='+C.ToString); // 2*B
C:=A*10; // PRODOTTO MATRICE-SCALARE
Console.WriteLine('A * 10 ='+C.ToString); // A*10
C:=A*B;
Console.WriteLine('A * B ='+C.ToString); // A*B
C:=-A;
Console.WriteLine('-A = '+C.ToString); // -A
// Usa matrici delle dimensioni sbagliate:
CreaMatriciErrate;
try
C:=A+B; // Errore!
Console.WriteLine('A + B ='+C.ToString); // Non arriverò mai qui
except on E:EMathError do Console.WriteLine(E.Message);
end;
try
// Le matrici hanno dimensioni incompatibili con il prodotto
C:=A*B; // Errore!
Console.WriteLine('A * B ='+C.ToString); // Non arriverò mai qui
except on E:EMathError do Console.WriteLine(E.Message);
end;
Console.ReadLine; // Attendi la pressione del tasto ENTER
end.
Click here - Clicca qui