. , , ,

,,,

,

Š  

1.

-------------------------------------------------------------

70- - :

- ;

- ;

- ( goto).

- .

- - , , . .

- . , , .   struct), - (record). . - , - ,

, , , , ( "" ).

: ----------

-- 頠 頠

: 򠠠 򠠠

, ( , ) .

- , . , ( ).

- . "" .

//------ dat - "" --------typedef struct dat

{

unsigned day;

unsigned month;

unsigned year;

}

DAT;

//----- "" --------------static int mm[] = {31,28,31,30,31,30,31,31,30,31,30,31};

//----- -----------------------------int TestData(p)

DAT *p;

{

if (p->month ==2 && p->day==29 && p->year %4 ==0) return(1);

if (p->month ==0 || p->month >12 ||

p->day ==0 || p->day >mm[p->month])

return(0);

return(1);

}

//------ ----------------------------------------void NextData(p)

DAT *p;

{

p->day++;

if (p->day <= mm[p->month]) return;

if (p->month ==2 && p->day==29 && p->year %4 ==0) return;

p->day=1;

p->month++;

if (p->month !=13) return;

p->month=1;

p->year++;

}

//------- n --------------------------void PlusData(p,n)

DAT *p;

int n;

{

while (n-- !=0) NextData(p);

}

//------- ---------------------------------

void main()

{

DAT a;

do

{

scanf("%d%d%d", &a.day, &a.month, &a.year);

}

while(TestData(&a) ==0);

PlusData(&a, 17);

}

//--------------------------------------------------------

() . , , .

++ . :

蠠 ++

------------------------- ----------- : :

,

 ,

, , . ,

, .

--------------------------------------------------------- : :

, ,

,

. .

--------------------------------------------------------- : :

- ,

, - ( '+' ,

- ,

int double). ,

.

2. ++

-----------------------------------------------

, .

,

-

.

2.1.

------------------------- .

. ( ) .

p, *p . , , .

struct.

struct dat

{ int day,month,year; }

dat NextDat(dat x) // -

{ ... return(x); } //

dat Nextdat1(dat *p)

{ ... return(*p); } //

dat a,b,c,*q; // struct

void main()

{

q = &b;

a = b; //

a = *q; //

c = NextDat(b); //

c = NextDat1(&b); // ,

} // NextDat -

2.2. ( )

----------------------------------------

-

- . . , ++ - : , . . . - .

++ ""

------------------------ -----------------------------//--------------- -----------------int &a = 5; int a, *pa =a;

*pa = 5;

//--------------- -----------------int x; int x,*pa;

int &a = x; pa = &x;

a = 5; *pa = 5;

//-------------- ----------------struct dat struct dat

{ int day,month,year }; { int day,month, year };

dat x; dat x;

dat& b = x; dat* pb = &x;

dat& c = {12,12,1990}; dat cc = {12,12,1990};

dat *pc = &cc;

b.year = 1990; pb->year= 1990;

c.day=b.day+3; pc->day = pb->day+3;

c = b; // pc->day = pb->day;

// pc->month = pb->month;

pc->year = pb->year;

. , - , - , . - , , ,

- - . .

, , :

- ;

- ;

- .

1. -

---------------------------------------------------------dat Inc(dat x) ========> -

{ --------> -

x.day++;

return(x); ---- +---+x.day++

} b =========> x =========

L---- +---+ return(x)

void main()

{ ---- +---+ ---

dat a,b,*p; a <========= x <=======

a = Inc(Inc(b)); L---- +---+ L---p = &Inc(b); x.day++

a = *p;

}

2. -

---------------------------------------------------------dat* Inc(dat* x) x->day++

{ x->day++

x->day++; ---- +---+

return(x); ===== b <--------- x

} --> <---- +--+

L---- return(x)

======+=====void main() a=*.. +---+

{ --- L---- x

dat a,b,*p; =========>

a = *Inc(Inc(&b)); L---- +--+

p = Inc(&b); ---- return(x)

a = *p; L-- <===========} L--- ----

L====> a

L---

3. -

---------------------------------------------------------dat& Inc(dat& x) x.day++ dat* px

{ x.day++

x.day++; ---- +---+

return(x); ===== b <--------- px

} --> <---- +--+

L---- return(px)

======+=====void main() a=*.. +---+

{ --- L---- px

dat a,b,*p; =========>

a = Inc(Inc(b)); L---- +--+

p = &Inc(b); ---- return(px)

a = *p; L-- <===========} L--- ----

L====> a

L---

:

- - , ;

- , ;

- 2 3 , ;

- 1 3 , ;

- , ( ), ++ .

, , , . "Borland C" :

- - "" , ;

- return(x), x - , x , ;

- -, - ;

- , , -. , , .

++

----------------- ---------- --

dat Inc(dat x) void Inc(dat *r,dat x)

{ {

x.day++; x.day++;

return(x); *r = x; //

} } //

void main() void main()

{ {

dat a,b*p; dat a,b,*p;

dat t,u; // ye ye

a = Inc(b); Inc(&a,b); //

p = &Inc(b); Inc(&t,b); //

p = &t; //

a = *p; a = *p; //

a = Inc(Inc(b)); Inc(&u,b); //

Inc(&a,u); //

} }

2.3. -

-------------------------------- :

//------------ dat - "" --struct dat

{

unsigned day;

unsigned month;

unsigned year;

int TestData();

void NextData();

void PlusData(int n)

{

while(n-- !=0) dat::NextData(this);

}

};

//----------- "" --------static int mm[] = {31,28,31,30,31,30,31,31,30,31,30,31};

//----------- -----------------------int dat::TestData()

{

if (month ==2 && day==29 && year %4 ==0) return(1);

if (month ==0 || month >12 || day ==0 || day >mm[month])

return(0);

return(1);

}

//----------- ----------------------------------void dat::NextData()

{

day++;

if (day <= mm[month]) return;

if (month ==2 && day==29 && year %4 ==0) return;

day=1;

month++;

if (month !=13) return;

month=1;

year++;

}

//--------- --------------------------------void main()

{

dat a;

do

{

scanf("%d%d%d", &a.day, &a.month, &a.year);

}

while(a.TestData() ==0);

a.PlusData(17);

}

//------------------------------------------------------- ,

. - :

- ( PlusData). ;

- ( ). ,

< >::< >

-

this - ,

( struct dat *this ).

this->month = 5;

this->day++;

month = 5;

day++;

- , ,

< >.< > ( < > )

2.4.

--------------------------- ++

, . .

overload:

overload SetDat;

void SetDat(int dd,int mm,int yy,dat *p)

{ //

p->day=dd;

p->month=mm;

p->year=yy;

}

void SetDat(char *s,dat *p) //

{

sscanf(s,"%d%d%d", &p->day, &p->month, &p->year);

}

void main()

{

dat a,b;

SetDat(12, 12, 1990, &a); //

SetDat("12,12,1990", &b); //

}

- , .

struct dat

{

int day,month,year;

void SetDat(int,int,int);

void Setdat(char *);

}

void dat::SetDat(int dd,int mm,int yy)

{

day=dd; month=mm; year=yy;

}

void dat::SetDat(char *s)

{

sscanf(s,"%d%d%d",&day,&month,&year);

}

void main()

{

dat a,b;

a.SetDat(12,12,1990);

b.SetDat("12,12,1990");

}

2.5.

---------------------------------------------

- malloc() free(), ( ). ( ), . sizeof , . ++ , malloc free new delete. , :

++ ""

------------------------- ---------------------------------char *s,x[80]; char *s,x[80];

dat *p,*q; struct dat *p,*q;

void main() void main()

{ {

p = new dat; p = malloc(sizeof (struct dat));

q = new dat[15]; q = malloc(15*sizeof (struct dat));

gets(x); gets(x);

s = new char[strlen(x)+1]; s = malloc(strlen(x)+1);

... ...

delete p; free(p);

delete q; free(q);

delete s; free(s);

}

:

<: ࠠ <

> new >

delete <

>

2.6.

----------------------------------

,

:

//----- ,

//-----

#include

void dat::SetDat(int d=0, int m=0, int y=0)

{

struct date x;

getdate(&x); //

//

//

year = (y == 0) ? x.da_year : y;

month= (m == 0) ? x.da_month: m;

day = (d == 0) ? x.da_day : d;

}

2.7

---------------------------------------

"" , (int,unsigned) , . ++ "" , . , , "alloc.h"

extern void* malloc(int n);

dat *p;

p = (dat *) malloc (10*sizeof(dat));

L--- void* dat*

, , . " " .

2.8 (inline)

-------------------------------

( - ) inline-, . - inline , ( ),:

struct dat

{

int d,m,y;

void Setdat(char *p) // inline

{

... //

}

2.9

-------------------------------

, "" , () .

:

struct dat

{

int day,month,year;

void Getdat();

void Putdat();

void Nextdat();

}

int dat::*p; // int

// dat

p = & dat::month; // p - ()

// month

// dat

dat x,*px = &x; //

x.*p = 5; //

px->*p = 5; // . *< >

// <*dat>-> *< >

x.month = 5;

px->month =5;

-, ,

:

void (dat::*fun)(); // -

// dat

fun = & dat::Putdat(); // fun -

// - Putdat dat

(x.*fun)(); // -

(px->*fun)(); // fun x

// px

x.Putdat();

px->Putdat();

2.10 ()

---------------------------------------

++ . const, , , . , :

const int n=5;

n++; //

int xxx(const int m)

{

m++; //

}

const , () :

- conts , . :

const char * p;

p = "1234567890"; //

p + =3; //

*(p+3) = '3'; //

(*p)++; //

- const , . :

char const* p = "1234567890";

char c;

(*p) = '3'; //

p++; //

c = *(p+3); //

const char const* p = "1234567890";

2.11 ++

----------------------------------------

++ "" :

- (struct)

(char,int);

- -. -

""

;

- ( , , ). ( ).

.

3. . .

----------------------------------------------------

3.1. ++

----------------------------------

++ , -. "" () , , "" () ,

. ,

():

------------------------- -----------------------------------struct dat class dat

{ { //

int day,month,year; int day,month,year;

public: //

void SetDat(int,int,int); void SetDat(int,int,int);

void SetDat(char *); void SetDat(char *);

} }

void main() void main()

{ {

// - a,b // - a,b dat

dat a,b; dat a,b;

a.day = 5; //

a.month = 12; //

bAA.SetDat("12,12,1990"); b.Setdat("12,12,1990");

} }

"" . private.

T .

, , ( ).

, ( ). , ( ) :

- (), ;

- , ;

- , malloc() free() new delete.

, :

class dat

{ ....... }

dat a,b; //

dat *p; //

void main()

{

dat c,d; //

p = new dat; //

...

delete p; //

} <---------------------//

3.2. .

-------------------------------------

.

, ...

.

- .

,

,

~< >.

. . ()

 ,  .

, .

:

- -

main(), - main(). , -

;

- -

(), - ;

- - new, - delete.

++ . . delete . .

//--------------------------------------------------------#include

#include

static int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

class dat

{

int day,month,year;

public:

dat(int,int,int); //

// ( )

dat(char *); //

dat(); //

~dat(); //

};

//------- - ---------dat::dat(char *s)

{

int i;

char ss[80];

strcpy(ss,s);

for (i=0; ss[i] !=0; i++)

if (ss[i]=='-') ss[i]=','; // '-' ','

sscanf(ss,"%d%d%d",&day,&month,&year);

}

// ( 0 - )

dat::dat(int d=0, int m=0, int y=0)

{

struct date x;

getdate(&x); //

//

//

year = (y == 0) ? x.da_year : y;

month= (m == 0) ? x.da_month: m;

day = (d == 0) ? x.da_day : d;

}

//------ --------------------------dat::dat()

{

struct date x;

getdate(&x); //

//

year = x.da_year ;

month= x.da_month;

day = x.da_day ;

}

//------ ------------------------------------------dat::~dat()

{

printf(" ==> %2d-%2d-%4dn",day,month,year);

}

//------------------------------------------------------dat a("12-12-1990"); // -

// main()

dat b[10]; // -

// main()

void xxx(dat &p)

{

dat c(12,12); // dat(int,int,int)

//

dat d = p; //

... // , ..

... //

} //

// c d

void main()

{

int i,n;

scanf("%d",&n);

dat *p = new dat[n]; // ⠠ //

for (i=0; i<10; i++) // n

xxx(b[i]); // b[i]

for (i=0; i< n; i++) //

xxx(p[i]) // p[i]

delete[n] p; // ⠠ // n

} // a b[10]

// main()

}

3.3 . .

----------------------------------------------------------

. , ++ . :

- 堠 ⠠ . (), ;

- 堠 ࠠ , -;

- ( ).

( "" ) -. , "" - , , . , , . , "" , , .

, "" , - , . , , "". , " ".

, , , friend . :

class A

{

int x; //

...

friend class B; // B A

// ( A)

friend void C::fun(A&);

// - fun C

// A

friend void xxx(A&,int);

// xxx A

friend void C::operator+(&);

// C

// < C>+< A>

// A

class B

{

public: int fun1(A&);// A

void fun2(A&);// ----------------------------------- }

class C

{

public: void fun(A&);// ------------------------------------ void operator+(A&);//------------------------------- ....

}

- (const).

,

.

void dat::put() const

{

}

3.4

------------------------------

,

. , , ...

- . ,

< >::< >

. . .

:

--------------------------------------------------class list

{

static list *fst; //

static list *lst; //

list *next; //

.... .....

public:

void insfst(); //

void inslst(); //

void show(); //

void extract(); //

list(); //

~list(); //

}

list list::fst=NULL; //

list list::lst=NULL;

//-------------------------------------------------------void insfst()

{

next = NULL;

if (fst==NULL)

fst=lst=this;

else

{ next=fst; fst=this; }

}

//-------------------------------------------------------void inslst()

{

next = NULL;

if (fst==NULL)

fst=lst=this;

else

{ lst->next=this; lst=this; }

}

//-------------------------------------------------------void list::extract()

{

list *p,*pred; //

for (pred=NULL,p=fst; p !=NULL; //

pred=p,p=p->next)

if (p=this) break; // -

if (p !=NULL)

{ // -

if (pred==NULL)

fst = next;

else

pred->next=next;

}

}

//-------------------------------------------------------void list::show()

{

list *p;

for (p=fst; p !=NULL; p=p->next)

{ ... ... }

}

//------ -----------list::list()

{

insfst();

}

//------ ------list::~list()

{

extract();

}

.

,

- .

. ""

. ,

.

-.

"" , . this. , , , .

class list

{ ...

static void show(); // a

} //

//-------------------------------------------------------static void list::show()

{

list *p;

for (p=fst; p !=NULL; p=p->next)

{ ... ... }

}

//-------------------------------------------------------void main()

{ ...

list::show(); //

}

4. .

------------------------------------

, , . "" () , - , . , ( ), .

, .

, , , . ,

dat dat+int int+dat.

- :

operator<>( < -> )

operator

.

(, , ) .

(, ) .

.

, :

- - ,

,

this;

- ,

, ,

. , .

.

#include

#include

#include

static int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

class dat

{

int day,month,year;

public:

void next(); // -

//

dat operator++(); // ++

dat operator+(int); // " + "

// this

friend dat operator+(dat,int) //

friend dat operator+(int, dat) //

dat(); //

dat(int,int,int); // (. )

dat(char *); //

~dat(); //

}; // (. )

//------ --------------------// this,

//

//-------------------------------------------------------void dat::next()

{

day++;

if (day > days[month])

{

if ((month==2) && (day==29) && (year%4==0)) return;

day=1;

month++;

if (month==13)

{

month=1;

year++;

}

}

}

//------ -----------------------------//1. - this

//2. ()

//3. dat++ ( )

//4. : -- ++

// (

// ).

//-------------------------------------------------------

dat dat::operator++()

{

//

dat x = *this; //

dat::next(); //

return(x); //

}

//------ " + " ------------------------------//1. - this

//2. ,

// x

//-------------------------------------------------------dat dat::operator+(int n)

{

dat x;

x = *this; // x

while (n-- !=0) x.next(); // next x

return(x); // x

}

//------ " + " ------------------------------//1. -

//2.

//3. dat - ,

//

//-------------------------------------------------------dat operator+(dat p,int n)

{

while (n-- !=0) p.next(p); // next p

return(p); // x

}

//------ " + " -----------------------------//1. -

//2. dat - ,

//

//-------------------------------------------------------dat operator+(int n, dat p)

{

while (n-- !=0) p.next(); // next p

return(p); // p

}

//-------------------------------------------------------

void main()

{

int i;

dat a;

dat b(17,12,1990);

dat c(12,7);

dat d(3);

dat e;

dat *p = new dat[10];

clrscr();

e = a++;

d=b+15;

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

p[i] = p[i] + i;

delete[10] p;

}

, . . . . , .

- dat , . . . . , "" . :

- - - , , , ;

- x . (, ), :

dat x = *this;

:

dat x;

dat x("13-JUL-1990");

- ;

- - ,

(

, );

- (

),

- (. " "), return

- .

- :

- ,

;

- (

) . ,

, , .

( ) ( ).

, ,

.

,

(), .  

" + ",

.

class dat

{

int day,month,year;

public:

void next(); // -

//

dat& operator+(int); // " + "

// this

//------ " + " ------------------------------//1. -

//2.

//3. dat -

// ,

//4. .

//-------------------------------------------------------friend dat& operator+(dat& p,int n)

{

while (n-- !=0) p.next();// next p

// .

return(p); // p

}

// " + " ------------------------------------//1. -

//2. dat -

// , .

//3. .

//-------------------------------------------------------

friend dat& operator+(int n, dat& p)

{

while (n-- !=0) p.next(); // next p

// .

return(p); // p

}

//-------------------------------------------------------

dat(); //

dat(int,int,int); // (. )

dat(char *); //

~dat(); //

}; // (. )

//------ " + " -------------------------------//1. - this

//2.

//3. -

//--------------------------------------------------------dat& dat::operator+(int n)

{

while (n-- !=0)

next(); // next this

return(*this); // (this)

}

//---------------------------------------------------------

void main()

{

int i;

dat a;

dat b(17,12,1990);

dat c(12,7);

dat d(3);

dat e;

dat *p = new dat[10];

e = a++;

d=b+15;

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

p[i] + i;

delete[10] p;

}

5.

-------------------------------------------------------

,

(

), .

,

. - ,

, -

.

5.1

----------------------------------------

dat int long. , (long) (int). dat int long. - , this. , .

static int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

class dat

{

int day,month,year;

public:

operator int(); // dat int

operator long(); // dat long

long operator -(dat &p); // dat-dat

//

dat(); //

dat(int,int,int); //

dat(char *); //

~dat(); //

};

//------ dat int -----------------------------// this

//------------------------------------------------------- dat::operator int()

{

int r; //

int i; //

for (r=0, i=1; i

r += days[month]; //

if ((month>2) && (year%4==0)) r++; //

r += day; //

return(r);

}

//------ dat long ---------------------------// this

//------------------------------------------------------

dat::opertor long()

{

long r; //

r = 365 * (year-1) //

r += year / 4; //

r += (int)(*this); // -

// (

return(r); // dat int

}

//-------- ---------------// this

// p

//-------------------------------------------------------long dat::operator-(dat& p)

{

return((long)(*this) - (long)p); //

// long .

}

void main()

{

dat a("12-05-1990"); // ,

dat b; //

int c;

long d;

// long

printf(" 12-05-1990 %4ld n",(long)b-(long)a);

// int

printf(" %3d n",(int)b);

//

c = b;

d = b - a; // dat-dat

printf(" 12-05-1990 %4ld n",d);

printf(" %3d n",c);

}

5.2

---------------------------------------------

.

, "xxx" "operator xxx()". , "zzz" "operator zzz". , "zzz". - "zzz::zzz", . , "xxx" "zzz",

"zzz"

class zzz

{

int par_zzz;

----------------- ()

zzz(xxx p);

zzz(xxx& p);

L-------------------- ()

};

void zzz::zzz(xxx &p)

{

par_zzz = ... p.par_xxx ...;

----- L-------

}

class xxx

{

friend class zzz;

int par_xxx;

};

:

- "zzz", this;

- (, par_zzz) this

this->par_zzz = ...

(*this).par_zzz = ...

par_zzz = ...

- , , , ( ), .

;

- "zzz" "xxx" "zzz"

"xxx".

long dat - .

- man, .

static int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

class dat

{

int day,month,year;

public:

dat(long); // long dat

dat(man&); // man dat

dat(); //

dat(int,int,int); //

dat(char *); //

~dat(); //

};

class man

{

friend class dat; // dat

... // man

int d,m,y; // ""

... // man

public:

man(dat&); // dat man

man(); //

~man(); //

};

//------ man dat ----------------------------// this ,

// - -

//-------------------------------------------------------void dat::dat(man& p)

{

day = p.d;

month = p.m;

year = p.y;

}

//------ long dat ---------------------------// this

// long

//-------------------------------------------------------

void dat::dat(long p)

{

year = p / 365.25; //

p = p - (year-1)*365L - year/4; //

year++; // - 0001

for (month=1; p > 0; month++) //

{

p -= days[month];

if (month == 2 && year % 4 == 0) p--;

}

month--; //

p += days[month]; //

if (month == 2 && year % 4 == 0) p++;

day = p + 1;

}

void main()

{

dat a("12-05-1990"); // ,

dat b; //

int c;

long d;

// long

printf(" 12-05-1990 %4ld n", (long)b-(long)a);

// int

printf(" %3d n", (int)b);

c = b; //

d = b - a; // dat-dat

printf(" 12-05-1990 %4ld n",d);

printf(" %3d n",c);

}

5.3 new delete

----------------------------------------

:

void *operator new(size_t size);

void operator delete (void *);

堠 void * - , ,

size - .

.

5.4 [], (), ->

--------------------------------------

() :

-------------------class one

{

public:

typeout operator()(type1,type2);

};

:

type1 a; //

type2 b; //

one obj; //

... obj(a,b) ...  obj.operator()(a,b)

-> :

------------------class two

{

public: type Y;

};

class one

{

two operator->(); //

蠠 two* operator->(); // two,

}; // Y

:

one obj;

... obj->Y .. (obj.operator->()) ->Y

[] :

-------------------- .

class text_page

{

char **page; //

public:

int operator[](char*); //

//

char* operator[](int); //

};

5.5

------------------------------------------------

Ka ,

< > < 2> = < 1>

, . K - , .

E , return - .

Ta , . , - , this . , .

:

class string

{

char *s; //

int sz; //

public: string(string&);//

}; //

// -

string::string(string& right)

{

s = new char[right->sz];

strcpy(s,right->s);

}

6.

---------------------------

6.1

-------------------

. , . ,

.

, "" .

(), . -

-

. man -

, .

class man

{

char name[20]; //

char *address;

dat dat1; //

dat dat2; //

public:

void newadr(); // -

man(char*); //

}

//----- " " ----------------void man::newadr()

{

int n;

char s[80]; //

if (address != NULL)

delete address; //

printf(" :");

gets(s);

address = new char[strlen(s)+1];//

strcpy(address,s); //

}

, - , - . , - , "" .

, , .

, , . , .

. , . , :

class man

{

char name[20]; //

dat dat1; //

dat dat2; //

public:

man(char *,char *,char *); //

man(char *);

}

//----- man

// dat1 dat2

//----------------------------------------------------- man::man(char *p)

{

}

//----- man

// dat1 dat2

//--------------------------------------------------------- man::man(char *p,char *p1, char *p2) : dat1(p1), dat2(p2)

{

// --- ---

}

------------------

dat1

-------------

- ࠠ

man

dat2

void main ------ man

{

man JOHN("John","8-9-1958","15-1-1987");

} L------

ࠠ dat2 man

dat1 man

6.2

---------------------

.

"" ,

"" , "" :

, - , .

(),

- ().

:

class <> : < 1>,< 2>,...< n>

{

}

堠 ࠠ  蠠 :

- . , ;

- ( , ). O ;

- - "" , , , ;

- , . .

class a

{

public:

void f() {}

void g() {}

}

class b : a ------------------------

{

public: void f() -------------------- "f"

{ ...

a::f(); -------------- "f"

} -------------- "g"

void h() {} --------------

}

void main()

{

a A1;

b B1;

B1.f(); --------------- b::f()

B1.g(); --------------- a::f()

}

"" , , this

, .

:

- , , .

- ,

;

-

.

, .

,

.

6.3

--------------------------------------

, . , , , .

, - ,

- .

:

- A B,

B.

, B

A A , - A. B A;

- ,

class B : A { }

A B. , - A - B, , B. B A ;

- ,

class B : public A { }

A B, B , ;

- , B - ( ) A, B, ( ):

class B : A {

...

public:

...

public A::fun;

...

}

:

class A class B

----------- ----------------

privat ======================> privat A

+----------+ ( B)

public class B:A +---------------+

======================> privat B

( B)

class B : public A ===============

======================> public B

class B : A { ...

public A::newadr; <----

---------------------->

L----------- L---------------

, , .

. (protected) .

protected . ,

.

public, , . :

class A

{

int a1; //

protected: int a2; //

public:

}

class B : A // a1,a2 B

{

void x();

}

void B::x()

{

a1 = 5; // : a1 B

a2 = 3; // a2 B

}

class B : public A // a2

{ // B,

// protected: int a2;

}

class A class B

----------- ----------------

privat ======================> privat A

+----------+ ( B)

protected class B:A +---------------+

======================> privat B

=============== ( B)

+----------+ class B: public A +---------------+

public L======> protected B ==========>

===============

public

6.4

----------------------------------------------------

, () . , .

, , , . , . () ( ) . , , ++ (

)

, "" .

class A

{

public: void f1();

};

class B : A

{

public: void f1(); // e B

void f2(); //

};

A *pa;

B *pb;

B x;

pa = &x; //

// B

// A

pa->f1(); //

// A::f1(),

//

. :

pb = (B*) pa; // -

pb ->f2(); // , "pa"

// "B"

6.5 -

------------------------------------------------------

,

" ". " " , ,

.

- ,

,

.

,

. - " " ,

,

- "run".

7. .

-----------------------------

7.1

------------------------------

, . . , . , .

p[] A1

+---+ -b---------

--------------------->-a-------======== b::f()

+---+ L---------===

------------ L-----------

+---+ C1

---------- -c---------

+---+ L-------->-a-------======== c::f()

L---------===

L-----------

A1

L---------->-a------- ======= a::f()

L--------

class a

{ ... void f(); };

class b : public a

{ ... void f(); };

class c : public a

{ ... void f(); };

a A1;

b B1;

c C1;

a *p[3]; //

p[0] = &B1; //

p[1] = &C1; //

p[2] = &A1;

" "

" " ,

"" . ,

p[0]->f(); // a::f()

p[1]->f(); // , f()

p[2]->f(); //

, , . :

- "" ;

-

;

-

;

- ,

.

class a

{

public: int id; //

void f();

void newf(); // f()

}

a::a() //

{ ...

id = 0;

}

b::b()

{ ...

id = 1;

}

c::c()

{ ...

id = 2

}

void a::newf()

{

switch (id)

{

case 0: a::f(); break;

case 1: b::f(); break;

case 2: c::f(); break;

}

}

p[0]->newf(); // b::f() B1

p[1]->newf(); // c::f() C1

p[2]->newf(); // a::f() 1

. () - , . , , .

, " " " " , .

().

. - . . . ( , ).

class A

{

------> void (**ftable)(); //

//

public:

virtual void x();

virtual void y();

virtual void z();

A();

~A();

};

//

------> void (*TableA[])() = { A::x, A::y, A::z };

A::A()

{

------> ftable = TableA; //

}

class B : public A

{

public:

void x();

void z();

B();

~B();

};

// A

// B

--> void (*TableB[])() = { B::x, A::y, B::z };

L B

B::B() L------ A

{

--> ftable = TableB; // B

}

void main()

{

A* p; // p A

B nnn; // p = &nnn; // B

p->z(); ------------------> (*(p->ftable[2]))();

}

p nnn TableB B::z()

----- -------->--B----- ----->--------- --->----------

------ ftable--A--- 0+--------+

L----- ------ 1+--------+

+-----+ 2 --------- L--------- L--------

7.2

---------------------

,

"",

. . :

class base

{

public:

virtual print() =0;

virtual get() =0;

}

, .

7.3

---------------------------------------------------

. .

, .. ,

:

class d : public a,public b, public c { };

d D1;

pd = &D1; // #define db sizeof(a)

pa = pd; // #define dc sizeof(a)+sizeof(b)

pb = pd; // pb = (char*)pd + db

pc = pd; // pc = (char*)pd + dc

D1

pd -------------------->-d---------

pa --------------------->-a-------T T

db = sizeof(a)

L--------- +

pb --------------------->-b------- dc = sizeof(a) + sizeof(b)

L---------

pc --------------------->-c-------+

L---------

L----------

this (db,dc), - .

,

.. , "b" "f()" "d" D1.f() :

this = &D1; //

this = (char*)this + db // b

b::f(this); // b

//

. -,

(

- "a" "d", "b" "d" "c" "d").

-, ,

,

.. this . , this "", .

class a

{

public: virtual void f();

virtual void g();

};

class b

{

public: virtual void h();

virtual void t();

};

class c : public a, public b

{ // f(),t()

public: void g(); // g()

void h(); // h()

}

a A1;

b B1;

c C1;

pa = &A1;

pb = &B1;

pa->f(); // a::f()

pb->h(); // b::h()

pa = &C1;

pb = &C1;

pa->f(); // a::f()

pa->g(); // c::g()

pb->h(); // c::h()

pb->t(); // b::t()

:

A1

-a---- "a"

------------>--------

+-----+ a::f()

L------ +-------+

a::g()

L------- B1

-b---- "b"

------------>--------

+-----+ b::h()

L------ +-------+

b::t()

L------- C1

T --c----- "a" "c"

--a--- --------

db ----------->a::f()

L------ +-------+

+ --b--- c::g()

------- L------- L------ "b" "c"

L--->-------- "" c::h()

L-------- xxx()----->--xxx()----------------

+-------+ this=(char*)this - db

b::t() goto c::h

L-------- L----------------------

.

7.4.

------------------------------

,

,

class base {}

class a : public base {}

class b : public base {}

class c : a, b {}

"c" base.

class a : virtual public base {}

class b : virtual public base {}

class c : public a, public b {}

a A1;

b B1;

c C1;

, ,

. , .

, , .

A1 B1 C1

--a------ --b----- --c---------------

------ ------ --a-------

+--------+ +-------+ -------

+---------+

-base--<---- -base-<----

L------- L------ L----------

L--------- L-------- --b-------

------

+---------+

L----------

-base---<---

L--------<----

L-----------------

.

8.

-------------------------------------------------

() ,

. , ,

, , .

. ,

,

.

, .

8.1

--------------------------------------------------

, , , :

- class string - ;

- class integer - ;

- class real - ;

- class dat - ;

- class time - ..

.

,

:

, , ,

(int, long, char*),

...

. , .

.

class string : base

{

char *s; //

int sz; //

public: int GET(); //

void PUT(); //

int CMP(base*); //

char *NAME(); //

base *COPY(); //

operator long(); // long //

operator char*(); // char* //

base& operator+(char*); // "+ "

//

string();

~string();

}

//------------------------------------------------------string::string() //

{

s = NULL; //

sz = 0;

}

//------------------------------------------------------string::~string() //

{

if (s !=NULL) delete s; //

}

//------------------------------------------------------int string::GET()

{

char ss[80];

if (s !=NULL) delete s;

gets(ss); //

s = new char[sz = strlen(ss)+1];//

strcpy(s,ss);

return(1); //

}

//------------------------------------------------------void string::PUT()

{ puts(s); }

//------------------------------------------------------int string::CMP(base* two) //

{

string *p = (string*) two; //

return(strcmp(s, p->s); //

} // ( )

//-------------------------------------------------------char *string::NAME() //

{ return(""); }

//-------------------------------------------------------base *string::COPY() //

{ //

string *p = new string; //

return(p); //

}

//-------------------------------------------------------string::operator long() // long { //

return (sz);

}

//-------------------------------------------------------string::operator char*() // char* { // char *p = new char[sz]; //

strcpy(p,s);

return(p);

}

//--------------------------------------------------------base& string::operator+(char* two) // "+ "

{ //

char ss[80]; //

strcpy(ss,s); //

strcat(ss,two);

delete s;

s = new char[sz = strlen(ss)+1];

strcpy(s,ss);

return(*(base*)this); //

} //

//-------------------------------------------------------

"base"

. , .

class base

{

public:

virtual int GET()=0; //

virtual void PUT()=0; //

virtual int CMP(base*)=0; //

virtual char *NAME()=0; //

virtual base *COPY()=0; //

virtual operator long()=0; // long

virtual operator char*()=0; // char*

virtual base& operator+(char*)=0;

// "+ "

virtual ~base(); //

//

}; //

:

-

;

-

base. ;

- ,

( );

- (table) TBL

.

( ).

- head. , .

COPY , GET.

.

,

(TYPE) string,integer,dat ...

TYPE[i]->NAME().

.

class table

{

int nc; //

int nr; //

char **names; //

base **head; //

//

base ***TBL; //

public:

void append(); //

void sort(int); //

long averrage(int); //

//

base& operator()(int,int);

//

table(); // -

~table(); // -

}

TBL

-- ---------0

------->+--------+..

L-- +--------+i Ġ string

base*** ----------->---------0 integer

+--------+ +--------+.. real

+--------+ +--------+j --dat-------

base** -------------->-base-----

+--------+ L----------

base*

L----------- base

head

-- S0

-------------->---------0 -string---

L-- ------------------>-base---

base** +--------+ ---------->L--------

-------------- L--------- +--------+ D0

--------- -dat------

+--------+ L--->-base---

base* L--------

L---------//------------------------------------------------------// ( )

string S0;

dat D0;

time T0;

integer I0;

base *TYPE[] = {

(base*) &S0;

(base*) &D0;

(base*) &T0;

(base*) &I0;

};

//-----------------------------------------------------//

#define MAXCOL 30

#define MAXREC 1000

table::table()

{

int i,j,n;

char ss[80];

names = new char*[MAXCOL]; //

head = new base*[MAXCOL]; //

for (nc=0; nc

{ //

gets(ss);

if (strlen(ss)=0) break;// -

name[nc] = new char[strlen(ss)+1];

//------

...

for (j=0; j<3; j++)

{

gotoxy(10,5+j);

cputs( TYPE[j]->NAME() );

}

//------ - n

head[nc] = TYPE[n]; // ,

//

//

TBL = new base**[MAXREC];

nr = 0; //

}

}

//------------------------------------------------------//

tabe::~table()

{

int i,j;

for (i=0; i

{

for (j=0; j

delete TBL[i][j]; // i-

// j-

//

// ( )

delete TBL[i];

}

delete TBL;

for (j=0; j

delete names[j];

}

//------------------------------------------------------//

void table::append()

{

int i;

TBL[nr] = new base*[nc]; //

for (i=0; i

{ //

TBL[nr][i] = head[i]->COPY();

printf(" %s %s :",names[i],head[i]->NAME());

//

while(TBL[nr][i]->GET() ==0);//

}

nr++;

}

//-------------------------------------------------------//

long table::averrage(int n)

{

long r;

int i;

if (n<0 || n>=nc) return(0);

for (r=0, i=0; i

return(r / nr);

}

//-------------------------------------------------------// ""

void table::sort(int n)

{

int i,k;

base *p;

do

{

for (i=0; i< nr-1; i++) //

//

// n-

if (TBL[i][n]->CMP(TBL[i+1][n]) <0)

{

p = TBL[i][n];

TBL[i][n] = TBL[i+1][n];

TBL[i+1][n] = p;

k++;

}

}

while (k); //

}

//----------------------------------------------------------// (i,j) -

// ,

//

static base empty; //

base& table::opertor()(int i,int j)

{

if (i<0 || i >= nr) return(empty);

if (j<0 || j >= nc) return(empty);

return (*TBL[i][j]); //

}

//--------------------------------------------------------//

void main()

{

int i,j;

table R; //

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

R.append(); // 10-

R.sort(1); //

scanf("%d %d", &i, &j); //

R(i,j).PUT();

R(1,2) + "1234"; // " + "

// 1,2

}

9. .

-----------------

,

, , ...

, .

(),

() .

.

堠 ࠠ 堠 蠠

.

++

(, ) (template).

,

.

--- - "T",

---

template class vector

{

int tsize; //

int csize; //

T **obj; // // "T"

public:

T *operator[](int);

// [int]

//

// "T"

void insert(T*); // "T"

int extract(T*); //

};

-, .

"vector" (), "":

vector a;

vector b;

extern class time;

vector

,

(,

):

class vector

{

int tsize;

int csize;

int **obj;

public:

int *operator[](int);

void insert(int*);

int index(int*);

};

, ,

. , : -

. :

--- - "T",

--- -

-

template T* vector::operator[](int n)

{

if (n >=tsize) return(NULL);

return (obj[n]);

}

template int vector::index(T *pobj)

{

int n;

for (n=0; n

if (pobj == obj[n]) return(n);

return(-1);

}

,

(,

). , 堠

.

int* vector::operator[](int n)

{

if (n >=tsize) return(NULL);

return (obj[n]);

}

int vector::index(int *pobj)

{

int n;

for (n=0; n

if (pobj == obj[n]) return(n);

return(-1);

}

-,

. ,

, .  

.

template class FIFO

{

int fst,lst; // -

//

T queue[size]; // "T"

// "size"

public:

T from(); // -

void into(T); //

FIFO(); //

};

template FIFO::FIFO()

{

fst = lst = 0;

}

template T FIFO::from()

{

T work;

if (fst !=lst)

{

work = area[lst++];

lst = lst % size;

}

return(work);

}

template void FIFO::into(T obj)

{

area[fst++] = obj;

fst = fst % size;

}

:

FIFO<100> a;

FIFO<20> b;

struct x {};

FIFO<50> c;

"a".

class FIFO<100>

{

int fst,lst;

double queue[100];

public:

double from();

void into(double);

FIFO();

};

FIFO<100>::FIFO()

{

fst = lst = 0;

}

double FIFO<100>::from()

{

double work;

if (fst !=lst)

{

work = area[lst++];

lst = lst % 100;

}

return(work);

}

void FIFO<100>::into(double obj)

{

area[fst++] = obj;

fst = fst % 100;

}

Š   1. ------------------------------------------------------------- 70- -

 

 

 

! , , , .
. , :