[̲] [] []

 

11. -в

11.1 ᒺ

11.2 ᒺ .

11.3 ᒺ.

11.4

 

11.

 

. , . , , , . 80- XX ᒺ- . ᒺ- . ᒺ- . , , ѳ, ᒺ- .

, ᒺ- . ³ . ᒺ- .

, , . ᒺ . ᒺ , . () (). ᒺ , ᒺ .

, ᒺ , - . . , - -. , - . . . . , .

ᒺ- ѳ. - ᒺ , 5.5 ᒺ ( , Object Pascal Delphi ᒺ- ). ᒺ- ѳ , C++.

 

11.1

 

, ᒺ. ᒺ :

 

type t = object (t)

a1: t1;

...

am: tm;

def_N1;

...

def_Nk;

end;

 

t -; a1, ... am t t1, ..., tm; def_N1, ..., def_Nk - :

 

procedure Ni(y1: r1; ...; yf: rf;

var z1: s1; ..., var zg: sg);

function Fi(y1: r1; ...; yf: rf): r;

 

- , (t) . ,

 

type Person = Object

Name,Surname: String;

BYear: Integer;

Procedure Input;

Procedure Output;

end;

 

Person Name, Surname, Byear Input Output.

 

type Student = Object(Person)

Course: 1..MaxCourse;

Session: ARRAY [1..ExNum] OF Mark;

Procedure Input;

Procedure Output;

Function Average: Real;

end;

 

Student, Person, Course, Session Input Output.

, , , -. , , - . ҳ , -, -, -.

Student Person Person . Student: Name, Surname, Byear, Course, Session. Student Input Output Person, .

, . - ( , ). ,

 

Procedure Person.Input;

 

. (Name, Course ). -, , Input Student Input Person : Person.Input.

 

ᒺ :

 

var A, B: Person;

C: Student;

 

ᒺ ᒺ ., , A.Name, C.Course, B.Input. ϳ . ᒺ, .

ᒺ . , ᒺ (, A:=B) , ᒺ - ᒺ -. , Student Person, A:=C. C A , Person. . ᒺ .

ᒺ. ObjPers Person Student . ֳ 䳿 . , 50, - 60. , 4 , ; 5.

Input Output Student Person, Name, Surname, BYear.

Coeff Ball - ca 䳿.

 

Coeff: ca = (0.00,1.00,1.20);

 

, .

Stipend . ᒺ Student, .

 

Program ObjPers;

 

TYPE

Person = Object

Name,Surname: String; {, }

BYear: Integer; { }

Procedure Input; { }

Procedure Output; { }

end;

 

Procedure Person.Input;

begin

Write(': '); Readln(Surname);

Write(': '); Readln(Name);

Write('г : '); Readln(BYear);

end;

 

Procedure Person.Output;

begin

Write(Surname:15);

Write(Name:10);

Write(BYear:5);

end;

 

 

Const

MaxCourse = 5; { }

ExNum = 4; { }

 

Type

Mark = 2..5; {}

Student = Object(Person)

Course: 1..MaxCourse; {}

Session: ARRAY [1..ExNum] OF Mark;{ }

Procedure Input; { }

Procedure Output; { }

Function Average: Real; { }

end;

 

Procedure Student.Input;

var i: Integer;

begin

Person.Input;

Write(': '); Readln(Course);

Write(' : ');

for i:=1 to ExNum do Read(Session[i]);

Readln;

end;

 

Procedure Student.Output;

var i: Integer;

begin

Person.Output;

Write(Course:2);

for i:=1 to ExNum do Write(Session[i]:3);

end;

 

Function Student.Average: Real;

var i: Integer; s: Real; b: Boolean;

begin

s:=0; b:=true;

for i:=1 to ExNum do begin

s:=s+Session[i];

b:=b and (Session[i] <> 2)

end;

if b then Average:=s/ExNum else Average:=0

end;

 

const MaxStud = 4; { }

MaxCoeff = 3; { 䳿}

 

type ca = array [1..MaxCoeff] of Real;

 

const

MinFee = 50; { }

Coeff: ca = (0.00,1.00,1.20); { 䳿}

Ball : ca = (0.00,4.00,5.00); { }

 

Function Stipend(av: Real): Real;

var i: Integer; s: Real;

begin

i:=MaxCoeff+1;

repeat

i:=i-1

until (av >= Ball[i]);

Stipend:=MinFee*Coeff[i]

end;

 

var

Stud: ARRAY [1..MaxStud] OF Student; { }

i: Integer; av,stip: Real;

 

begin

writeln(' ');

for i:=1 to MaxStud do begin

Stud[i].Input;

end;

writeln(' .. ');

for i:=1 to MaxStud do begin

av:=Stud[i].Average; stip:=Stipend(av);

Stud[i].Output; write(av:6:2); write(stip:6:0);

Writeln;

end;

end.

 

C++. C++ ᒺ :

 

class t: public t

{

t1 a1;

...

tm am;

def_N1;

...

def_Nk;

};

 

t -; a1, ... am t t1, ..., tm; def_N1, ..., def_Nk - :

 

r Fi(r1 y1, ..., rf yf);

 

- , : public t . ,

 

class person {

public:

char name[30];

char surname[30];

int byear;

void input();

void output();

};

 

person name, surname, byear input output.

 

class student: public person {

public:

unsigned course;

unsigned session[EX_NUM];

void input();

void output();

double average();

};

 

student, person, course, session input output.

person student public: : . public:, .

student person person course, session. student input output person.

, . - . :,

 

void person::output()

 

ѳ . (name, course ). -, , input student input person : person::input();.

 

ᒺ :

 

person a, b;

student c;

 

ᒺ ᒺ ., , a.name, c.course, b.input().

ᒺ , .

C++ 䳿. C++ .

 

/* ObjPers */

 

#include <stdio.h>

 

class person {

public:

char name[30]; /* */

char surname[30]; /* */

int byear; /* */

void input(); /* */

void output(); /* */

};

 

 

void person::input()

{

printf(": "); scanf("%s", surname);

printf(": "); scanf("%s", name);

printf("г : "); scanf("%d", &byear);

}

 

void person::output()

{

printf("%s %s %d", surname, name, byear);

}

 

#define MAX_COURSE 5 /* */

#define EX_NUM 4 /* */

#define TRUE 1

 

 

class student: public person {

public:

unsigned course; /* */

unsigned session[EX_NUM]; /* */

void input(); /* */

void output(); /* */

double average(); /* */

};

 

void student::input()

{

int i;

person::input();

printf(": "); scanf("%u", &course);

printf(": ");

for (i=0; i<EX_NUM; i++)

scanf("%u", &session[i]);

}

 

void student::output()

{

int i;

person::output();

printf(" %u", course);

for (i=0; i<EX_NUM; i++)

printf(" %u", session[i]);

}

 

double student::average()

{

int i, b;

double s;

 

s=0; b=TRUE;

for (i=0; i<EX_NUM; i++)

{

s+=session[i];

b=b && (session[i]!=2);

}

if (b) return s/EX_NUM;

else return 0;

}

 

#define MAX_STUD 4 /* */

#define MAX_COEFF 3 /* 䳿 */

 

#define MIN_FEE 50 /* */

double coeff[] = {0.00,1.00,1.20}; /* 䳿 */

double ball[] = {0.00,4.00,5.00}; /* */

 

double stipend(double av)

{

int i;

 

i=MAX_COEFF;

do

i--;

while (av < ball[i]);

return MIN_FEE*coeff[i];

}

 

main()

{

student stud[MAX_STUD]; /* */

int i;

double av,stip;

 

printf(" \n");

for(i=0; i<MAX_STUD; i++)

stud[i].input();

printf("\n");

for(i=0; i<MAX_STUD; i++)

{

av=stud[i].average(); stip=stipend(av);

stud[i].output(); printf(" %lf %lf\n", av, stip);

}

}

 

11.2 ᒺ .

 

, ᒺ.

. A B S ( B S A). Q A. Q, , B.

 

Type

A = Object

...

Procedure S(...);

Procedure Q(...);

...

end;

B = Object(A)

...

Procedure S(...);

...

end;

...

{}

 

Procedure A.Q(...);

...

S(...);

...

end;

...

Var X: A; Y: B;

...

{}

X.Q(...);

...

Y.Q(...);

 

: S X.Q(...) Y.Q(...)?. ³ , ᒺ , ᒺ . ᒺ : . ᒺ , . . , , X.Q(...) Y.Q(...) S A, Q A. , X.Q(...) S A, Y.Q(...) S B.

, , . . , . . ³ , .

 

( , ) ᒺ . , . , 䳿 ᒺ . , , File Create Open .

 

virtual, . :

 

procedure S (var x, y: real); virtual;

 

, , , procedure constructor. :

 

constructor Create(k: integer);

 

.

 

: . : GrPoint Circle - . .

 

ObjVirtual Graph:

         DETECT , , ;

         InitGraph(gd,gm,'c:\tp7\bgi') , gm gd; 'c:\tp7\bgi' - ( BGI);

         GetColor , ; , , .

         GetBkColor , ;

         SetColor(c) , c;

         PutPixel(x,y,c) , x, y c;

         Circle(x,y,r) , x, y, r ;

         CloseGraph , .

 

GrPoint X,Y Visible, , . Create A,B. GetX GetY . OnScreen , . SwitchOn SwitchOff . , Move Dx, Dy x y . Move , . , (SwitchOff). - (X:=X+Dx; Y:=Y+Dy;). , , (SwitchOn).

 

Circle GrPoint GetX, GetY, OnScreen, Move. X,Y Circle , Radius . Visible, , , . Create (A,B) R. Create GrPoint, . GetR . ³ SwitchOn, SwitchOff . SwitchOff (c:=GetColor), (SetColor(GetBkColor)), (Graph.Circle(X,Y,Radius)) (SetColor(c)).

 

ᒺ- ᒺ. ij, X, Y . , , , . ObjVirtual GetX, GetY, OnScreen, GetR. , ( ) private. , private public , . X, Y, Visible, R GrPoint Circle. private , ᒺ .

 

(100,100), , 10 20 . (150,150) 30, 10 20 . Readln , <Enter>.

SwitchOn SwitchOff , p.Move(10,20) Move SwitchOn SwitchOff GrPoint, c.Move(10,20) Move SwitchOn SwitchOff Circle.

 

 

Program ObjVirtual;

 

Uses Graph;

 

Type

GrPoint = Object { }

private

X,Y: Integer; { }

Visible: Boolean; { }

public

Constructor Create(A,B: Integer); { }

Function GetX: Integer; { x }

Function GetY: Integer; { y }

Function OnScreen: Boolean; { ? }

Procedure SwitchOn; Virtual; { }

Procedure SwitchOff; Virtual; { }

Procedure Move(Dx,Dy: Integer); { }

end;

Circle = Object(GrPoint) { }

private

Radius: Integer; { }

public

Constructor Create(A,B,R: Integer); { }

Function GetR: Integer; { }

Procedure SwitchOn; Virtual; { }

Procedure SwitchOff; Virtual; { }

end;

 

Constructor GrPoint.Create;

begin

X:=A; Y:=B; Visible:=False

end;

 

Function GrPoint.GetX;

begin

GetX:=X

end;

 

Function GrPoint.GetY;

begin

GetY:=Y

end;

 

Function GrPoint.OnScreen;

begin

OnScreen:=Visible

end;

 

Procedure GrPoint.SwitchOn;

begin

Visible:=True; PutPixel(X,Y,GetColor)

end;

 

Procedure GrPoint.SwitchOff;

begin

Visible:=False; PutPixel(X,Y,GetBkColor)

end;

 

Procedure GrPoint.Move;

var vis: Boolean;

begin

vis:=Visible;

if vis then SwitchOff;

X:=X+Dx; Y:=Y+Dy;

if vis then SwitchOn

end;

 

Constructor Circle.Create;

begin

GrPoint.Create(A,B); Radius:=R

end;

 

Function Circle.GetR;

begin

GetR:=Radius

end;

 

Procedure Circle.SwitchOn;

begin

Visible:=True; Graph.Circle(X,Y,Radius)

end;

 

Procedure Circle.SwitchOff;

var c: Integer;

begin

Visible:=False;

c:=GetColor; SetColor(GetBkColor);

Graph.Circle(X,Y,Radius); SetColor(c)

end;

 

var p: GrPoint; c: Circle; gm,gd: Integer;

begin

gd:=DETECT; InitGraph(gd,gm,'c:\tp7\bgi');

p.Create(100,100);

p.SwitchOn; Readln;

p.Move(10,20); Readln;

c.Create(150,150,30);

c.SwitchOn; Readln;

c.Move(10,20); Readln;

CloseGraph;

end.

 

C++ virtual, . :

 

virtual void s (double x, double y);

 

C++ , . , GrPoint,

 

GrPoint(int a, int b);

 

, . C++ . , . . ObjVirtual GrPoint Circle:

 

Circle::Circle(int a, int b, int r) :GrPoint(a, b)

{

...

}

 

GrPoint Circle. :.

ᒺ , , , ,

 

GrPoint p = GrPoint(100,100);

 

 

GrPoint p(100,100);

 

ObjVirtual C++ . (<graphics.h>), .

C++, . private ( ) C++ - (friend) . , , protected.

getx, gety, on_screen, getr, . inline-, , , , . Inline- . ϳ , .

// C++ , .

 

//ObjVirtual;

#include <stdio.h>

#include <graphics.h>

 

#define TRUE 1

#define FALSE 0

 

class GrPoint { //

protected:

int x,y; //

int visible; //

public:

GrPoint(int a, int b); //

int getx() { return x; }; // x

int gety() { return y; }; // y

int on_screen() { return visible; };// ?

virtual void switch_on(); //

virtual void switch_off(); //

void move(int dx, int dy); //

};

 

class Circle: public GrPoint{ //

protected:

int radius; //

public:

Circle (int a, int b, int r); //

int getr() { return radius; }; //

virtual void switch_on(); //

virtual void switch_off(); //

};

 

GrPoint::GrPoint(int a, int b)

{

x=a; y=b; visible=FALSE;

};

 

void GrPoint::switch_on()

{

visible=TRUE; putpixel(x,y,getcolor());

}

 

void GrPoint::switch_off()

{

visible=FALSE; putpixel(x,y,getbkcolor());

}

 

void GrPoint::move(int dx, int dy)

{

int vis;

 

vis=visible;

if (vis) switch_off();

x+=dx; y+=dy;

if (vis) switch_on();

}

 

Circle::Circle(int a, int b, int r) :GrPoint(a, b)

{

radius=r;

}

 

void Circle::switch_on()

{

visible=TRUE; circle(x, y, radius);

}

 

void Circle::switch_off()

{

int c;

 

visible=FALSE;

c=getcolor(); setcolor(getbkcolor()) ;

circle(x, y, radius); setcolor(c);

}

 

main()

{

GrPoint p(100,100);

Circle c(150,150,30);

int gm, gd = DETECT;

 

initgraph(&gd, &gm,"C:\\BC31\\BGI");

p.switch_on(); getchar();

p.move(10,20); getchar();

c.switch_on(); getchar();

c.move(10,20); getchar();

closegraph();

}

 

[̲] [] []