Menu English Ukrainian russian Home

Free technical library for hobbyists and professionals Free technical library


Lecture notes, cheat sheets
Free library / Directory / Lecture notes, cheat sheets

Computer science and information technology. Compatibility of object types (lecture notes)

Lecture notes, cheat sheets

Directory / Lecture notes, cheat sheets

Comments on the article Comments on the article

Table of contents (expand)

LECTURE #11. Object data type

1. Object type in Pascal. The concept of an object, its description and use

Historically, the first approach to programming has been procedural programming, otherwise known as bottom-up programming. Initially, common libraries of standard programs used in various fields of computer application were created. Then, based on these programs, more complex programs were created to solve specific problems.

However, computer technology was constantly developing, it began to be used to solve various problems of production, the economy, and therefore it became necessary to process data of various formats and solve non-standard problems (for example, non-numerical ones). Therefore, when developing programming languages, they began to pay attention to the creation of various types of data. This contributed to the emergence of such complex data types as combined, multiple, string, file, etc. Before solving the problem, the programmer carried out decomposition, i.e., splitting the task into several subtasks, for each of which a separate module was written. The main programming technology included three stages:

1) top-down design;

2) modular programming;

3) structural coding.

But starting from the mid-60s of the XX century, new concepts and approaches began to form, which formed the basis of the technology of object-oriented programming. In this approach, modeling and description of the real world is carried out at the level of concepts of a specific subject area to which the problem being solved belongs.

Object-oriented programming is a programming technique that closely resembles our behavior. It is a natural evolution of earlier innovations in programming language design. Object-oriented programming is more structural than all previous developments regarding structured programming. It is also more modular and more abstract than previous attempts at data abstraction and programming details internally. An object-oriented programming language is characterized by three main properties:

1) Encapsulation. Combining records with procedures and functions that manipulate the fields of these records forms a new data type - an object;

2) Inheritance. Definition of an object and its further use to build a hierarchy of child objects with the ability for each child object related to the hierarchy to access the code and data of all parent objects;

3) Polymorphism. Giving an action a single name, which is then shared up and down the hierarchy of objects, with each object in the hierarchy performing that action in a way that suits it.

Speaking of the object, we introduce a new data type - object. An object type is a structure consisting of a fixed number of components. Each component is either a field containing data of a strictly defined type, or a method that performs operations on an object. By analogy with the declaration of variables, the declaration of a field specifies the data type of this field and the identifier naming the field: by analogy with the declaration of a procedure or function, the declaration of a method specifies the title of a procedure, function, constructor, or destructor.

An object type can inherit components of another object type. If type T2 inherits from type T1, then type T2 is a child of type T1, and type T1 itself is a parent of type T2. Inheritance is transitive, i.e. if TK inherits from T2, and T2 inherits from T1, then TK inherits from T1. The scope (domain) of an object type consists of itself and all its descendants.

The following source code is an example of an object type declaration, type

type

point = object

X, Y: integer;

end;

Rect = object

A, B: TPoint;

procedure Init(XA, YA, XB, YB: Integer);

procedure Copy(var R: TRectangle);

procedure Move(DX, DY: Integer);

procedure Grow(DX, DY: Integer);

procedure Intersect(var R: TRectangle);

procedure Union(var R: TRectangle);

function Contains(P: Point): Boolean;

end;

StringPtr = ^String;

FieldPtr = ^TField;

TField = object

X, Y, Len: Integer;

Name: StringPtr;

constructor Copy(var F: TField);

constructor Init(FX, FY, FLen: Integer; FName: String);

destructor Done; virtual;

procedure Display; virtual;

procedure Edit; virtual;

function GetStr: String; virtual;

function PutStr(S: String): Boolean; virtual;

end;

StrFieldPtr = ^TStrField;

StrField = object(TField)

Value: PString;

constructor Init(FX, FY, FLen: Integer; FName: String);

destructor Done; virtual;

function GetStr: String; virtual;

function PutStr(S: String): Boolean;

virtual;

function Get:string;

procedure Put(S: String);

end;

NumFieldPtr = ^TNumField;

TNumField = object(TField)

private

Value, Min, Max: Longint;

public insurance

constructor Init(FX, FY, FLen: Integer; FName: String;

FMin, FMax: Longint);

function GetStr: String; virtual;

function PutStr(S: String): Boolean; virtual;

function Get: Longint;

function Put(N: Longint);

end;

ZipFieldPtr = ^TZipField;

ZipField = object(TNumField)

function GetStr: String; virtual;

function PutStr(S: String): Boolean;

virtual;

end.

Unlike other types, object types can only be declared in the type declaration section at the outermost level of the scope of a program or module. Thus, object types cannot be declared in a variable declaration section or inside a procedure, function, or method block.

A file type component type cannot have an object type or any structure type containing object type components.

2. Inheritance

The process by which one type inherits the characteristics of another type is called inheritance. The descendant is called the derived (child) type, and the type that the child type inherits from is called the parent (parent) type.

Previously known Pascal record types cannot inherit. However, Borland Pascal extends the Pascal language to support inheritance. One of these extensions is a new data structure category related to records, but much more powerful. The data types in this new category are defined using the new reserved word "object". An object type can be defined as a complete, independent type in the manner of describing Pascal entries, but it can also be defined as a descendant of an existing object type by putting the parent type in parentheses after the reserved word "object".

3. Instantiate Objects

An instance of an object is created by declaring a variable or constant of an object type, or by applying the standard New procedure to a variable of type "pointer to object type". The resulting object is called an instance of the object type;

var

F: TField;

Z: TZipField;

FP:PField;

ZP: PZipField;

Given these variable declarations, F is an instance of TField and Z is an instance of TZipField. Similarly, after applying New to FP and ZP, FP will point to a TField instance, and ZP will point to a TZipField instance.

If an object type contains virtual methods, then instances of that object type must be initialized by calling a constructor before calling any virtual method.

Below is an example:

var

S: StrField;

bend

S.Init(1, 1, 25, 'First name');

S.Put('Vladimir');

S.Display;

...

S Done;

end.

If S.Init was not called, then calling S.Display will cause this example to fail.

Assigning an instance of an object type does not imply initialization of the instance. An object is initialized by compiler-generated code that runs between the invocation of the constructor and the point at which execution actually reaches the first statement in the constructor's code block.

If the object instance is not initialized and range checking is enabled (by the {SR+} directive), then the first call to the virtual method of the object instance gives a run-time error. If range checking is disabled (by the {SR-} directive), then the first call to a virtual method of an uninitialized object can lead to unpredictable behavior.

The mandatory initialization rule also applies to instances that are components of struct types. For example:

var

Comment: array [1..5] of TStrField;

I: integer

begin

for I := 1 to 5 do

Comment [I].Init (1, I + 10, 40, 'first_name');

.

.

.

for I := 1 to 5 do Comment [I].Done;

end;

For dynamic instances, initialization is typically about placement and cleanup is about deletion, which is achieved through the extended syntax of the New and Dispose standard procedures. For example:

var

SP: StrFieldPtr;

begin

New(SP, Init(1, 1, 25, 'first_name');

SP^.Put('Vladimir');

SP^.Display;

.

.

.

Dispose(SP, Done);

end.

A pointer to an object type is assignment compatible with a pointer to any parent object type, so at runtime a pointer to an object type can point to an instance of that type or to an instance of any child type.

For example, a pointer of type ZipFieldPtr can be assigned to pointers of type PZipField, PNumField, and PField, and at runtime, a pointer of type PField can either be nil or point to an instance of TField, TNumField, or TZipField, or any instance of a child type of TField. .

These assignment pointer compatibility rules also apply to object type parameters. For example, the TField.Cop method can be passed instances of TField, TStrField, TNumField, TZipField, or any other type child of TField.

4. Components and Scope

The scope of a bean identifier extends beyond the object type. Moreover, the scope of a bean identifier extends through the blocks of procedures, functions, constructors, and destructors that implement the methods of the object type and its descendants. Based on these considerations, the spelling of the component identifier must be unique within the object type and within all its descendants, as well as within all its methods.

The scope of the component identifier described in the private part of the type declaration is limited to the module (program) that contains the object type declaration. In other words, private identifier beans act like ordinary public identifiers within the module that contains the object type declaration, and outside the module any private beans and identifiers are unknown and inaccessible. By putting related types of objects in the same module, you can make sure that these objects can access each other's private components, and these private components will be unknown to other modules.

In an object type declaration, a method header may specify the parameters of the object type being described, even if the description is not yet complete.

Author: Tsvetkova A.V.

<< Back: Object data type (Object type in Pascal. The concept of an object, its description and use. Inheritance. Creating instances of objects. Components and scope)

>> Forward: Object Type Compatibility (Encapsulation. Extensible objects. Object type compatibility)

We recommend interesting articles Section Lecture notes, cheat sheets:

General and clinical immunology. Lecture notes

Control theory. Crib

Civil law. Part II. Crib

See other articles Section Lecture notes, cheat sheets.

Read and write useful comments on this article.

<< Back

Latest news of science and technology, new electronics:

The existence of an entropy rule for quantum entanglement has been proven 09.05.2024

Quantum mechanics continues to amaze us with its mysterious phenomena and unexpected discoveries. Recently, Bartosz Regula from the RIKEN Center for Quantum Computing and Ludovico Lamy from the University of Amsterdam presented a new discovery that concerns quantum entanglement and its relation to entropy. Quantum entanglement plays an important role in modern quantum information science and technology. However, the complexity of its structure makes understanding and managing it challenging. Regulus and Lamy's discovery shows that quantum entanglement follows an entropy rule similar to that for classical systems. This discovery opens new perspectives in the field of quantum information science and technology, deepening our understanding of quantum entanglement and its connection to thermodynamics. The results of the study indicate the possibility of reversibility of entanglement transformations, which could greatly simplify their use in various quantum technologies. Opening a new rule ... >>

Mini air conditioner Sony Reon Pocket 5 09.05.2024

Summer is a time for relaxation and travel, but often the heat can turn this time into an unbearable torment. Meet a new product from Sony - the Reon Pocket 5 mini-air conditioner, which promises to make summer more comfortable for its users. Sony has introduced a unique device - the Reon Pocket 5 mini-conditioner, which provides body cooling on hot days. With it, users can enjoy coolness anytime, anywhere by simply wearing it around their neck. This mini air conditioner is equipped with automatic adjustment of operating modes, as well as temperature and humidity sensors. Thanks to innovative technologies, Reon Pocket 5 adjusts its operation depending on the user's activity and environmental conditions. Users can easily adjust the temperature using a dedicated mobile app connected via Bluetooth. Additionally, specially designed T-shirts and shorts are available for convenience, to which a mini air conditioner can be attached. The device can oh ... >>

Energy from space for Starship 08.05.2024

Producing solar energy in space is becoming more feasible with the advent of new technologies and the development of space programs. The head of the startup Virtus Solis shared his vision of using SpaceX's Starship to create orbital power plants capable of powering the Earth. Startup Virtus Solis has unveiled an ambitious project to create orbital power plants using SpaceX's Starship. This idea could significantly change the field of solar energy production, making it more accessible and cheaper. The core of the startup's plan is to reduce the cost of launching satellites into space using Starship. This technological breakthrough is expected to make solar energy production in space more competitive with traditional energy sources. Virtual Solis plans to build large photovoltaic panels in orbit, using Starship to deliver the necessary equipment. However, one of the key challenges ... >>

Random news from the Archive

AT90SC12872RCFT microcontroller for personal identification devices 17.04.2004

ATMEL Corporation has introduced a new microcontroller AT90SC12872RCFT specifically for the personal identification market.

The microcontroller has 72 Kbytes of non-volatile memory for storing identification data, as well as 128 Kbytes of permanent memory for storing programs. The microcontroller has two contact interfaces and one contactless. There are security features that prevent data from being overwritten.

Other interesting news:

▪ inflatable oven

▪ Nanoparticles control immunity

▪ The capacity of optical discs will dramatically increase

▪ Dual Layer Blu-rays Now Available

▪ RackStation RS3617xs NAS

News feed of science and technology, new electronics

 

Interesting materials of the Free Technical Library:

▪ section of the site Consumer Electronics. Selection of articles

▪ article Homemade thicknesser. Tips for the home master

▪ article How did you find the locator in bats? Detailed answer

▪ article by Sabadilla. Legends, cultivation, methods of application

▪ article Front acoustics: podiums. Encyclopedia of radio electronics and electrical engineering

▪ article Little mouse with big relatives. physical experiment

Leave your comment on this article:

Name:


Email (optional):


A comment:





All languages ​​of this page

Home page | Library | Articles | Website map | Site Reviews

www.diagram.com.ua

www.diagram.com.ua
2000-2024