program ok;
uses
crt;
type
TVector=object
x,y:integer;
procedure Init(x1,x2,y1,y2:integer);
procedure SetVector(valueX,valueY:integer);
procedure GetVector(var valueX:integer;var valueY:integer);
end;
TOperation=object
vector1,vector2:TVector;
procedure SetVectors(valueVector1,valueVector2:TVector);
end;
TOperationAdd=object(TOperation)
procedure Add(var result:TVector);
end;
TOperationSub=object(TOperationAdd)
procedure Sub(var result:TVector);
end;
procedure TVector.Init(x1,x2,y1,y2:integer);
begin
x:=x2-x1;
y:=y2-y1;
end;
procedure TVector.SetVector(valueX,valueY:integer);
begin
x:=valueX;
y:=valueY;
end;
procedure TVector.GetVector(var valueX:integer;var valueY:integer);
begin
valueX:=x;
valueY:=y;
end;
procedure TOperation.SetVectors(valueVector1,valueVector2:TVector);
begin
vector1:=valueVector1;
vector2:=valueVector2;
end;
procedure TOperationAdd.Add(var result:TVector);
var
vectorX1,vectorX2,vectorY1,vectorY2:integer;
begin
vector1.GetVector(vectorX1,VectorY1);
vector2.GetVector(vectorX2,VectorY2);
result.SetVector(vectorX1+vectorX2,vectorY1+vectorY2);
end;
procedure TOperationSub.Sub(var result:TVector);
var
vectorX1,vectorX2,vectorY1,vectorY2:integer;
begin
vector1.GetVector(vectorX1,VectorY1);
vector2.GetVector(vectorX2,VectorY2);
result.SetVector(vectorX2-vectorX1,vectorY2-vectorY1);
end;
var
x,y,x1,x2,y1,y2 :integer;
vector1,vector2,result:TVector;
operationAdd :TOperationAdd;
operationSub :TOperationSub;
begin
clrscr;
randomize;
x1:=random(10);
x2:=random(10);
y1:=random(10);
y2:=random(10);
vector1.Init(x1,x2,y1,y2);
vector1.GetVector(x,y);
writeln('A(',x1,',',y1,'); B(',x2,',',y2,'); AB(',x:1,',',y,')');
x1:=random(10);
x2:=random(10);
y1:=random(10);
y2:=random(10);
vector2.Init(x1,x2,y1,y2);
vector2.GetVector(x,y);
writeln('C(',x1,',',y1,'); D(',x2,',',y2,'); CD(',x,',',y,')');
operationAdd.SetVectors(vector1,vector2);
operationAdd.Add(result);
result.GetVector(x,y);
writeln('Add: (',x,',',y,')');
operationSub.SetVectors(vector1,vector2);
operationSub.Sub(result);
result.GetVector(x,y);
writeln('Sub: (',x,',',y,')');
readkey;
end.
Полиморфизм. Таблица виртуальных методов
Измените, пожалуйста, рабочую программу, чтоб та использовалась VMT (таблица витуальных методов).
Код:
program ok;
uses
crt;
type
TVector=object
x,y:integer;
procedure Init(x1,x2,y1,y2:integer);
procedure SetVector(valueX,valueY:integer);
procedure GetVector(var valueX:integer;var valueY:integer);
end;
TOperation=object
vector1,vector2:TVector;
procedure SetVectors(valueVector1,valueVector2:TVector);
procedure Execute(var Result: TVector); virtual;
end;
TOperationAdd=object(TOperation)
procedure Execute(var result:TVector); virtual;
end;
TOperationSub=object(TOperationAdd)
procedure Execute(var result:TVector); virtual;
end;
procedure TVector.Init(x1,x2,y1,y2:integer);
begin
x:=x2-x1;
y:=y2-y1;
end;
procedure TVector.SetVector(valueX,valueY:integer);
begin
x:=valueX;
y:=valueY;
end;
procedure TVector.GetVector(var valueX:integer;var valueY:integer);
begin
valueX:=x;
valueY:=y;
end;
procedure TOperation.SetVectors(valueVector1,valueVector2:TVector);
begin
vector1:=valueVector1;
vector2:=valueVector2;
end;
procedure TOperation.Execute(var Result: TVector);
begin
end;
procedure TOperationAdd.Execute(var result:TVector);
var
vectorX1,vectorX2,vectorY1,vectorY2:integer;
begin
vector1.GetVector(vectorX1,VectorY1);
vector2.GetVector(vectorX2,VectorY2);
result.SetVector(vectorX1+vectorX2,vectorY1+vectorY2);
end;
procedure TOperationSub.Execute(var result:TVector);
var
vectorX1,vectorX2,vectorY1,vectorY2:integer;
begin
vector1.GetVector(vectorX1,VectorY1);
vector2.GetVector(vectorX2,VectorY2);
result.SetVector(vectorX2-vectorX1,vectorY2-vectorY1);
end;
var
x,y,x1,x2,y1,y2 :integer;
vector1,vector2,result:TVector;
operationAdd :TOperationAdd;
operationSub :TOperationSub;
begin
clrscr;
randomize;
x1:=random(10);
x2:=random(10);
y1:=random(10);
y2:=random(10);
vector1.Init(x1,x2,y1,y2);
vector1.GetVector(x,y);
writeln('A(',x1,',',y1,'); B(',x2,',',y2,'); AB(',x:1,',',y,')');
x1:=random(10);
x2:=random(10);
y1:=random(10);
y2:=random(10);
vector2.Init(x1,x2,y1,y2);
vector2.GetVector(x,y);
writeln('C(',x1,',',y1,'); D(',x2,',',y2,'); CD(',x,',',y,')');
operationAdd.SetVectors(vector1,vector2);
operationAdd.Execute(result);
result.GetVector(x,y);
writeln('Add: (',x,',',y,')');
operationSub.SetVectors(vector1,vector2);
operationSub.Execute(result);
result.GetVector(x,y);
writeln('Sub: (',x,',',y,')');
readkey;
end.
uses
crt;
type
TVector=object
x,y:integer;
procedure Init(x1,x2,y1,y2:integer);
procedure SetVector(valueX,valueY:integer);
procedure GetVector(var valueX:integer;var valueY:integer);
end;
TOperation=object
vector1,vector2:TVector;
procedure SetVectors(valueVector1,valueVector2:TVector);
procedure Execute(var Result: TVector); virtual;
end;
TOperationAdd=object(TOperation)
procedure Execute(var result:TVector); virtual;
end;
TOperationSub=object(TOperationAdd)
procedure Execute(var result:TVector); virtual;
end;
procedure TVector.Init(x1,x2,y1,y2:integer);
begin
x:=x2-x1;
y:=y2-y1;
end;
procedure TVector.SetVector(valueX,valueY:integer);
begin
x:=valueX;
y:=valueY;
end;
procedure TVector.GetVector(var valueX:integer;var valueY:integer);
begin
valueX:=x;
valueY:=y;
end;
procedure TOperation.SetVectors(valueVector1,valueVector2:TVector);
begin
vector1:=valueVector1;
vector2:=valueVector2;
end;
procedure TOperation.Execute(var Result: TVector);
begin
end;
procedure TOperationAdd.Execute(var result:TVector);
var
vectorX1,vectorX2,vectorY1,vectorY2:integer;
begin
vector1.GetVector(vectorX1,VectorY1);
vector2.GetVector(vectorX2,VectorY2);
result.SetVector(vectorX1+vectorX2,vectorY1+vectorY2);
end;
procedure TOperationSub.Execute(var result:TVector);
var
vectorX1,vectorX2,vectorY1,vectorY2:integer;
begin
vector1.GetVector(vectorX1,VectorY1);
vector2.GetVector(vectorX2,VectorY2);
result.SetVector(vectorX2-vectorX1,vectorY2-vectorY1);
end;
var
x,y,x1,x2,y1,y2 :integer;
vector1,vector2,result:TVector;
operationAdd :TOperationAdd;
operationSub :TOperationSub;
begin
clrscr;
randomize;
x1:=random(10);
x2:=random(10);
y1:=random(10);
y2:=random(10);
vector1.Init(x1,x2,y1,y2);
vector1.GetVector(x,y);
writeln('A(',x1,',',y1,'); B(',x2,',',y2,'); AB(',x:1,',',y,')');
x1:=random(10);
x2:=random(10);
y1:=random(10);
y2:=random(10);
vector2.Init(x1,x2,y1,y2);
vector2.GetVector(x,y);
writeln('C(',x1,',',y1,'); D(',x2,',',y2,'); CD(',x,',',y,')');
operationAdd.SetVectors(vector1,vector2);
operationAdd.Execute(result);
result.GetVector(x,y);
writeln('Add: (',x,',',y,')');
operationSub.SetVectors(vector1,vector2);
operationSub.Execute(result);
result.GetVector(x,y);
writeln('Sub: (',x,',',y,')');
readkey;
end.
P. S.: Такие темы нужно размещать в разделе "Студентам".
2 KIV: Спасибо большое, но сумма и разность векторов не выводится на экран :(