Разработка классов на языке Object Pascal. Соколов Е.В. - 45 стр.

UptoLike

Составители: 

45
function DynamicRVector.GetComp(i: Integer): Rational;
begin
if (i>=1) and (i<=Length(self.comp))
then result:=Rational.CreateEqualTo(self.comp[i-1])
else raise Exception.Create('Index is out of bounds');
end;
procedure DynamicRVector.SetComp(i: Integer; value: Rational);
begin
if (i>=1) and (i<=Length(self.comp))
then self.comp[i-1].Assign(value)
else raise Exception.Create('Index is out of bounds');
end;
function DynamicRVector.AsString: String;
var i: Integer;
begin
result:='(';
for i:=0 to Length(self.comp)-1 do begin
result:=result+self.comp[i].AsString;
if i<Length(self.comp)-1 then result:=result+', ';
end;
result:=result+')';
end;
constructor DynamicRVector.CreateEmpty(d: Integer);
begin
inherited Create; SetLength(self.comp, d);
end;
7. Единственным заметным отличием модуля, содержащего класс
DynamicRVector, от рассмотренных ранее является отсутствие объявле-
ния константы Dim. Теперь уже вся информация, описывающая объект,
содержится в нем самом .
Пример 2.4.5. Размещение класса DynamicRVector внутри модуля.
unit UnitDynamicRVector;
interface
uses UnitRational;
type DynamicRVector = class
private
comp: array of Rational;
constructor CreateEmpty(d: Integer);
public
function Plus (v: DynamicRVector): DynamicRVector;
function Minus (v: DynamicRVector): DynamicRVector;
function ScalarProduct(v: DynamicRVector): Rational;
function DynamicRVector.GetComp(i: Integer): Rational;
begin
  if (i>=1) and (i<=Length(self.comp))
     then result:=Rational.CreateEqualTo(self.comp[i-1])
     else raise Exception.Create('Index is out of bounds');
end;
procedure DynamicRVector.SetComp(i: Integer; value: Rational);
begin
  if (i>=1) and (i<=Length(self.comp))
     then self.comp[i-1].Assign(value)
     else raise Exception.Create('Index is out of bounds');
end;
function DynamicRVector.AsString: String;
var i: Integer;
begin
  result:='(';
  for i:=0 to Length(self.comp)-1 do begin
     result:=result+self.comp[i].AsString;
     if i