You get a bonus - 1 coin for daily activity. Now you have 1 coin

Object Oriented Programming Introduction

Lecture



  • 7. Object Oriented Programming
    • 7.1 Object Type
    • 7.2 Encapsulation
    • 7.3 Inheritance
    • 7.4 Polymorphism


7.1. Object type

An object can be viewed as an improvement of the record type, in which the description of the properties and parameters of the modeled entity are supplemented by methods - descriptions of actions with the object. Unlike writing, an object is declared the word object .
Example: create the simplest object: the position on the screen in graphical mode:
program oop;
uses graph;
type pozicia = object
x, y: integer;
procedure init (xn, yn: integer);
procedure locate (var xl, yl: integer);
end;
procedure pozicia.init;
begin
x: = xn;
y: = yn;
end;
procedure pozicia.locate;
begin
xl: = x;
yl: = y;
end;
var d, r, xx, yy: integer;
p: pozicia;
begin
d: = detect;
randomize;
initgraph (d, r, 'c: \ tp \ bgi');
p.init (random (GetMaxX), random (GetMaxY));
closegraph;
p.locate (xx, yy);
write (xx, yy);
end.


7.2. Encapsulation.

One of the main properties of OOP is encapsulation - closure in the common shell (Object ... end) of all components of the description. In this case, the fields are global for the methods of this object, since fields and methods have a common scope, then the coincidence of field names and formal parameters of methods is not permissible. Blocks-methods are taken out for the description of the object type. The names of the method blocks belonging to different types may be the same. Even if the names match, the method headers will be different, because consist of a prefix (type name) and a method name.
Access to the fields of objects from outside can be forcibly restricted. To do this, the group of fields in the object description is enclosed in Private Public brackets After this field will be available only to the methods of this module.


7.3. Inheritance.

Primitive objects are not used as program modules, but are used as carriers of common properties and methods. Such objects are called parental . Objects based on parent are called children . The parent type not used to describe variables is called abstract . The type descendant inherits all fields of the father type. Among them are all the fields inherited by the father, if he has ancestors. Increasing the number of fields in a child is optional. Methods can also be inherited, but selectively. A description of the type of descendant has a distinctive detail - the name of the type of father:
= object ()
With an increase in the complexity of objects, the number of actions increases, which can be replaced by building a new method, and method names are created as if the objects did not have a related relationship. The identical designation of functional-like methods simplifies not only the perception of a system of objects, but also programming.
An important detail of using inheritance in programs is the use of assigning objects to the values ​​of objects. The assignment A: = B is valid if A and B are of the same type, A is the ancestor of B, or for each field A there is a corresponding field in B.


7.4. Polymorphism.

Polymorphism involves the definition of a class or several classes for related object types so that each class has its own functional role. Methods of a single class are usually given a common name. In a situation when it is necessary to use a complex method in several objects and the differences in the behavior of objects are minimal, it is possible to create an adjacent complex method with making distinctions into replaceable subordinate methods. This method is called constructive polymorphism . This idea is being implemented by creating virtual replacement methods. The title of such a method contains the word virtual , and to connect them to a common method, refer to the constructor — a block with a special header.
constructor ()
The constructor must be addressed for each object using virtual methods.
Task: type kom - son of type pozicia represents filled squares with side length raz (in pixels). The inherited fields x, y are the coordinates of the center of the square. The kom.zoom procedure increases (decreases) an object if the argument znak> 0 (znak <= 0). The length of raz is changed to 2 * delt, where delt is another argument.

uses graph, crt;
type pozicia = object
x, y: integer;
procedure init (xn, yn: integer);
procedure locat (var xl, yl: integer);
end;
kom = object (pozicia)
cvet, raz: word;
procedure init (xn, yn: integer; color: word);
procedure zoom (delt, znak: integer);
end;
procedure pozicia.init;
begin
x: = xn;
y: = yn;
end;
procedure pozicia.locat;
begin
xl: = x;
yl: = y;
end;
procedure kom.init;
begin
pozicia.init (xn, yn);
cvet: = color;
raz: = 1;
end;
procedure kom.zoom;
var j, d: integer;
begin
if znak> 0 then setcolor (cvet)
else setcolor (getBkcolor);
for j: = 1 to delt do
begin
if znak> 0 then raz: = raz + 2;
d: = raz div 2;
moveto (xd, yd);
linerel (d + d, 0);
linerel (0, d + d);
linerel (-dd, 0);
linerel (0, -dd);
if (znak <= 0) and (raz> 1) then raz: = raz-2;
end;
end;
const n = 50;
var j, d, r, xx, yy: integer;
kvad: array [1..n] of kom;
begin
d: = detect;
randomize;
initgraph (d, r, 'c: \ tp \ bgi');
for j: = 1 to n do
kvad [j] .init (random (GetMaxX), random (GetMaxY), random (GetMaxColor);
repeat
delay (100);
j: = random (n) +1;
kvad [j] .zoom (random (8) +1, random (3) -1);
until kepressed;
closegraph;
kvad [1] .locat (xx, yy);
write (xx, '', yy);
readln;
end.



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Object oriented programming

Terms: Object oriented programming