ВУЗ:
Составители:
Рубрика:
37
begin
if (i>=1) and (i<=Dim)
then result:=Rational.CreateEqualTo(self.comp[i])
else raise Exception.Create('Index is out of bounds');
end;
procedure RVector.SetComp(i: Integer; value: Rational);
begin
if (i>=1) and (i<=Dim)
then self.comp[i].Assign(value)
else raise Exception.Create('Index is out of bounds');
end;
function RVector.AsString: String;
var i: Integer;
begin
result:='(';
for i:=1 to Dim do begin
result:=result+self.comp[i].AsString;
if i<Dim then result:=result+', ';
end;
result:=result+')';
end;
constructor RVector.CreateEmpty;
begin inherited Create; end;
constructor RVector.Create;
var i: Integer;
begin
inherited Create;
for i:=1 to Dim do self.comp[i]:=Rational.Create;
end;
destructor RVector.Destroy;
var i: Integer;
begin
for i:=1 to Dim do self.comp[i].Destroy;
inherited Destroy;
end;
end.
Теперь демонстрационная программа из примера 2.3.2 успешно
работает и выдает корректный результат. Но успокаиваться рано: утечке
памяти подвержен еще один метод — ScalarProduct. Чтобы в этом убе-
диться, достаточно подсчитать количество объектов класса Rational, соз-
даваемых в процессе его работы.
Первый из них размещается в памяти инструкцией
result:=Rational.Create;
begin
if (i>=1) and (i<=Dim)
then result:=Rational.CreateEqualTo(self.comp[i])
else raise Exception.Create('Index is out of bounds');
end;
procedure RVector.SetComp(i: Integer; value: Rational);
begin
if (i>=1) and (i<=Dim)
then self.comp[i].Assign(value)
else raise Exception.Create('Index is out of bounds');
end;
function RVector.AsString: String;
var i: Integer;
begin
result:='(';
for i:=1 to Dim do begin
result:=result+self.comp[i].AsString;
if iСтраницы
- « первая
- ‹ предыдущая
- …
- 35
- 36
- 37
- 38
- 39
- …
- следующая ›
- последняя »
