.

Объективное программирование

Язык: русский
Формат: реферат
Тип документа: Word Doc
0 2187
Скачать документ

AAAAeAIEA A IAUAEOIIA I?IA?AIIE?IAAIEA

Eaeoeey 1. Iauaeoiia i?ia?aiie?iaaiea eae oaoiieiaey i?ia?aiie?iaaiey

————————————————————-

O?aaeeoeeiiiay oaoiieiaey i?ia?aiie?iaaiey 70-o aiaeia –
no?oeoo?iia i?ia?aiie?iaaiea:

– iiaeoeueiia i?ia?aiie?iaaiea;

– ienoiaeyuaa i?ia?aiie?iaaiea;

– no?oeoo?iia i?iaeoe?iaaiea i?ioeaaeo? e aeaiiuo (i?ia?aiie?iaaiea
aac goto).

sscue Ianeaeue – niioaaonoaoao oeacaiiui i?eioeeiai e aue
?ac?aaioai iiae aeeyieai eaeae no?oeoo?iiai i?ia?aiie?iaaiey.

Aeueoa?iaoeaiue iiaeoiae – ainoiaeyuaa i?ia?aiie?iaaiea –
i?aaeiieaaaao a i?inoaeoai neo/aa nicaeaiea neiy no?oeoo? aeaiiuo e
i?ioeaaeo?, iaania/eaathueo iieiue iaai? aeaenoaee iaae iauaeoaie,
eioi?ua i?aaenoaaeaiu a aeaiiie caaea/a. I?eia? o?aaeeoeeiiiiai
iiaeoiaea aeaeeioaea noaiaea?oiuo ooieoeee.

Neaaeothuee oaa – aaaaeaiea a i?ia?aiio iauaeoia. Iiae iauaeoii
iiieiaaony no?oeoo?a aeaiiuo, eioi?ay niaea?aeeo iieioth eioi?iaoeeth i
ninoiyiee niioaaonoaothuaai oece/aneiai iauaeoa, eioi?ue ioia?aaeaaony
i?ia?aiiie. A Ne yoiio iiaeao niioaaonoaiaaoue no?oeoo?a struct),
a Ianeaea – caienue (record). Iiiaeanoai iauaeoia iaeiiai oeia
ninoaaeytho iiiyoea eeanna. Iauaeoii-i?eaioe?iaaiiue iiaeoiae e
?ac?aaioea i?ia?aii i?aaeiieaaaao, /oi a i?ia?aiia onoaiaaeeaaaony
acaeiii-iaeiicia/iia niioaaonoaea iaaeaeo oece/aneeie iauaeoaie,

ioia?aaeaaiuie i?ia?aiiie, e i?ia?aiiiuie iauaeoaie, yaeythueieny, ii
nouanoao, no?oeoo?e?iaaiiuie ia?aiaiiuie (a aeaeueiaeoai iiae
oa?ieiii “iauaeo” aoaeai iiieiaoue i?ia?aiiiue iauaeo).

O?aaeeoeeiiiue iiaeoiae: ———- ia?aiaiiay oei aeaiiuo

Iauaeoii-i?eaioe?i- oece/aneee i?ia?aiiiue eeann

aaiiue iiaeoiae: iauaeo iauaeo iauaeoia

I?e nicaeaiee iauaeoia i?ia?aiieno ii?aaeaeyao iiiaeanoai ooieoeee,
i?e iiiiue eioi?uo (a oi/iaa, eneeth/eoaeueii /a?ac eioi?ua)iaae
iauaeoii auiieiyaony iaeioi?ia aeiionoeiia iiiaeanoai iia?aoeee.

Oaeea ooieoeee aeieaeiu eiaoue iaycaoaeueiue ia?aiao? – nnueeo ia
oaeouee iauaeo, aeey eioi?iai iie aucuaathony. Naie ooieoeee yaeythony
iaiouaieeiie /anoueth iiiyoey eeanna iauaeoia, oae eae iie ii?aaeaeytho
aiciiaeiua aeaenoaey iaae iauaeoaie iaeiiai e oiai aea oeia (oi anoue
eeanna).

Iauaeoii-i?eaioe?iaaiiua i?ia?aiiu iiaeii ?ac?aaaouaaoue e n
iiiiuueth o?aaeeoeeiiiuo ycueia i?ia?aiie?iaaiey. ?anniio?ei i?eia?
ii?aaeaeaiey iauaeoia oeia “aeaoa” ia eeanne/aneii Ne.

//—— no?oeoo?a dat – aiaeia eeanna iauaeoia “aeaoa” ——–typedef
struct dat

{

unsigned day;

unsigned month;

unsigned year;

}

DAT;

//—– iaai? ooieoeee aeey eeanna iauaeoia “aeaoa” ————–static
int mm[] = {31,28,31,30,31,30,31,31,30,31,30,31};

//—– I?iaa?ea ia ei??aeoiinoue —————————–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);

}

//—— Neaaeothuay aeaoa —————————————-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++;

}

//——- Neaaeothuay aeaoa /a?ac n aeiae ————————–void
PlusData(p,n)

DAT *p;

int n;

{

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

}

//——- Iniiaiay i?ia?aiia ———————————

void main()

{

DAT a;

do

{

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

}

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

PlusData(&a, 17);

}

//——————————————————–

Oaeoe/anee ii?aaeaeaiea eeanna iauaeoia eae oeia aeaiiuo e
ecaanoiiai iaai?a ooieoeee aeey auiieiaiey iia?aoeee iaae ia?aiaiiuie
yoiai oeia yeaeaaeaioii iiiyoeth aaciaiai oeia aeaiiuo (AOAe) ycuea
i?ia?aiie?iaaiey. Aaeeinoaaiiia ioee/ea eeanna io AOAe caeeth/aaony a
oii, /oi ia?aue ii?aaeaeyaony i?ia?aiienoii, a aoi?ie ano?iai a
ii?aaeaeaiea ycuea i?ia?aiie?iaaiey.

sscue i?ia?aiie?iaaiey Ne++ i?aaenoaaeyao niaie ?anoe?aiea
ycuea Ne aeey i?ia?aiie?iaaiey iauaeoia e eo eeannia. I?e yoii
eniieueciaaiea eeannia yeaeaaeaioii aieioue aei neioaenena
eniieueciaaieth aaciauo oeiia aeaiiuo:

Iiiyoey eeanne/aneiai Ne Iiiyoey Ne++

————————- ———– AOAe:
Eeann:

yeaiaio aeaiiuo ycuea, aeey ii?aaeaeyaiay iieueciaaoaeai

eioi?iai ecaanoii iiiaeanoai no?oeoo?a, yeaiaiou eioi?ie

cia/aiee, oi?ia i?aaenoaaeaiey, yaeythony ?aiaa ii?aaeaeai iaai?
iia?aoeee. iuie oeiaie aeaiiuo e eeannaie,

e iiiaeanoaa ooieoeee,iia?e?othueo n
iei.

——————————————————— Ia?aiaiiay:
Iauaeo:

iaeanoue iaiyoe, niaea?aeauay ia?aiaiiay, niaea?aeauay

no?oeoo?o aeaiiuo ii?aaeaeaiiiai no?oeoo?o aeaiiuo, ii?aaeaeaiioth

oeia. eae eeann.

——————————————————— Iia?aoeey:
Ia?aii?aaeaeaiea iia?aoi?ia:

iia?aoeey iaae ia?aiaiiie eioa?- ooieoeey, ii?aaeaeaiiay aeey iauae
i?aoe?oaony ii ioiioaieth e oiio oia oeacaiiiai eeanna iiaeao auoue

AOAe, e eioi?iio ioiineony ia?a- aucaaia a aeaea iaeiie ec noaiaea?o
iaiiay (oae iia?aoeey ‘+’ iuo iia?aoeee ycuea Ne, eioi?ay

ii-?aciiio eioa?i?aoe?oaony aeey ia?aii?aaeaeyaony, anee iia?aiaeii

ia?aiaiiuo oeia int e double). aa yaeyaony iauaeo eeanna, a ia

ia?aiaiiay AOAe.

Eaeoeey 2. Aeiiieieoaeueiua aiciiaeiinoe ycuea Ne++

———————————————–

Ieaea ?anniio?ei n?aaenoaa, ?anoe?ythuea eeanne/aneee Ne. Oioy

iie e ia ioiinyony iaiin?aaenoaaiii e eeannai, n eo iiiiuueth iiaeii

?aaeeciaaoue ?anniio?aiiua auoa i?eioeeiu iauaeoii-i?eaioe?iaaiiiai

i?ia?aiie?iaaiey.

2.1. I?enaaeaaiea no?oeoo?

————————- Iia?aoeey i?enaaeaaiey iiaeao auoue
i?eiaiaia e no?oeoo?ai iaeiiai oeia. A yoii neo/aa i?aaeiieaaaaony eo
iiaaeoiia eiie?iaaiea

iaeiie a ae?oaoth. Iia (a ia nnueea ia iaa) iiaeao auoue oaeaea
oaeoe/aneei ia?aiao?ii e ?acoeueoaoii ooieoeee. Anee eiaaony nnueea ia

no?oeoo?o n eiaiai p, oi ?acoeueoaoii iia?aoeee *p yaeyaony no?oeoo?a a
oeaeii. Oaeei ia?acii, no?oeoo?a i?eaeeaeaaony e aaciaui oeiai aeaiiuo a
oii niunea, /oi iaae iae aiciiaeiu auoaoeacaiiua iia?aoeee. Aeey
iaicia/aiey no?oeoo?u iiaeii oaeaea eniieueciaaoue eiy

no?oeoo?u aac eeth/aaiai neiaa struct.

struct dat

{ int day,month,year; }

dat NextDat(dat x) // Oi?iaeueiue ia?aiao? – no?oeoo?a

{ … return(x); } // Aica?aoeoue no?oeoo?o eae ?acoeueoao

dat Nextdat1(dat *p)

{ … return(*p); } // Aica?ao no?oeoo?u einaaiii ii nnueea

dat a,b,c,*q; // Eeth/aaia neiai struct ia eniieuecoaony

void main()

{

q = &b;

a = b; // I?yiia i?enaaeaaiea no?oeoo?

a = *q; // Einaaiiia i?enaaeaaiea ii nnueea

c = NextDat(b); // I?enaaeaaiea no?oeoo?u eae ?acoeueoaoa

c = NextDat1(&b); // ooieoeee, oaeoe/aneee ia?aiao? a

} // NextDat – eiiey no?oeoo?u

2.2. Ia?auaiey ii aae?ano (iayaiay nnueea)

—————————————-

I?e ?aaioa ni no?oeoo?aie aieueoiai ?acia?a – i?e ia?aaea/a eo

a ea/anoaa ia?aiao?ia e ?acoeueoaoia ooieoeee – eiie?iaaiea eo yaeyaony
iayooaeoeaiie iia?aoeeae. Ai?acaei yooaeoeaiaa ia?aaeaaaoue nnueeo ia
yoo no?oeoo?o. Aeey oiai, /oiau iinoiyiii ia oeacuaaoue iia?aoeee
acyoey aae?ana e einaaiiiai ia?auaiey ii nnueea a Ne++ aaaaeai oei
– iayaiay nnueea: i?e ii?aaeaeaiee ia?aiaiiie iayaii aaiaeeony nnueea,
oeacuaathuay ia yoo ia?aiaiioth. Eniieueciaaiea yoie ia?aiaiiie a
aieueoeinoaa iia?aoeee i?aaeiieaaaao einaaiiia ia?auaiea ii
niioaaonoaothuae nnueea. I?e eieoeeaeecaoeee oaeie ia?aiaiiie cia/aieai
ae?oaie ia?aiaiiie nicaeaaony nnueea ia yoo ae?oaoth ia?aiaiioth. I?e
eniieueciaaiee a ethaii au?aaeaiee ia?aiaiiie – iayaiie nnueee ?aaeueii
i?iecaiaeeony einaaiiia ia?auaiea ii nicaeaiiie nnueea.

Ne++ Yeaeaaeaio a “eeanne/aneii” Ne

————————
—————————–//————— Eieoeeaeecaoeey
eiinoaioie —————–int &a = 5; int a,
*pa =a;

*pa = 5;

//————— Eieoeeaeecaoeey ia?aiaiiie —————–int
x; int x,*pa;

int &a = x; pa = &x;

a = 5; *pa = 5;

//————– Iayaiay nnueea ia no?oeoo?o —————-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; // Eiie?iaaiea pc->day = pb->day;

// no?oeoo?u pc->month = pb->month;

pc->year = pb->year;

Iaeaieaa /anoi iayaiua nnueee eniieuecothony i?e ia?aaea/a
ia?aiao?ia e ?acoeueoaoia ooieoeee. A yoii neo/aa o?aineyoi? nai
auae?aao, /oi iaiaoiaeeii eniieueciaaoue a ea/anoaa oaeoe/aneiai
ia?aiao?a – ia?aiaiioth eee nnueeo ia iaa, e /oi eniieuecoaony a
ea/anoaa ?acoeueoaoa – nnueea eee ia?aiaiiay, einaaiii aae?anoaiay ii
nnueea. Oeaeue iiaeiaiuo ooeu?aiee aoaeao aeaeia iicaeiaa – i?e
ia?aii?aaeaeaiee iia?aoi?ia, a iiea iiaeii caiaoeoue, /oi aucia
ooieoeee, n

ia?aiao?aie – iau/iuie cia/aieyie e iayaiuie nnueeaie – neioaene/anee
eaeaioe/ai. Oi aea naiia eanaaony ?acoeueoaoia.

A ea/anoaa eeethno?aoeee ?anniio?ei o?e i?eia?a ooieoeee, eiathueo
a ea/anoaa oi?iaeueiiai ia?aiao?a e ?acoeueoaoa no?oeoo?o, eioi?ay
ia?aaeaaony niioaaonoaaiii:

– cia/aieai;

– yaiie nnueeie;

– iayaiie nnueeie.

I?eia? 1. Ia?aiao?u – cia/aiey

———————————————————dat Inc(dat
x) ========> – eiie?iaaiea

{ ——–> – nnueea

x.day++;

return(x); —-¬ noae +—+x.day++

} ¦ b =========> x =========¬

L—- +—+ ¦ return(x)

void main() ¦

{ —-¬ noae +—+ –¦-¬ a?aiaiiay

dat a,b,*p; ¦ a <========= x <======= ¦ ia?aiaiiaya = Inc(Inc(b)); L---- +---+ L---p = &Inc(b); x.day++a = *p;}I?eia? 2. Ia?aiao?u - yaiua nnueee---------------------------------------------------------dat* Inc(dat* x) x->day++

{ x->day++

x->day++; —-¬ noae +—+

return(x); a===== b <--------- x ¦} ¦ --> <----¬ +-¦-+¦ ¦ L---- ¦ ¦return(x)¦ ¦ a======+=====void main() a=*..¦ ¦ ¦ ¦ +---+ noae{ ¦ ¦ --¦-¬ L---- x ¦dat a,b,*p; ¦ ¦ ¦ =========> ¦

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

p = Inc(&b); ¦ ¦ —-¬ ¦return(x)

a = *p; ¦ L– <===========} ¦ L--- ¦ ----¬L====> a ¦

L—

I?eia? 3. Ia?aiao?u – iayaiua nnueee

———————————————————dat&
Inc(dat& x) x.day++ iayaiay nnueea dat* px

{ x.day++

x.day++; —-¬ noae +—+

return(x); a===== b <--------- px¦} ¦ --> <----¬ +-¦-+¦ ¦ L---- ¦ ¦return(px)¦ ¦ a======+=====void main() a=*..¦ ¦ ¦ ¦ +---+ noae{ ¦ ¦ --¦-¬ L---- px¦dat a,b,*p; ¦ ¦ ¦ =========> ¦

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

p = &Inc(b); ¦ ¦ —-¬ ¦return(px)

a = *p; ¦ L– <===========} ¦ L--- ¦ ----¬L====> a ¦

L—

N?aaiaiea yoeo i?eia?ia iieacuaaao neaaeothuaa:

– i?e ?aaioa n oi?iaeueiui ia?aiao?ii – iayaiie nnueeie
eniieuecoaony eiy oi?iaeueiiai ia?aiao?a a ea/anoaa eaeaioeoeeaoi?a
ia?aiaiiie, eioi?ay caiaiyaony o?aineyoi?ii ia einaaiiia ia?auaiea ii
iayaiie nnueea;

– i?e aica?auaiee ?acoeueoaoa eniieuecoaony eiy
ia?aiaiiie,eioi?ay caiaiyaony o?aineyoi?ii iayaiie nnueeie ia iaa;

– i?eia?u 2 e 3 eaeaioe/iu ii ?aaeecaoeee, ii ioee/athony ii
neioaeneno auciaa ooieoeee;

– i?eia?u 1 e 3 ioee/athony ii ?aaeecaoeee, ii eaeaioe/iu ii
neioaeneno auciaa ooieoeee;

– ec i?aaeuaeouaai neaaeoao, /oi i?e auciaa ooieoeee nienie
oaeoe/aneee ia?aiao?ia iaaeinoaoi/ai aeey ii?aaeaeaiey o?aineyoi?ii
niiniaa eo ia?aaea/e (cia/aieai eee nnueeie), iiyoiio a Ne++ aeey
eaaeaeie aiaoiae ooieoeee iaiaoiaeeii caaeaoue i?ioioei.

Oae eae ?acia? no?oeoo?u, ia?aaeaaaaiie a ea/anoaa ?acoeueoaoa
ooieoeee, iiaeao auoue neieue oaiaeii aieueoei, oi aeey aai o?aiaiey
iaiaoiaeeii nicaeaoue a?aiaiioth ia?aiaiioth. O?aineyoi? “Borland C” a
yoii neo/aa iinooiaao neaaeothuei ia?acii:

– i?e aoiaea a ooieoeeth a noaea i?aaeiieaaaaony nouanoaiaaiea
iayaiiai ia?aiao?a – “aeeeiiie” nnueee ia no?oeoo?o, a eioi?ie
?aciauaaony ?acoeueoao ooieoeee;

– i?e auiieiaiee iia?aoeee return(x), aaea x – eieaeueiay
ia?aiaiiay eee oi?iaeueiue ia?aiao?, auiieiyaony iiaaeoiaia eiie?iaaiea
ia?aiaiiie x ii aae?ano, caaeaiiiio iayaiui ia?aiao?ii;

– anee ?acoeueoao ooieoeee iaiin?aaenoaaiii i?enaaeaaaony ae?oaie
ia?aiaiiie-no?oeoo?a, oi i?e auciaa oaeie ooieoeee a noae iiiauaaony
iayaiue ia?aiao? – nnueea ia ia?aiaiioth a eaaie /anoe iia?aoeee
i?enaaeaaiey;

– ai anao inoaeueiuo neo/ayo a aucuaathuae ooieoeee nicaeaaony ii
iaeiie iayaiie aaoiiaoe/aneie ia?aiaiiie ia eaaeaeue aucia ooieoeee,
aica?auathuae no?oeoo?o a ea/anoaa ?acoeueoaoa, a i?e auciaa
ia?aaeaaony niioaaonoaothuay nnueea ia yoo ia?aiaiioth-no?oeoo?o.
Oaeea ia?aiaiiua eiatho ana naienoaa aaoiiaoe/aneeo, iie nouanoaotho ana
a?aiy ?aaiou aucuaathuae ooieoeee, aiciiaeii aeaaea iieo/eoue nnueeo
ia oaeoth ia?aiaiioth.

I?ia?aiia ia Ne++ ?aaeecaoeey

—————– ———- — iayaiue ia?aiao?

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

{ {

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

return(x); *r = x; // Eiie?iaaiea

} } // ?acoeueoaoa

void main() void main()

{ {

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

dat t,u; // A?aiaiiye ia?aiaiiye

a = Inc(b); Inc(&a,b); // Nnueea ia eaaoth /anoue

p = &Inc(b); Inc(&t,b); // I?enaeaaiea a?aiaiiie

p = &t; // ia?aiaiiie e iieo/aiea

a = *p; a = *p; // nnueee ia iaa

a = Inc(Inc(b)); Inc(&u,b); // I?iiaaeooi/iue ?acoeueoao

Inc(&a,u); // ai a?aiaiiie ia?aiaiiie

} }

2.3. Ooieoeee – yeaiaiou no?oeoo?u

——————————– Iiaoi?ei ?anniio?aiiue auoa
i?eia? a ae?oaie oi?ia:

//———— no?oeoo?a dat – aiaeia eeanna iauaeoia “aeaoa” –struct
dat

{

unsigned day;

unsigned month;

unsigned year;

int TestData();

void NextData();

void PlusData(int n)

{

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

}

};

//———– iaai? ooieoeee aeey eeanna iauaeoia “aeaoa” ——–static
int mm[] = {31,28,31,30,31,30,31,31,30,31,30,31};

//———– I?iaa?ea ia ei??aeoiinoue ———————–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);

}

//———– Neaaeothuay aeaoa ———————————-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++;}//--------- Iniiaiay i?ia?aiia --------------------------------void main(){dat a;do{scanf("%d%d%d", &a.day, &a.month, &a.year);}while(a.TestData() ==0);a.PlusData(17);}//------------------------------------------------------- Eae aeaeii ec i?eia?a, a ea/anoaa yeaiaioia no?oeoo?u iiaooaunooiaoue ooieoeee. Oaeea yeaiaiou-ooieoeee eiatho neaaeothuea iniaaiiinoe:- oaei ooieoeee iiaeao auoue ii?aaeaeaii a naiie no?oeoo?a (ooieoeey PlusData). A yoii neo/aa ooieoeey eiaao noaiaea?oiue aeae;- a ii?aaeaeaiee no?oeoo?u aeaaony oieueei i?ioioei ooieoeee(caaieiaie n ia?a/eneaieai oeiia oi?iaeueiuo ia?aiao?ia). Ii?aaeaeaiea naiie ooieoeee aeaaony ioaeaeueii, i?e yoii iieiia eiy ooieoeeeeiaao aeae::

– a oaea ooeoeee iayaii ii?aaeaeai iaeei oi?iaeueiue ia?aiao? n

eiaiai this – nnueea ia no?oeoo?o, aeey eioi?ie aucuaaaony ooieoeey

(A iaoai i?eia?a yoi aoaeao struct dat *this ). Iiey yoie no?oeoo?u
aeinooiiu /a?ac yaiia eniieueciaaiea yoie nnueee

this->month = 5;

this->day++;

eee iayaii

month = 5;

day++;

– aeey ia?aiaiiie, eiathuae oei iaeioi?ie no?oeoo?u, aucia

ooieoeeeyeaiaioa yoie no?oeoo?u eiaao aeae

. ( )

2.4. Ia?aii?aaeaeaiea ooieoeee

————————— A Ne++ aiciiaeii ii?aaeaeaiea
ianeieueeeo ooieoeee n iaeeiaeiaui

eiaiai, ii n ?aciuie oeiaie oi?iaeueiuo ia?aiao?ia. I?e yoii eiiieeyoi?
auae?aao niioaaonoaothuoth ooieoeeth ii oeio oaeoe/aneeo ia?aiao?ia.
Ia?aii?aaeaeyaioth ooieoeeth iaiaoiaeeii iauyaeoue n eeth/aaui

neiaii overload:

overload SetDat;

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

{ // Aeaoa aaiaeeony a aeaea o?ao oeaeuo

p->day=dd;

p->month=mm;

p->year=yy;

}

void SetDat(char *s,dat *p) // Aeaoa aaiaeeony a aeaea no?iee

{

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

}

void main()

{

dat a,b;

SetDat(12, 12, 1990, &a); // Aucia ia?aie ooieoeee

SetDat(“12,12,1990″, &b); // Aucia aoi?ie ooieoeee

}

Ooieoeee-yeaiaiou oaeaea iiaoo auoue ia?aii?aaeaeaiu, i?e yoii
yaiiai iauyaeaiey ia o?aaoaony.

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. Iia?aoi?u oi?aaeaiey aeeiaie/aneie iaiyoueth

———————————————

A aeaeeioaea Ne eiathony aeaa ooieoeee oi?aaeaiey aeeiaie/aneie
iaiyoueth – malloc() e free(), eioi?ua auaeaeytho e inaiaiaeaeatho
iaeanoue iaiyoe caaeaiiiai ?acia?a (a aaeoao). A yoie iaeanoe
i?ia?aiia iiaeao ?acianoeoue ia?aiaiioth (eee iannea), eioi?ay
iacuaaaony aeeiaie/aneie. I?e auaeaeaiee iaiyoe iiae aeeiaie/aneoth
ia?aiaiioth iaiaoiaeeii i?e iiiiue iia?aoeee sizeof ii?aaeaeyoue
eiee/anoai aaeoia, iaiaoiaeeiia aeey ?aciauaiey ia?aiaiiie oeacaiiiai
oeia. A Ne++ aaaaeaiu aeaa iia?aoi?a, aiaeiae/iua ooieoeeyi malloc e
free new e delete. Iie ioee/athony io niioaaonoaothueo ooieoeee oai,
/oi aeiioneatho eniieueciaaiey a ea/anoaa a?aoiaioia
iaiin?aaenoaaiii niaoeeoeeaoeeth oeia nicaeaaaaiie aeeiaie/aneie
ia?aiaiiie e nnueee ia aeeiaie/aneoth ia?aiaiioth:

Ne++ “Eeanne/aneee” Ne

————————- ———————————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);

}

Iia?aoi?u eiatho aeae:

new iienaoaeue oeia>

delete

2.6. Ia?aiao?u ooieoeee ii oiie/aieth

———————————-

I?e ii?aaeaeaiee oi?iaeueiuo ia?aiao?ia ooieoeee iiaeao auoue

oeacaii aai cia/aiea, i?eieiaaiia i?e auciaa ii oiie/aieth i?e

ionoonoaee yoiai ia?aiao?a a nienea oaeoe/aneeo:

//—– Ooieoeey onoaiaaeeaaao ii oiie/aieth oaeouaa cia/aiea aiaea,

//—– ianyoea e aeiy

#include

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

{

struct date x;

getdate(&x); // Noaiaea?oiay ooieoeey iieo/aiey

// oaeouae aeaou

// I?iaa?ea ia cia/aiea ii oiie/aieth

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

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

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

}

2.7 Eiio?ieue i?aia?aciaaiey oeiia nnueie

—————————————

A “eeanne/aneii” Ne i?e auiieiaiee i?enaaeaaiey, ia?aaea/a
oaeoe/aneeo ia?aiao?ia i?ienoiaeeo aaoiiaoe/aneia i?aia?aciaaiea
nnueie e aaciaui oeiai aeaiiuo (int,unsigned) e iaiai?io, a oaeaea
i?aia?aciaaiea iaeiiai oeia nnueee e ae?oaiio. A Ne++ oaeea
“aieueiinoe” eneeth/aiu, i?ia?aiieno aeieaeai nai auiieieoue yaiia
i?aia?aciaaiea. Iai?eia?, i?e eniieueciaaiee ooieoeee ?ani?aaeaeaiey
aeeiaie/aneie iaiyoe, eiathuae i?ioioei a “alloc.h”

extern void* malloc(int n);

dat *p;

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

¦

L— i?aia?aciaaiea void* a dat*

Anoanoaaiii, /oi yoi i?aia?aciaaiea oeiia oeeoeaiia a oii
niunea, /oi ia iaiyao cia/aiey nnueee e ia i?eaiaeeo e aaia?aoeee
eiaea. Iii oieueei iaiyao “oi/eo c?aiey” o?aineyoi?a ia aeaiioth
nnueeo.

2.8 Anoaaeyaiua (inline) ooieoeee

——————————-

Anee ooieoeey (iau/iay eee yeaiaio-ooieoeey no?oeoo?u eee
eeanna) iauyaeaiu inline-ooieoeeyie, oi i?e auciaa oaeeo ooieoeee
o?aineyoi? auiieiyao iiaenoaiiaeo ii oaenoo i?ia?aiiu oaea ooieoeee n
niioaaonoaothuae caiaiie oi?iaeueiuo ia?aiao?ia ia oaeoe/aneea.
Yeaiaio-ooieoeey oaeaea n/eoaaony inline ii oiie/aieth, anee aa oaei
ii?aaeaeaii iaiin?aaenoaaiii a ii?aaeaeaiee no?oeoo?u (eee
eeanna),iai?eia?:

struct dat

{

int d,m,y;

void Setdat(char *p) // Ooieoeey inline ii oiie/aieth

{

… // Oaei ooieoeee

}

2.9 Nnueee ia yeaiaiou no?oeoo?u

——————————-

Anee no?oeoo?a eiaao ianeieueei yeaiaioia iaeiiai oeia,oi aeey iaa
iiaeao auoue nicaeaia “aioo?aiiyy” nnueea, eioi?ay i?eieiaao
cia/aiea aioo?aiiaai aae?ana (niauaiey) yeaiaioa ioiineoaeueii aua?aiiie
no?oeoo?u. Oi?ie?iaaiea e eniieueciaaiea oaeie nnueee ynii

ec i?eia?a:

struct dat

{

int day,month,year;

void Getdat();

void Putdat();

void Nextdat();

}

int dat::*p; // Nnueea ia yeaiaio oeia int

// a no?oeoo?a dat

p = & dat::month; // Cia/aiea p – niauaiea (aae?an)

// yeaiaioa month a no?oeoo?a oeia

// dat

dat x,*px = &x; //

x.*p = 5; // Ia?auaiea ii aioo?aiiae nnueea

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

// <*dat>-> *

Yeaeaaeaioii

x.month = 5;

px->month =5;

Aiaeiae/iay aioo?aiiyy nnueea iiaeao auoue nicaeaia aeey
yeaiaioia-ooieoeee, i?eiaaeeaaeaueo iaeiie no?oeoo?a, i?e yoii
ooieoeee

aeieaeiu auoue eaeaioe/iuie ii ?acoeueoaoai e ia?aiao?ai:

void (dat::*fun)(); // Nnueea ia yeaiaio-ooieoeeth

// no?oeoo?u dat

fun = & dat::Putdat(); // Cia/aiea fun – nnueea ia

// yeaiaio-ooieoeeth Putdat a dat

(x.*fun)(); // Aucia yeaiaioa-ooieoeee ii

(px->*fun)(); // nnueea fun aeey no?oeoo?u x

// e aeey no?oeoo?u ii nnueea px

Yeaeaaeaioii

x.Putdat();

px->Putdat();

2.10 Iaeciaiyaiua ia?aiaiiua (eiinoaiou)

—————————————

A Ne++ aaaaeai aeiiieieoaeueiue eiio?ieue ca eciaiaieai cia/aiee
ia?aiaiiuo. Eeth/aaia neiai const, eniieuecoaiie i?e ii?aaeaeaiee e
eieoeeaeecaoeee ia?aiaiiie, cai?auaao aa eciaiaiea, /oi eiio?iee?oaony
o?aineyoi?ii i?e aa aeaeueiaeoai eniieueciaaiee. Oaeay aea
aiciiaeiinoue nouanoaoao e aeey oi?iaeueiuo ia?aiao?ia ooieoeee,
iai?eia?:

const int n=5;

n++; // Cai?auaii

int xxx(const int m)

{

m++; // Cai?auaii

}

I?eiaieoaeueii e nnueea const iiaeao eniieueciaaoueny a aeaoo
aa?eaioao, i?eiaieoaeueii e naiie nnueea (aae?ano) e i?eiaieoaeueii e
oeacoaiiio cia/aieth:

– i?e eniieueciaaiee conts i?eiaieoaeueii e oeacoaiiio cia/aieth
?ac?aoaaony iiaeeoeoee?iaaoue naio nnueeo i?e iiiiue i?enaaeaaiey e
iia?aoeee aae?aniie a?eoiaoeee, a eciaiaiey iia?aiaea einaaiii ii nnueea
cai?auaiu. Oaeay nnueea iacuaaaony nnueeie ia iinoiyiiue iauaeo:

const char * p;

p = “1234567890”; // ?ac?aoaii i?enaaeaaiea nnueea

p + =3; // ?ac?aoaia iiaeeoeeaoeey nnueee

*(p+3) = ‘3’; // Cai?auaii i?enaaeaaiea ii nnueea

(*p)++; // Cai?auai eie?aiaio ii nnueea

– i?e eniieueciaaiee const i?eiaieoaeueii e nnueea cai?auaaony
iaiyoue cia/aiea nnueee iinea eieoeeaeecaoeee, a oii /enea n?aaenoaaie
aae?aniie a?eoiaoeee. Oaeay nnueea iacuaaaony iinoiyiiie nnueeie ia
iauaeo:

char const* p = “1234567890”;

char c;

(*p) = ‘3’; // ?ac?aoaii i?enaaeaaiea ii nnueea

p++; // Cai?auaii eciaiaiea cia/aiey

c = *(p+3); // naiie nnueee

Iieiay oeenaoeey nnueee e aae?anoaiiai ath iauaeoa aiciiaeia a
aeaea

const char const* p = “1234567890”;

2.11 Iauea caia/aiey i aeiiieiaieyo a Ne++

—————————————-

Iniiaiua ioee/ey Ne++ io “eeanne/aneiai” Ne:

– no?oeoo?a (struct) i?eaeeaeaia ii naienoaai e aaciaui oeiai

aeaiiuo (char,int);

– aaaaeaii iiiyoea yeaiaioa-ooieoeee. Yeaiaiou-ooieoeee ea?atho

?ieue naiaia?aciiai “eioa?oaena” aeey eniieueciaaiey ii?aaeaeaiiie

i?ia?aiienoii no?oeoo?u;

– ?anoe?aiu aiciiaeiinoe o?aineyoi?a ii eiio?ieth e i?aia?aciaaieth
ia?aiao?ia i?e auciaa ooieoeee (iayaiay nnueea, ia?aii?aaeaeaiea,
ia?aiao?u ii oiie/aieth). Eiaiii iiyoiio auciao ethaie aiaoiae ooieoeee
aeieaeii i?aaeoanoaiaaoue iauyaeaiea aa i?ioioeia (caaieiaea ooieoeee ni
nieneii oeiia ia?aiao?ia).

Ana yoe iiaua naienoaa iaiaoiaeeiu i?e ii?aaeaeaiee iiiyoee

eeanna e iauaeoa.

Eaeoeey 3. Eeannu. Iauaeou. Eiino?oeoi?u e aeano?oeoi?u

—————————————————-

3.1.Iiiyoea eeanna e iauaeoa a Ne++

———————————-

A naiii i?inoii aeaea eeann ii?aaeaeyaony a Ne++ eae no?oeoo?a,
?aaioa n yeaiaioaie eioi?ie aiciiaeia oieueei /a?ac
yeaiaiou-ooieoeee. A ioee/ea io no?oeoo?u eeann eiaao “i?eaaoioth”
(ee/ioth) /anoue, yeaiaiou eioi?ie ia iiaoo auoue aeinooiiu eia/a eae
/a?ac ae?oaea yeaiaiouooieoeee, e “ioaee/ioth” (iauoth) /anoue, yeaiaiou

eioi?ie iiaoo auoue eniieueciaaiu iaiin?aaenoaaiii. Iauaeoii iacuaaaony
ii?aaeaeyaiay a i?ia?aiia ia?aiaiiay, oei eioi?ie ii?aaeaeai

eae eeann (no?oeoo?a):

Ii?aaeaeaiea no?oeoo?u Ii?aaeaeaiea eeanna

————————- ———————————–struct
dat class dat

{ { // I?eaaoiay /anoue

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

public: // Ioaee/iay /anoue

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

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

} aa}

void main() void main()

{ {

// Ii?aae-iea ia?aiaiiuo a,b // Ii?aae-iea iauaeoia a,b eeanna dat

dat a,b; dat a,b;

a.day = 5; // Iaiin?aaenoaaiiia eniieueciaaiea

a.month = 12; // i?eaaoiie /anoe iauaeoa cai?auaii

bAA.SetDat(“12,12,1990”); b.Setdat(“12,12,1990”);

} }

“I?eaaoiay” /anoue eeanna ia iaycaoaeueii aeieaeia neaaeiaaoue a
ia/aea ii?aaeaeaiey eeanna. Aeey aa iaicia/aiey a i?iecaieueiii
ianoa ii?aaeaeaiey eeanna iiaeii eniieueciaaoue neoaeaaiia neiai
private.

Taeei ia?acii a ia?aii i?eaeeaeaiee eeann ioee/aaony io
no?oeoo?u /aoei ii?aaeaeaiiui eioa?oaenii aeinooia e aai yeaiaioai.

Iauaeou eeanna iaeaaeatho anaie naienoaaie ia?aiaiiuo, a oii /enea
oaeeie, eae iaeanoue aeaenoaey e eeann iaiyoe (a?aiy aeecie).

Iineaaeiaa naienoai iaeaieaa eioa?anii, oae eae i?ioeannu nicaeaiey e
oie/oiaeaiey iauaeoia eeanna iiaoo nii?iaiaeaeaoueny auciaii ooieoeee
(eiino?oeoi? e aeano?oeoi?). Iaiiiiei, /oi ii eeannai iaiyoe (e
a?aiaie aeecie) a Ne ?acee/athony ia?aiaiiua:

– noaoe/aneea (aiaoiea), nicaeaaaaiua a noaoe/aneie iaiyoe
i?ia?aiiu e nouanoaothuea a oa/aiea anaai a?aiaie ?aaiou i?ia?aiiu;

– aaoiiaoe/aneea, nicaeaaaaiua a noaea a iiiaio auciaa ooieoeee e
oie/oiaeaaiua i?e aa caaa?oaiee;

– aeeiaie/aneea, nicaeaaaaiua e oie/oiaeaaiua a naiaiaeiie iaiyoe
caaea/e a iiiaiou auciaa ooieoeee malloc() e free() eee auiieiaiey
iia?aoi?ia new e delete.

Niioaaonoaaiii a i?ia?aiia aiciiaeii ii?aaeaeaiea noaoe/aneeo,
aaoiiaoe/aneeo e aeeiaie/aneeo iauaeoia iaeiiai eeanna:

class dat

{ ……. }

dat a,b; // Noaoe/aneea iauaeou

dat *p; // Nnueea ia iauaeo

void main()

{

dat c,d; // Aaoiiaoe/aneea iauaeou

p = new dat; // Aeeiaie/aneee iauaeo

delete p; // Oie/oiaeaiea aeeiaie/aneiai iauaeoa

} <---------------------// Oie/oiaeaiea aaoiiaoe/aneeo iauaeoia3.2. Nicaeaiea e oie/oiaeaiea iauaeoia.Eiino?oeoi?u e aeano?oeoi?u-------------------------------------I?ioeann nicaeaiey e oie/oiaeaiey iauaeoia eeanna annioeee?oaonyi?e iauaeoiii i?ia?aiie?iaaiee n nicaeaieai e oie/oiaeaieai niioaaonoaothueo ei oece/aneeo iauaeoia. Iiyoiio n yoeie aeaenoaeyieiaiaoiaeeii naycuaaoue ii?aaeaeyaiua i?ia?aiienoii ooieoeee aeey onoaiiaee ia/aeueiuo cia/aiee, ?aca?ae?iaaiey iaiyoe e o.ae.. Iayaiiaucuaaaiua ooieoeee i?e nicaeaiee e oie/oiaeaiee iauaeoia eeanna iacuaathony eiino?oeoi?aie e aeano?oeoi?aie. Iie ii?aaeaeythony eaeyeaiaiou-ooieoeee eeanna e eiaia eo niaiaaeatho n eiaiai eeanna.Eiino?oeoi?ia aeey aeaiiiai eeanna iiaeao auoue neieue oaiaeii iiiai,anee iie ioee/athony oi?iaeueiuie ia?aiao?aie, aeano?oeoi? aea anaaaeaiaeei e eiaao eiy ~.

N i?ioeannii nicaeaiey iauaeoia naycaii iiiyoea eo eieoeeaeecaoeee.
Eieoeeaeece?iaaoue iauaeou iau/iui niiniaii iaeuecy. Eo eieoeeaeecaoeey
inouanoaeyaony eeai yaiui i?enaaeaaieai (eiie?iaaieai)

ae?oaiai iauaeoa, eeai iayaiui auciaii eiino?oeoi?a. Anee

eiino?oeoi? eiaao oi?iaeueiua ia?aiao?u, oi a ii?aaeaeaiee ia?aiaiiie
iinea aa eiaie aeieaeiu i?enoonoaiaaoue a neiaeao cia/aiey oaeoe/aneeo
ia?aiao?ia.

Iiiaio auciaa eiino?oeoi?a e aeano?oeoi?a ii?aaeaeyaony a?aiaiai
nicaeaiey e oie/oiaeaiey iauaeoia:

– aeey noaoe/aneeo iauaeoia – eiino?oeoi? aucuaaaony ia?aae

aoiaeii a main(), aeano?oeoi? – iinea auoiaea ec main(). Eiino?oeoi?u
aucuaathony a ii?yaeea ii?aaeeaeaiey iauaeoia, aeano?oeoi?u – a

ia?aoiii ii?yaeea;

– aeey aaoiiaoe/aneeo iauaeoia – eiino?oeoi? aucuaaaony i?e

aoiaea a ooieoeeth (aeie), aeano?oeoi? – i?e auoiaea ec iaai;

– aeey aeeiaie/aneeo iauaeoia – eiino?oeoi? aucuaaaony i?e
auiieiaiee iia?aoi?a new, aeano?oeoi? – i?e auiieiaiee iia?aoi?a
delete.

A Ne++ aiciiaeii ii?aaeaeaiea ianneaa iauaeoia eeanna. I?e yoii
eiino?oeoi? e aeano?oeoi? aucuaathony aeey eaaeaeiai yeaiaioa
ianneaa e ia aeieaeiu eiaoue ia?aiao?ia. I?e auiieiaiee iia?aoi?a
delete aeey nnueee ia iannea iauaeoia iaiaoiaeeii oaeaea oeacuaaoue
aai ?acia?iinoue. Eiino?oeoi? aeey ianneaa iauaeoia aeieaeai auoue aac
ia?aiao?ia.

//——————————————————–#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); // Eiino?oeoi? n ia?aiao?aie

// (aiciiaeii oiie/aiea)

dat(char *); // Eiino?oeoi? n iaeiei ia?aiao?ii

dat(); // Eiino?oeoi? aac ia?aiao?ia

~dat(); // Aeano?oeoi?

};

//——- Eiino?oeoi? n ia?aiao?ii – oaenoiaay no?iea
———dat::dat(char *s)

{

int i;

char ss[80];

strcpy(ss,s);

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

if (ss[i]==’-‘) ss[i]=’,’; // Caiaia ‘-‘ ia ‘,’

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

}

// Eiino?oeoi? n o?aiy ia?aiao?aie (ii oiie/aieth 0 – oaeouay aeaoa)

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

{

struct date x;

getdate(&x); // Noaiaea?oiay ooieoeey iieo/aiey

// oaeouae aeaou

// I?iaa?ea ia cia/aiea ii oiie/aieth

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

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

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

}

//—— Eiino?oeoi? aac ia?aiao?ia ————————–dat::dat()

{

struct date x;

getdate(&x); // Noaiaea?oiay ooieoeey iieo/aiey

// oaeouae aeaou

year = x.da_year ;

month= x.da_month;

day = x.da_day ;

}

//—— Aeano?oeoi?
——————————————dat::~dat()

{

printf(“Aeaoa ==> %2d-%2d-%4d\n”,day,month,year);

}

//——————————————————dat
a(“12-12-1990”); // Aiaoiyy ia?aiaiiay – eiino?oeoi?

// aucuaaaony ia?aae main()

dat b[10]; // Iannea iauaeoia – eiino?oeoi? aac

// ia?aiao?ia aucuaaaony ia?aae main()

void xxx(dat &p)

{

dat c(12,12); // Aucuaaaony Eiino?oeoi? dat(int,int,int)

// aeey aaoiiaoe/aneiai iauaeoa

dat d = p; // Eiino?oeoi? aeey aaoiiaoe/aneiai iauaeoa ia

… // aucuaaaony, o.e. iauaeo eieoeeaeece?oaony

… // eiie?iaaieai

} // I?e auoiaea ec ooieoeee aucuaathony aeano?oeoi?u

// aeey iauaeoia c e d

void main()

{

int i,n;

scanf(“%d”,&n);

dat *p = new dat[n]; // Nicaeaiea ianneaa aeeiaie/aneeo iauaeoia
// eiino?oeoi? aac ia?aiao?ia yaii aucuaaaony

for (i=0; i<10; i++) // n ?acxxx(b[i]); // I?e auciaa iayaii ia?aaeaaony nnueea ia b[i]for (i=0; i< n; i++) //xxx(p[i]) // I?e auciaa iayaii ia?aaeaaony nnueea ia p[i]delete[n] p; // Oie/oiaeaiea ianneaa aeeiaie/aneeo iauaeoia // aeano?oeoi? yaii aucuaaaony n ?ac} // Aeano?oeo?u aeey a e b[10] aucuaathony iinea// auoiaea ec main()}3.3 Ia?aie/aiea aeinooia e iauaeoai eeanna. Ae?oaeanoaaiiinoue.----------------------------------------------------------Auoa auei aeaii oi?iaeueiia ii?aaeaeaiea eeanna. Niaea?aeaoaeueiay aai noi?iia ninoieo a oii, /oi eeann aunooiaao a Ne++ eae ii?aaeaeaiiue i?ia?aiienoii oei aeaiiuo. Oaeie oei aeaiiuo aeeth/aao a naay:- iienaiea no?oeoo?u iauaeoia eeanna. Iauaeo eeanna i?aaenoaaeyao niaie niaieoiiinoue yeaiaioia (no?oeoo?o), eaaeaeue ec eioi?uo yaeyaony ia?aiaiiie iaeioi?iai oeia eee iauaeoii ?aiaa ii?aaeaeaiiiai eeanna;- iienaiea iiiaeanoaa aeiionoeiuo iia?aoeee iaae iauaeoii eeanna, eioi?ua i?aaenoaaeaiu yeaiaioaie-ooieoeeyie;- iienaiea i?ioeaaeo? nicaeaiey e oie/oiaeaiey iauaeoa eeanna (eiino?oeoi? e aeano?oeoi?).I?e yoii aioo?aiiyy no?oeoo?a iauaeoia (iienaiiay a "i?eaaoiie" /anoe eeanna) aeinooiia oieueei yeaiaioai-ooieoeeyi. Iaiai?io, "ioaee/iay" /anoue ii?aaeaeaiey eeanna - yoi iienaiea oie /anoe iauaeoa, e eioi?ie aiciiaeai aeinooi ec ethaiai ianoa i?ia?aiiu, anee aeinooiai nai iauaeo. Aeaiiia ia?aie/aiea iicaieyao eneeth/eoue iaei??aeoiia eniieueciaaiea iauaeoia eeanna, eae neo/aeiia, oae e oiuoeaiiia. Aiciiaeiua ioeaee, naycaiiua n iai?aaeeueiui "iiaaaeaieai" iauaeoia eeanna, eieaeecothony oaeei ia?acii a ii?aaeaeaiee naiiai eeanna, a ia a eniieueciaaiee aai iauaeoia.Eiiaaea o?aaothony eneeth/aiey ec yoiai i?aaeea, eiaaea e "i?eaaoiie" /anoe iauaeoa eeanna o?aaoaony aeinooi ec iaeioi?ie ioaeaeueiie ooieoeee, eeai ec anao yeaiaioia-ooieoeee ae?oaiai eeanna, eeai ec ia?aii?aaeaeyaiie a ae?oaii eeanna iia?aoeee. Oiaaea a ii?aaeaeaiee eeanna, e iauaeoai eioi?iai ?ac?aoaaony oaeie aeinooi, aeieaeii auoue iauyaeaiea ooieoeee eee ae?oaiai eeanna "ae?oaeanoaaiiui". Yoi niaeanoaony n oai i?eioeeiii, /oi nai eeann ii?aaeaeyao i?aaa aeinooia e naiei iauaeoai "ni noi?iiu".Iauyaeaiea ae?oaeanoaaiiie ooieoeee i?aaenoaeyao niaie i?ioioei ooieoeee, iauyaeaiea ia?aii?aaeaeyaiie iia?aoeee eee eiy eeanna, eioi?ui ?ac?aoaaony aeinooi, n eeth/aaui neiaii friend aia?aaee. Iauay noaia iauyaeaiey oaeiaa:class A{int x; // I?eaaoiay /anoue eeanna...friend class B; // Ooieoeee eeanna B ae?oaeanoaaiiu eeanno A// (eiatho aeinooi e i?eaaoiie /anoe A)friend void C::fun(A&);// Yeaiaio-ooieoeey fun eeanna C eiaao// aeinooi e i?eaaoiie /anoe Afriend void xxx(A&,int);// Ooieoeey xxx ae?oaeanoaaiia eeanno Afriend void C::operator+(A&);// Ia?aii?aaeaeyaiay a eeanna C iia?aoeey// + ae?oaeanoaaiia

// eeanno A

class B

{

public: int fun1(A&);// Iaiaoiaeei aeinooi e i?eaaoiie /anoe A

void fun2(A&);// ———————————– }

class C

{

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

}

E n?aaenoaai eiio?iey aeinooia ioiinyony oaeaea iauyaeaiey
yeaiaioia-ooieoeee iinoiyiiuie (const). A yoii neo/aa yeaiaioooieoeey

ia eiaao i?aaa eciaiyoue cia/aiea oaeouaai iauaeoa, n eioi?ui iia

aucuaaaony. Caaieiaie ooieoeee i?e yoii eiaao aeae

void dat::put() const

{

}

3.4 Noaoe/aneea yeaiaiou eeanna

——————————

Eiiaaea o?aaoaony ii?aaeaeeoue aeaiiua, eioi?ua ioiinyony ei

anai iauaeoai eeanna. Yoi o?aaoaony, anee iauaeou eeanna ?acaeaeytho
iaeioi?ue iauee ?ano?n, naycaiu a iauee nienie e o.ae.. N yoie

oeaeueth a ii?aaeaeaiee eeanna iiaoo auoue aaaaeaiu noaoe/aneea yeaiaiou
– ia?aiaiiua. Oaeie yeaiaio nai a iauaeou eeanna ia aoiaeeo, caoi
i?e ia?auaiee e iaio oi?ie?oaony ia?auaiea e aiaoiae ia?aiaiiie n
eiaiai

::

niioaaonoaothuaai oeia. Aeinooiiinoue aa ii?aaeaeyaony noaiaea?oiui
ia?acii a caaeneiinoe io ?aciauaiey a i?eaaoiie eee iauae /anoe
eeanna. Naia ia?aiaiiay aeieaeia auoue yaii ii?aaeaeaia a i?ia?aiia e
eieoeeaeece?iaaia.

I?eia?: iauaeou eeanna naycaiu a iaeiinayciue nienie

————————————————–class list

{

static list *fst; // Nnueea ia ia?aue yeaiaio

static list *lst; // Nnueea ia iineaaeiee yeaiaio

list *next; // Nnueea ia neaaeothuee yeaiaio

…. …..

public:

void insfst(); // Anoaaeoue a ia/aei nienea

void inslst(); // Anoaaeoue a eiiaoe nienea

void show(); // I?iniio? anao iauaeoia

void extract(); // Eneeth/oue ec nienea

list(); // Eiino?oeoi?

~list(); // Aeano?oeoi?

}

list list::fst=NULL; // Ii?aaeaeaiea noaoe/aneeo yeaiaioia

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; // Iiene oaeouaai e i?aaeuaeouaai

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

pred=p,p=p->next)

if (p=this) break; // Anee iaeaeai – auoiae

if (p !=NULL)

{ // Iaeaeai – eneeth/aiea ec nienea

if (pred==NULL)

fst = next;

else

pred->next=next;

}

}

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

{

list *p;

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

{ …auaiae eioi?iaoeee ia iauaeoa… }

}

//—— I?e nicaeaiee iauaeoa ii iiiauaaony a nienie
———–list::list()

{

insfst();

}

//—— I?e oie/oiaeaiee iauaeoa ii eneeth/aaony ec nienea
——list::~list()

{

extract();

}

I?eia?ii eniieueciaaiey aioo?aiiaai nienea iauaeoia yaeyaony

nenoaia anieuaathueo ieii. I?e auiieiaiee iia?aoeee iaae iaeiei ec

ieii /anoi o?aaoaony i?iecaanoe iaeioi?ua aeaenoaey n ae?oaeie ieiaie,
oi anoue a ethaie iiiaio i?ia?aiia aeieaeai auoue ecaanoai

nienie nicaeaiiuo iauaeoia – ieii. Iineaaeiaaoaeueiinoue iauaeoia a

nienea iiaeao io?aaeaoue iineaaeiaaoaeueiinoue ioia?aaeaiey ieii ia
ye?aia. Oiaaea i?e auiieiaiee iia?aoeee “anieuoey” ieia iaiaoiaeeii

eciaieoue iineiaeaiea niioaaonoaothuaai iauaeoa a nienea. Anoanoaaiii,
/oi eiino?oeoi? e aeano?oeoi? iauaeoa aeeth/atho aai a nienie e

eneeth/atho.

Noaoe/aneeie iiaoo auoue iauyaeaiu oaeaea e yeaiaiou-ooieoeee.

Eo “noaoe/iinoue” ii?aaeaeyaony oai, /oi aucia eo ia naycai n
eiie?aaoiui iauaeoii e iiaeao auoue auiieiai ii iieiiio eiaie.
Niioaaonoaaiii a ieo ia eniieuecothony iayaiay nnueea this. Iie
aaiaeyony, eae i?aaeei, aeey auiieiaiey aeaenoaee, ioiinyueoniy ei anai
iauaeoai eeanna. Aeey i?aaeuaeouaai i?eia?a

class list

{ …

static void show(); // Noaoe/aneay ooieoeey i?iniio?a

} // anaai nienea iauaeoia

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

{

list *p;

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

{ …auaiae eioi?iaoeee ia iauaeoa… }

}

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

{ …

list::show(); // Aucia ooieoeee ii iieiiio eiaie

}

Eaeoeey 4. Ia?aii?aaeaeaiea iia?aoi?ia.

————————————

Iaiiiiei, /oi iiae eeannii iiieiaaony ii?aaeaeyaiue i?ia?aiienoii
oei aeaiiuo, eniieuecoaiue ia?aaia ni noaiaea?oiuie aaciauie oeiaie. N
oi/ee c?aiey “?aaiii?aaey” aiiaue aaiaeeiiai oeia aeaiiuo aeaeaoaeueii
eiaoue aiciiaeiinoue ?anoe?aiey (ia?aii?aaeaeaiey) iia?aoeee ycuea, a
eioi?uo iaeei eee ianeieueei iia?aiaeia iiaoo auoue iauaeoaie yoiai
eeanna Yoi aeinoeaaaony aaaaeaieai yeaiaioa-ooieoeee niaoeeaeueiiai
aeaea, ia?auaiea e eioi?ie eiiieeyoi? oi?ie?oao i?e o?aineyoeee oaeie
iia?aoeee. Anoanoaaiii, /oi oaeay ooieoeey aeieaeia eiaoue ?acoeueoao
(cia/aiea eee iayaiay nnueea), anee i?aaeiieaaaaony eniieueciaaiea yoie
iia?aoeee aioo?e ae?oaiai au?aaeaiey.

Ia?aii?aaeaeaiea iia?aoeee inouanoaeyaony a ?aieao noaiaea?oiiai
neioaenena ycuea Ne, oi anoue iaicia/aiea iia?aoeee e eiee/anoai
iia?aiaeia inoaaony i?aaeiei.

Iaiaoiaeeii ioiaoeoue oaeaea e oio oaeo, /oi aeey eaaeaeie
eiiaeiaoeee oeiia iia?aiaeia ia?aii?aaeaeyaiie iia?aoeee iaiaoiaeeii
aaanoe ioaeaeueioth ooieoeeth, oi anoue o?aineyoi? ia iiaeao
i?iecaiaeeoue ia?anoaiiaeo iia?aiaeia ianoaie, aeaaea anee aaciaay
iia?aoeey aeiioneaao yoi. Iai?eia?, i?e ia?aii?aaeaeaiee iia?aoeee
neiaeaiey iauaeoa

eeanna dat n oeaeui iaiaoiaeeii aeaa ooieoeee dat+int e int+dat.

Aeey ia?aii?aaeaeaiey iia?aoeee eniieuecoaony iniaay oi?ia
yeaiaioa-ooieoeee n caaieiaeii oaeiai aeaea:

operator( )

I?e yoii eiy ooieoeee ninoieo ec eeth/aaiai neiaa operator e

neiaiea aeaiiie iia?aoeee a neioaenena ycuea Ne.

Nienie oi?iaeueiuo ia?aiao?ia ooieoeee yaeyaony nieneii
iia?aiaeia (eiee/anoai, oeiu, niiniau ia?aaea/e) iia?aoeee.

?acoeueoao ooieoeee (oei, niinia ia?aaea/e) yaeyaony ?acoeueoaoii
ia?aii?aaeaeyaiie iia?aoeee. Niinia ia?aaea/e e oei oeacuaatho ia

aiciiaeiinoe eniieueciaaiey ?acoeueoaoa a ae?oaeo au?aaeaieyo.

Eiaaony aeaa niiniaa iienaiey ooieoeee, niioaaonoaothuae
ia?aii?aaeaeyaiie iia?aoeee:

– anee ooieoeey caaeaaony eae iau/iay yeaiaio-ooieoeey eeanna,

oi ia?aui a?aoiaioii niioaaonoaothuae iia?aoeee yaeyaony iauaeo,

nnueea ia eioi?ue ia?aaeaaony iayaiui ia?aiao?ii this;

– anee ia?aui a?aoiaioii ia?aii?aaeaeyaiie iia?aoeee ia yaeyaony
iauaeo iaeioi?iai eeanna, eeai ooieoeey iieo/aao ia aoiae ia

nnueeo ia iauaeo, a nai iauaeo, oiaaea niioaaonoaothuay
yeaiaioooieoeey aeieaeia auoue ii?aaeaeaia eae ae?oaeanoaaiiay n iieiui
nieneii

a?aoiaioia. Anoanoaaiii, /oi iieiia eiy ae?oaeanoaaiiie
ooieoeeeiia?aoi?a ia niaea?aeeo i?e yoii eiaie eeanna.

A ea/anoaa i?eia?a ?anniio?ei aeiii?aaeaeaiea noaiaea?oiuo
iia?aoeee iaae aeaoaie.

#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(); // Yeaiaio-ooieoeey au/eneaiey

// neaaeothuaai aeey

dat operator++(); // Iia?aoeey ++

dat operator+(int); // Iia?aoeey “aeaoa + oeaeia”

// n iayaiui iia?aiaeii /a?ac this

friend dat operator+(dat,int) // Iia?aoeee n yaiie ia?aaea/ae

friend dat operator+(int, dat) // anao ia?aiao?ia ii cia/aieth

dat(); // Eiino?oeoi?u

dat(int,int,int); // (ni. i?aaeuaeouea i?eia?u)

dat(char *); //

~dat(); // Aeano?oeoi?

}; // (ni. i?aaeuaeouea i?eia?u)

//—— Ooieoeey au/eneaiey neaaeothuaai aeiy ——————–//
Eniieuecoaony nnueea ia oaeouee iauaeo this,

// eioi?ue eciaiyaoniy a i?ioeanna iia?aoeee

//——————————————————-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++;

}

}

}

//—— Iia?aoeey eie?aiaioa aeaou —————————–//1.
Oi?ia yeaiaioa-ooeioeee n iayaiui iia?aiaeii ii nnueea this

//2. Aica?auaao eiieth aoiaeiiai iauaeoa (iia?aiaea) aei oaaee/aiey

//3. Niioaaonoaoao iia?aoeee dat++ (oaaee/aiea iinea eniieueciaaiey)

//4. Caia/aiea: aeey oia?iuo iia?aoeee oeia — eee ++ eniieueciaaiea

// eo aei eee iinea iia?aiaea ia eiaao cia/aiey (aucuaaaony iaeia

// e oa aea ooieoeey).

//——————————————————-

dat dat::operator++()

{

// Nicaeaaony a?aiaiiue iauaeo

dat x = *this; // A iaai eiie?oaony cia/aiea oaeouaai iauaeoa

dat::next(); // Oaaee/eaaaony cia/aiea oaeouaai iauaeoa

return(x); // Aica?auaaony a?aiaiiue iauaeo

}

//—— Iia?aoeey “aeaoa + oeaeia” ——————————//1.
Yeaiaio-ooieoeey n iayaiui ia?aui a?aoiaioii ii nnueea this

//2. Aoiaeiie iauaeo ia iaiyaony, ?acoeueoao aica?auaaony eiieae

// aioo?aiiaai aaoiiaoe/aneiai iauaeoa x

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

{

dat x;

x = *this; // Eiie?iaaiea oaeouaai iauaeoa a x

while (n– !=0) x.next(); // Aucia ooieoeee next aeey iauaeoa x

return(x); // Aica?ao eiiee iauaeoa x

}

//—— Iia?aoeey “aeaoa + oeaeia” ——————————//1.
Ae?oaeanoaaiiay yeaiaio-ooieoeey n iieiui nieneii a?aoiaioia

//2. Aeueoa?iaoeaiue aa?eaio i?aaeuaeouae ooieoeee

//3. Ia?aue iia?aiae eeanna dat – ia?aaeaaony ii cia/aieth,

// iiyoiio iiaeao iiaeeoeoee?iaaoueny aac eciaiaiey enoiaeiiai iauaeoa

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

{

while (n– !=0) p.next(p); // Aucia ooieoeee next aeey iauaeoa p

return(p); // Aica?ao eiiee iauaeoa x

}

//—— Iia?aoeey “oeaeia + aeaoa” —————————–//1.
Ae?oaeanoaaiiay yeaiaio-ooieoeey n iieiui nieneii a?aoiaioia

//2. Aoi?ie iia?aiae eeanna dat – ia?aaeaaony ii cia/aieth,

//iiyoiio iiaeao iiaeeoeoee?iaaoueny aac eciaiaiey enoiaeiiai iauaeoa

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

{

while (n– !=0) p.next(); // Aucia ooieoeee next aeey iauaeoa p

return(p); // Aica?ao eiiee iauaeoa 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;}Iiiaea ec ia?aii?aaeaeyaiuo iia?aoeee i?aaenoaaeytho niaie aiaeia iaeioi?iai eiiaaea?a, ii eioi?iio i?iaeaeaaaony iauaeo a i?ioeanna ia?aaioee. Oaeea iia?aoeee a ea/anoaa iaeiiai ec iia?aiaeia e a ea/anoaa ?acoeueoaoa eiatho iauaeo iaeiiai eeanna. Auoai?eaaaeaiiua iia?aoeee n aeaoaie eae ?ac yaeythony oaeiauie. Ii i?aia?aciaaiea iauaeoa iiaeao i?iecaiaeeoueny a oaeeo iia?aoeeyo aeaoiy ?aciuie niiniaaie. I?e yoii aicieeatho iaeioi?ua iniaaiiinoe a auciaa eiino?oeoi?ia e aeano?oeoi?ia, ia eioi?uo iaiaoiaeeii inoaiiaeoueny.A ?anniio?aiiii auoa i?eia?a iaeiei ec ia?aiao?ia yeaiaioaooieoeee - iauaeo eeanna dat ia?aaeaaony ii cia/aieth, aiaeiae/ii ia?aaeaaony e ?acoeueoao iia?aoeee. Yoe aeaa iauaeoa ii?aaeaeaiu iayaii e nicaeathony iooai eiie?iaaiey ec ae?oaeo iauaeoia. Iiyoiio eiino?oeoi?u aeey ieo ia aucuaathony. Iaeiaei i?e oie/oiaeaiee yoeo iauaeoia i?iecaiaeeony aucia aeano?oeoi?ia. Oaeei ia?acii, i?e i?iaeoe?iaaiee eeanna e ia?aaea/a aai iauaeoia ii cia/aieth iaiaoiaeeii o/eouaaoue yoe "eeoiea" iauaeou e aeano?oeoi?u. Ia?a/eneei aiciiaeiua neo/ae aicieeiiaaiey e oie/oiaeaiey iauaeoia a ooieoeee:- iauaeo -oi?iaeueiue ia?aiao? - nicaeaaony a noaea, aai cia/aiea i?aaenoaaeyao niaie eiieth niioaaonoaothuaai oaeoe/aneiai ia?aiao?a, niioaaonoaaiii, eiino?oeoi? aeey iaai ia aucuaaaony;- aaoiiaoe/aneee iauaeo x a oaea ooieoeee nicaeaaony a noaea ia?aae auciaii ooieoeee. Anee ii eieoeeaeece?oaony eiieae ae?oaiai iauaeoa (iai?eia?, oaeouaai), oi eiino?oeoi? aeey iaai ia aucuaaaony:dat x = *this;A i?ioeaiii neo/aa aucuaaaony eiino?oeoi? n niioaaonoaothuei iaai?ii ia?aiao?ia:dat x;dat x("13-JUL-1990");- a ethaii neo/aa ia?aae auoiaeii ec ooieoeee aucuaaaony aeano?oeoi? aeey aaoiiaoe/aneiai iauaeoa;- anee ooieoeey eiaao oi?iaeueiue ia?aiao? - iauaeo eeanna, oiaeey iaai aucuaaaony aeano?oeoi? iinea auoiaea ec ooieoeee (anee yoiia?aii?aaeaeyaiue iia?aoi?, oi iinea au/eneaiey au?aaeaiey);- anee ooieoeey aica?aoaao iauaeo ii cia/aieth (a ia ii yaiieeee iayaiie nnueea), oi ooieoeey iieo/aao aeiiieieoaeueiue iayaiueia?aiao? - nnueeo ia ?acoeueoao (ni. "Iayaiua nnueee"), a ii iia?aoi?o return i?iecaiaeeony eiie?iaaiea aica?auaaiiai iauaeoa aiauaeo-?acoeueoao ii caaeaiiie nnueea. Iaeiaei i?e auciaa oaeieooieoeee o?aineyoi? iiaeao ii-?aciiio oi?ie?iaaoue yoo nnueeo:- anee i?iecaiaeeony i?yiia i?enaaeaaiea ?acoeueoaoa ooieoeee,oi ia?aaeaaony nnueea ia iauaeo a eaaie /anoe iia?aoeee i?enaaeaaiey;- a inoaeueiuo neo/ayo ia eaaeaeue aucia ooieoeee (ii neioaenenoi?ia?aiiu) aucuaathuae ooieoeeae oi?ie?oaony aeiiieieoaeueiue aaoiiaoe/aneee iauaeo. Eiino?oeoi? aeey iaai ia aucuaaaony, oae eae iioi?ie?oaony eiie?iaaieai ?acoeueoaoa ooieoeee, a aeano?oeoi? aucuaaaony i?e auoiaea ec aucuaathuae ooieoeee, eae aeey iau/iiai aaoiiaoe/aneiai iauaeoa.Iaeaieaa yooaeoeaiui niiniaii ?aaiou n iauaeoii yaeyaony ia?aaea/a aai ia aoiae (oi?iaeueiue ia?aiao?) e auaea/a ia auoiae a ea/anoaa nnueee (i?aeiouanoaaiii iayaiie). Iaeiaei a yoii niiniaaana eciaiaiey, i?iecaiaeeiua i?e auiieiaiee iia?aoeee, i?ienoiaeyo aenoiaeiii iauaeoa. Yoi ia niaeanoaony n eioa?i?aoaoeeae aieueoeinoaaaeia?iuo iia?aoeee, aaea ?acoeueoao yaeyaony ioaeaeueiui yeaiaioiiaeaiiuo (iauaeoia), a iia?aiaeu ia iaiythony. A ea/anoaa i?eia?a?anniio?ei aa?eaio iia?aoeee neiaeaiey "aeaoa + oeaeia", a eioi?iienoiaeiay aeaoa oaaee/eaaaony ia caaeaiiia cia/aiea e iaeiia?aiaiii yaeyaony ?acoeueoaoii iia?aoeee.class dat{int day,month,year;public:void next(); // Yeaiaio-ooieoeey au/eneaiey// neaaeothuaai aeeydat& operator+(int); // Iia?aoeey "aeaoa + oeaeia"// n iayaiui iia?aiaeii /a?ac this//------ Iia?aoeey "aeaoa + oeaeia" ------------------------------//1. Ae?oaeanoaaiiay yeaiaio-ooieoeey n iieiui nieneii a?aoiaioia//2. Aeueoa?iaoeaiue aa?eaio i?aaeuaeouae ooieoeee//3. Ia?aue iia?aiae eeanna dat - iayaiay nnueea ia oaeoe/aneee// ia?aiao?, cia/aiea iaiyaony i?e auiieiaiee iia?aoeee//4. Oaei ooieoeee iaiin?aaenoaaiii a ii?aaeaeaiee eeanna.//-------------------------------------------------------friend dat& operator+(dat& p,int n){while (n-- !=0) p.next();// Aucia ooieoeee next aeey iauaeoa p// ii iayaiie nnueea ia iaai.return(p); // Aica?ao iayaiie nnueee iaa p}// Iia?aoeey "oeaeia + aeaoa" ------------------------------------//1. Ae?oaeanoaaiiay yeaiaio-ooieoeey n iieiui nieneii a?aoiaioia//2. Aoi?ie iia?aiae eeanna dat - iayaiay nnueea ia oaeoe/aneee// ia?aiao?, cia/aiea iaiyaony i?e auiieiaiee iia?aoeee.//3. Oaei ooieoeee iaiin?aaenoaaiii a ii?aaeaeaiee eeanna.//-------------------------------------------------------friend dat& operator+(int n, dat& p){while (n-- !=0) p.next(); // Aucia ooieoeee next aeey iauaeoa p// ii iayaiie nnueea ia iaai.return(p); // Aica?ao iayaiie nnueee ia p}//-------------------------------------------------------dat(); // Eiino?oeoi?udat(int,int,int); // (ni. i?aaeuaeouea i?eia?u)dat(char *); //~dat(); // Aeano?oeoi?}; // (ni. i?aaeuaeouea i?eia?u)//------ Iia?aoeey "aeaoa + oeaeia" -------------------------------//1. Yeaiaio-ooieoeey n iayaiui ia?aui a?aoiaioii ii nnueea this//2. Iaiyaony cia/aiea oaeouaai iauaeoa//3. ?acoeueoao - iayaiay nnueea ia oaeouee iauaeo//--------------------------------------------------------dat& dat::operator+(int n){while (n-- !=0)next(); // Aucia ooieoeee next n oaeouei iauaeoii thisreturn(*this); // Aica?ao iayaiie nnueee ia iauaeo (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;}Eaeoeey 5. Iniaaiiinoe ia?aii?aaeaeaiey ?acee/iuo iia?aoeee-------------------------------------------------------Iaeiie ec aaaeiuo aiciiaeiinoae eniieueciaaiey ia?aiaiiuo aaciauo oeiia a iia?aoeeyo yaeyaony eo i?aia?aciaaiea e ae?oaei oeiai,eioi?ia iiaeao i?iecaiaeeoueny iayaii (a aeia?iuo a?eoiaoe/aneeoiia?aoeeyo e i?e i?enaaeaaiee), eeai n eniieueciaaieai iia?aoeee yaiiai i?aia?aciaaiey oeia. Anee i?aneaaeiaaoue oeaeueth aeinoeaeaiea i?eaeiaaaeaiee iiauo eeannia anae iieiiou naienoa aaciauo oeiia aeaiiuo, oi aiaeiae/iua i?aia?aciaaiey iaiaoiaeeii aaanoe e aeey yoeoeeannia. Aeaeaa ?anniio?ei aeaa aiciiaeiuo niiniaa oaeiai i?aia?aciaaiey - noaiaea?oiue, i?aia?aciaaiea iauaeoa eeanna e ia?aiaiiieaaciaiai oeia aeaiiuo, e ianoaiaea?oiue - i?aia?aciaaiea ia?aiaiiieaaciaiai oeia aeaiiuo eee iauaeoa eeanna e iauaeoo ae?oaiai eeanna.5.1 I?aia?aciaaiea e aaciaiio oeio aeaiiuo----------------------------------------A ea/anoaa i?eia?a ?anniio?ei iayaiia i?aia?aciaaiea iauaeoa eeanna dat e aaciaui oeiai aeaiiuo int e long. Nouiinoue aai caeeth/aaony a au/eneaiee iieiiai eiee/anoaa aeiae a aeaoa, caaeaiiie aoiaeiui iauaeoii (long) e eiee/anoaa aeiae a oaeouai aiaeo a yoie aea aeaoa (int). Aeey caaeaiey yoeo iia?aoeee iaiaoiaeeii ia?aii?aaeaeeoue a eeanna dat iaeiieiaiiua iia?aoeee int e long. Ia?aii?aaeaeyaiua iia?aoeee caaeathony niioaaonoaothueie yeaiaioaie-ooieoeeyie aac ia?aiao?ia, nnueea ia oaeouee iauaeo aoiaeiiai eeanna ia?aaeaaony /a?ac iayaiue ia?aiao? this. Oei ?acoeueoaoa niaiaaeaao n aaciaui oeiii, e eioi?iio inouanoaeyaony i?eaaaeaiea e iiyoiio ia oeacuaaaony.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(); // I?aia?aciaaiea dat a intoperator long(); // I?aia?aciaaiea dat a longlong operator -(dat &p); // Iia?aoeey dat-dat au/eneyao// ?aciinoue aeao a aeiyodat(); // Eiino?oeoi?udat(int,int,int); //dat(char *); //~dat(); // Aeano?oeoi?};//------ I?aia?aciaaiea dat a int -----------------------------// Eniieuecoaony nnueea ia oaeouee iauaeo this//------------------------------------------------------- dat::operator int(){int r; // Oaeouee ?acoeueoaoint i; // N/ao/ee ianyoeaafor (r=0, i=1; i2) && (year%4==0)) r++; // Aenieiniue aiae

r += day; // Aeiae a oaeouai ianyoea

return(r);

}

//—— I?aia?aciaaiea dat a long —————————//
Eniieuecoaony nnueea ia oaeouee iauaeo this

//——————————————————

dat::opertor long()

{

long r; // Oaeouee ?acoeueoao

r = 365 * (year-1) // Aeiae a i?aaeuaeoueo iieiuo aiaeao

r += year / 4; // Aenieiniua aiaea

r += (int)(*this); // Aeiae a oaeouai aiaeo – i?aaeuaeouay

// iia?aoeey (yaiia i?aia?aciaaiea

return(r); // dat a int

}

//——– Iia?aoeey au/eneaiey ?acieoeu aeaoo aeao —————//
Ia?aue iia?aiae ii nnueea ia oaeouee iauaeo this

// Aoi?ie iia?aiae ii iayaiie nnueea p

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

{

return((long)(*this) – (long)p); // I?aia?aciaaoue iaa iauaeoa

// e oeio long e au/ene. ?aciinoue

}

void main()

{

dat a(“12-05-1990”); // Aeaoa, caaeaiiay oaenoiaie no?ieie

dat b; // Oaeouay aeaoa

int c;

long d;

// ssaiia i?aia?aciaaiea e long

printf(“N 12-05-1990 i?ioei %4ld aeiae\n”,(long)b-(long)a);

// ssaiia i?aia?aciaaiea e int

printf(“A yoii aiaeo i?ioei %3d aeiae\n”,(int)b);

// Iayaiia i?aia?aciaaiea i?e i?enaaeaaiee

c = b;

d = b – a; // Iia?aoeey dat-dat

printf(“N 12-05-1990 i?ioei %4ld aeiae\n”,d);

printf(“A yoii aiaeo i?ioei %3d aeiae\n”,c);

}

5.2 I?aia?aciaaiea ia?aiaiiie e iauaeoo eeanna

———————————————

Aeaiiue niinia ia yaeyaony noaiaea?oiui e o?aaoao i?iaa?ee
?aaioiniiniaiinoe a eniieuecoaiii eiiieeyoi?a. Ii iniiaai ia oii

oaeoa, /oi i?e eiiieeyoeee yaiiai eee iayaiiai i?aia?aciaaiey iauaeoa
eeanna e aaciaiio oeio aeaiiuo “xxx” aucuaaaony ia?aii?aaeaeyaiay
iia?aoeey “operator xxx()”. Niioaaonoaaiii, i?e yaiii eee iayaiii
i?aia?aciaaiee e eeanno “zzz” aeieaeia aucuaaoueny ia?aii?aaeaeyaiay
iia?aoeey “operator zzz”. Eiae/ii, /oi oaeay iia?aoeey aeieaeia auoue
ii?aaeaeaia a eeanna “zzz”. Ii oiaaea eiy niioaaonoaothuae
yeaiaioa-ooieoeee aoaeao “zzz::zzz”, /oi niioaaonoaoao eiino?oeoi?o.
Oaeei ia?acii, anee iaiaoiaeeii ii?aaeaeeoue yaiia eee iayaiia
i?aia?aciaaiea io aaciaiai oeia eee eeanna “xxx” e eeanno “zzz”,

oi a eeanna “zzz” iaiaoiaeeii ii?aaeaeeoue eiino?oeoi?

class zzz

{

int par_zzz;

—————– aoiaeiie oei (eeann)

zzz(xxx p); eee

zzz(xxx& p);

L——————– auoiaeiie oei (eeann)

};

void zzz::zzz(xxx &p)

{

par_zzz = … p.par_xxx …;

yeaiaio iauaeoa—– L——-yeaiaio iauaeoa

auoiaeiiai eeanna aoiaeiiai eeanna

}

class xxx

{

friend class zzz;

int par_xxx;

};

ni neaaeothueie naienoaaie:

– iauaeo eeanna “zzz”, eioi?ue yaeyaony auoiaeiui i?e
i?aia?aciaaiee oeiia aeinooiai eae a ethaii eiino?oeoi?a /a?ac nnueeo ia
oaeouee iauaeo this;

– yeaiaioai auoiaeiiai iauaeoa (iai?eia?, par_zzz) aeieaeiu
auoue i?enaiaiu cia/aiey n yaiui eee iayaiui eniieueciaaieai nnueee this

this->par_zzz = …

(*this).par_zzz = …

par_zzz = …

– iauaeo eee ia?aiaiiay oiai eeanna eee aaciaiai oeia, eioi?ua
yaeythony aoiaeiuie a i?aia?aciaaiee oeiia, aeinooiiu /a?ac
niioaaonoaothuee oi?iaeueiue ia?aiao?, eioi?ue iiaeao auoue eae
cia/aieai (eiieae iauaeoa eee ia?aiaiiie), oae e iayaiie nnueeie.

Cia/aiea ia?aiaiiie eee yeaiaioia aoiaeiiai iauaeoa iiaoo
eniieueciaaoueny eae a?aoiaiou i?e i?aia?aciaaiee oeiia;

– aeey aeinooia ec ooieoeee eeanna “zzz” e i?eaaoiie /anoe iauaeoa
eeanna “xxx” eeann “zzz” aeieaeai auoue iauyaeai ae?oaeanoaaiiui

a ii?aaeaeaiee eeanna “xxx”.

A ea/anoaa i?eia?a ?anniio?ei ia?aoiia i?aia?aciaaiea aaciaiai
oeia long e oeio dat – eiee/anoai aeiae io ia/aea eaoien/eneaiey
i?aia?acoaony e aeaoa. Caeanue aea ?anniio?ei ae?oaie eeann

– man, a eioi?ii iaeiei ec yeaiaioia i?eaaoiie /anoe yaeyaony aeaoa.

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); // I?aia?aciaaiea long a dat

dat(man&); // I?aia?aciaaiea man a dat

dat(); // Eiino?oeoi?u

dat(int,int,int); //

dat(char *); //

~dat(); // Aeano?oeoi?

};

class man

{

friend class dat; // Eeann dat ae?oaeanoaaiai

… // eeanno man

int d,m,y; // Yeaiaio “aeaoa” a iauaeoa

… // eeanna man

public:

man(dat&); // I?aia?aciaaiea dat a man

man(); // Eiino?oeoi?

~man(); // Aeano?oeoi?

};

//—— I?aia?aciaaiea man a dat
—————————-//Eniieuecoaony nnueea ia oaeouee iauaeo
this aeey auoiaeiiai eeanna,

//oi?iaeueiue ia?aiao? – iayaiay nnueea – aeey aoiaeiiai eeanna

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

{

day = p.d;

month = p.m;

year = p.y;

}

//—— I?aia?aciaaiea long a dat
—————————//Eniieuecoaony nnueea ia oaeouee iauaeo this
aeey auoiaeiiai eeanna

//oi?iaeueiue ia?aiao? oeia long ia?aaeaaony ii cia/aieth

//——————————————————-

void dat::dat(long p)

{

year = p / 365.25; // *enei eao n o/aoii aenieiniuo

p = p – (year-1)*365L – year/4; // Inoaoie aeiae a oaeouai aiaeo

year++; // Ia/aeueiue aiae – 0001

for (month=1; p > 0; month++) // Au/eoaiea aeiae ii ianyoeai

{

p -= days[month];

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

}

month–; // Ainnoaiiaeaiea iineaaeiaai

p += days[month]; // ianyoea

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

day = p + 1;

}

void main()

{

dat a(“12-05-1990”); // Aeaoa, caaeaiiay oaenoiaie no?ieie

dat b; // Oaeouay aeaoa

int c;

long d;

// ssaiia i?aia?aciaaiea e long

printf(“N 12-05-1990 i?ioei %4ld aeiae\n”, (long)b-(long)a);

// ssaiia i?aia?aciaaiea e int

printf(“A yoii aiaeo i?ioei %3d aeiae\n”, (int)b);

c = b; // Iayaiia i?aia?aciaaiea i?e i?enaaeaaiee

d = b – a; // Iia?aoeey dat-dat

printf(“N 12-05-1990 i?ioei %4ld aeiae\n”,d);

printf(“A yoii aiaeo i?ioei %3d aeiae\n”,c);

}

5.3 Ia?aii?aaeaeaiea iia?aoeee new e delete

—————————————-

Iia?aoeee nicaeaiey e oie/oiaeaiey iauaeoia a aeeiaie/aneie iaiyoe
iiaoo auoue ia?aii?aaeaeaiu neaaeothuei ia?acii:

void *operator new(size_t size);

void operator delete (void *);

aaea void * – nnueea ia iaeanoue iaiyoe, auaeaeyaioth iiae iauaeo,

size – ?acia? iauaeoa a aaeoao.

Ia?aii?aaeaeaiea yoeo iia?aoeee iicaieyao iaienaoue nianoaaiiia

?ani?aaeaeaiea iaiyoe aeey iauaeoia eeanna.

5.4 Ia?aii?aaeaeaiea iia?aoeee [], (), ->

————————————–

Ia?aii?aaeaeaiea () :

——————-class one

{

public:

typeout operator()(type1,type2);

};

Aucia:

type1 a; // Aucia iia?aoi?a niaiaaeaao n

type2 b; // neioaenenii auciaa ooieoeee

one obj; // n eiaiai aeaiiiai iauaeoa

… obj(a,b) … yeaeaaeaioii obj.operator()(a,b)

Ia?aii?aaeaeaiea -> :

——————class two

{

public: type Y;

};

class one

{

two operator->(); // Iia?aoeey aeieaeia aica?auaoue iauaeo

eee two* operator->(); // eee nnueeo ia iauaeo eeanna two,

}; // a eioi?ii ii?aaeaeai yeaiaio Y

Aucia:

one obj;

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

Ia?aii?aaeaeaiea [] : eniieuecoaony aeey iiaeaee?iaaiey ae?ooaeueiuo

——————– ianneaia yeaiaioia ii?aaeaeaiiiai oeia.

class text_page

{

char **page; // Iannea nnueie ia no?iee

public:

int operator[](char*); // Annioeeaoeaiue iiene eiaeaena

// ii no?iea

char* operator[](int); // Auaeaeaiea no?iee ii eiaeaeno

};

5.5 Ia?aii?aaeaeaiea iia?aoeee eiie?iaaiey iauaeoia

————————————————

Kae ecaanoii, ii?aaeaeaiea iauaeoa eeanna a aeaea

=

i?eaiaeeo e oiio, /oi iauaeo eieoeeaeece?oaony iooai iiaaeoiiai
eiie?iaaiey niaea?aeeiiai ae?oaiai iauaeoa aac auciaa eiino?oeoo?a. K
oaeei aea iauaeoai ioiinyony iauaeou – oi?iaeueiua ia?aiao?u ooieoeee,
eioi?ua eieoeeaeece?othony eiieyie oaeoe/aneeo ia?aiao?ia.

Enee ooieoeey aica?auaao iauaeo, oi iia?aoi? return oaeaea auiieiyao
eiie?iaaiea iauaeoa – iia?aiaea a iauaeo iacia/aiey.

Taeia eiie?iaaiea ia ei??aeoii a oii neo/aa, anee iauaeou
niaea?aeao nnueee ia ae?oaea iauaeou eee ia?aiaiiua a aeeiaie/aneie
iaiyoe. A yoii neo/aa iiaeii ainiueueciaaoueny niaoeeaeueiui
eiino?oeoi?ii eiie?iaaiey, ia?aiao?ii eio?iai yaeyaony iayaiay nnueea
ia iauaeo – enoi/iee, a this oeacuaaao ia iauaeo i?eaiiee. Aoaeo/e
ii?aaeaeaiiui, ii aucuaaaony ai anao auoaia?a/eneaiiuo neo/ayo
eiie?iaaiey iauaeoia iaeei a ae?oaie.

I?eia? ei??aeoiiai eiino?oeoi?a eiie?iaaiey aeey eeanna no?ie eiaao
aeae:

class string

{

char *s; // Nnueea ia no?ieo

int sz; // Aeeeia no?iee

public: string(string&);// Eiino?oeoi? eiie?iaaiey

}; // nicaeaao eiieth no?iee a aeeiaie/aneie

// iaiyoe aeey iauaeoa – i?eaiieea

string::string(string& right)

{

s = new char[right->sz];

strcpy(s,right->s);

}

Eaeoeey 6. I?iecaiaeiua eeannu

—————————

6.1 Aeiaeaiiua eeannu

——————-

Iiiyoea i?iecaiaeiiai eeanna aaiaeeo a nenoaio eeannia i?eioeei

ea?a?oee. Aeaenoaeoaeueii, anee ii?aaeaeai eeann iauaeoia n
aeinoaoi/ii iaueie naienoaaie oi iauaeo aeaiiiai eeanna aeaeaoaeueii
aeeth/aoue a ea/anoaa iaeiiai ec yeaiaioia a iauaeou ae?oaeo eeannia.
Nouanoaoao aeaa niiniaa oaeiai aeeth/aiey, eaaeaeue ec ieo eiaao

nianoaaiiua oeaee e iniaaiiinoe.

Ia?aue neo/ae i?aaenoaaeyao niaie iau/iue niinia iino?iaiey

ei?a?oe/aneie no?oeoo?u aeaiiuo, eiaaea iauaeo noa?iai eeanna yaeyaony
iaeiei ec yeaiaioia aeaiiuo “i?eaaoiie” /anoe iiaiai eeanna.

Ii eiaao nianoaaiiia eiy (eiaiiaai), ii eioi?iio e iaio iiaeii
ia?auaoueny eae e iauaeoo. A yeaiaioao-ooieoeeyo iiaiai eeanna iiaeii

eniieueciaaoue yeaiaiou-ooieoeee e iia?aoeee aeey iauaeoa noa?iai

eeanna. ?anniio?ei a ea/anoaa i?eia?a eeann man – eioi?iaoeey i

/aeiaaea, aeeth/athuay a naay aeaou ?iaeaeaiey e iinooieaiey ia ?aaioo.

class man

{

char name[20]; // Ae?oaea yeaiaiou eeanna

char *address;

dat dat1; // Aeaoa ?iaeaeaiey

dat dat2; // Aeaoa iinooieaiey ia ?aaioo

public:

void newadr(); // Yeaiaio-ooieoeey

man(char*); // Eiino?oeoi?

}

//—– Ooieoeey “Eciaieoue aae?an i?iaeeaaiey” —————-void
man::newadr()

{

int n;

char s[80]; // No?iea iiaiai aae?ana

if (address != NULL)

delete address; // Inaiaiaeeoue iaiyoue

printf(“Aaaaeeoa iiaue aae?an:”);

gets(s);

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

strcpy(address,s); // Caiieieoue iiea aae?ana

}

Ec aeaiiiai i?eia?a aeaeii, /oi eiaiiaaiiua iauaeou noa?iai
eeanna iiaeii eniieueciaaoue a yeaiaioao-ooieoeeyo iiaiai eeanna eae
iau/iua yeaiaiou, aucuaaoue ii?aaeaeaiiua aeey ieo yeaiaiou-ooieoeee
noa?iai eeanna e auiieiyoue ia?aii?aaeaeaiiua aeey ieo iia?aoeee.
Caiaoei, /oi i?e yoii yeaiaiou-ooieoeee iiaiai eeanna ia eiatho
aeinooia e i?eaaoiie /anoe iauaeoia aaciaiai eeanna, oi anoue
“niaea?aeeiia” aeiaeaiiuo iauaeoia aeey ieo cae?uoi.

Ii caeanue aicieeaao aii?in, eae eieoeeaeece?othony e
oie/oiaeathony iauaeou noa?iai eeanna i?e nicaeaiee eee oie/oiaeaiee
iauaeoa iiaiai eeanna, oi anoue eae acaeiiaeaenoaotho eo
eiino?oeoi?u e aeano?oeoi?u.

A neo/aa, anee eiino?oeoi? iauaeoa iiaiai eeanna caaeai iau/iui
ia?acii, oi ia?aae auciaii yoiai eiino?oeoi?a aoaeoo aucaaiu
eiino?oeoi?u aac ia?aiao?ia aeey aoiaeyueo a iaai iauaeoia noa?iai
eeanna. E iaiai?io, iinea auciaa aeano?oeoi?a aeey iauaeoa iiaiai
eeanna aoaeoo aucaaiu aeano?oeoi?u aeiaeaiiuo iauaeoia noa?iai
eeanna.

Iaeiaei i?e eiino?oe?iaaiee aeiaeaiiuo iauaeoia ei aeaeaoaeueii
ia?aaeaaaoue ia?aiao?u. Iiyoiio i?e iienaiee eiino?oeoi?a iauaeoa
iiaiai eeanna iiaeii a caaieiaea a yaiii aeaea oeacaoue oio aeae
eiino?oeoi?a iauaeoa noa?iai eeanna, eioi?ue o?aaoaony. E?iia oiai, aai
ia?aiao?u iiaoo caaenaoue io ia?aiao?ia auciaa eiino?oeoi?a iiaiai
eeanna:

class man

{

char name[20]; // Ae?oaea yeaiaiou eeanna

dat dat1; // Aeaoa ?iaeaeaiey

dat dat2; // Aeaoa iinooieaiey ia ?aaioo

public:

man(char *,char *,char *); // Eiino?oeoi?u

man(char *);

}

//—– Eiino?oeoi? eeanna man n iayaiui auciaii eiino?oeoi?ia

// aeey dat1 e dat2 aac ia?aiao?ia

//—————————————————–
man::man(char *p)

{

}

//—– Eiino?oeoi? eeanna man n yaiui auciaii eiino?oeoi?ia

// aeey dat1 e dat2 n ia?aiao?aie

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

{ ¦ ¦ ¦

// — Oaei eiino?oeoi?a — ¦ ¦ ¦

} ¦ ¦ ¦

Aucia eiino?oeoi?a aeey —————— ¦ ¦

aeiaeaiiiai iauaeoa dat1 ¦ ¦

A ea/anoaa ia?aiao?a ia?aaeaaony ————- ¦

no?iea – aoi?ie ia?aiao? auciaa ¦

eiino?oeoi?a aeey eeanna man Aucia eiino?oeoi?a aeey

aeiaeaiiiai iauaeoa dat2

void main —— No?iea eiino?oeoi?a man

{ ¦

man JOHN(“John”,”8-9-1958″,”15-1-1987″);

} ¦ L—— No?iea ia?aaeaaony

No?iea ia?aaeaaony eiino?oeoi?o iauaeoa

eiino?oeoi?o iauaeoa dat2 a iauaeoa man

dat1 a iauaeoa man

6.2 I?iecaiaeiua eeannu

———————

Ae?oaie neo/ae aeiaeaiiinoe eeannia iniiauaaaony ia iiieiaiee

eeanna eae niaieoiiinoe aeaiiuo e iia?aoeee iaae ieie. I?e yoii

i?eioeei aeiaeaiiinoe ?anniao?eaaaony eae nicaeaiea iiaiai
“i?iecaiaeiiai” eeanna, eioi?ue aeeth/aao a naay ana eee aieueooth
/anoue

naienoa noa?iai “aaciaiai” eeanna, eee “ianeaaeoao” eo: no?oeoo?a

iauaeoa noa?iai eeanna aeeth/aaony a iiaue iauaeo, a ana
yeaiaiou-ooieoeee noa?iai eeanna i?eiaieiu e iauaeoo iiaiai eeanna,
oi/iaa e aai noa?ie ninoaaeythuae.

Noa?ue eeann i?e yoii iacuaaaony aaciaui eeannii (AE), iiaue

– i?iecaiaeiui eeannii (IE).

Neioaenen ii?aaeaeaiey i?iecaiaeiiai eeanna eiaao aeae:

class : ,,…

{

ii?aaeaeaiea i?eaaoiie e ioaee/iie /anoe

i?iecaiaeiiai eeanna

}

Ia?a/eneei iniiaiua naienoaa aaciaiai e i?iecaiaeiiai
eeannia:

– iauaeo aaciaiai eeanna ii?aaeaeyaony a i?iecaiaeiii eeanna eae
iaeiaiiaaiiue. Yoi cia/eo, /oi ii ia iiaeao auoue eniieueciaai a yaiii
aeaea eae iau/iue eiaiiaaiiue iauaeo;

– yeaiaiou aeaiiuo aaciaiai eeanna aeeth/athony a iauaeo
i?iecaiaeiiai eeanna (eae i?aaeei, eiiieeyoi? ?aciauaao eo a ia/aea
iauaeoa i?iecaiaeiiai eeanna). Oaeiaei i?eaaoiay /anoue aaciaiai
eeanna cae?uoa aeey i?yiiai eniieueciaaiey a i?iecaiaeiii eeanna;

– yeaiaiou-ooieoeee aaciaiai eeanna “ianeaaeothony” a i?iecaiaeiii
eeanna, oi anoue aucia ooieoeee, ii?aaeaeaiiie a aaciaii eeanna, aeey
iauaeoa i?iecaiaeiiai eeanna aiciiaeai e iiieiaaony eae aucia aa aeey
aoiaeyuaai a iaai iauaeoa aaciaiai eeanna;

– a i?iecaiaeiii eeanna iiaeii ia?aii?aaeaeeoue ianeaaeoaioth
ooieoeeth, eioi?ay aoaeao aucuaaoueny aianoi ianeaaeoaiie. I?e yoii
aeey auiieiaiey niioaaonoaothueo aeaenoaee iaae iauaeoii aaciaiai
eeanna iia iiaeao aeeth/aoue yaiue aucia ia?aii?aaeaeyaiie ooieoeee.

I?eia? noaiu ii?aaeaeaiey i?iecaiaeiiai eeanna

class a

{

public:

void f() {}

void g() {}

}

class b : a ———————— aaciaue eeann

{

public: void f() ——————– “f” ia?aii?aaeaeyaony

{ …

a::f(); ————– yaiue aucia “f” aeey AE

} ————– “g” ianeaaeoaony ec AE

void h() {} ————– nianoaaiiay ooieoeey a IE

}

void main()

{

a A1;

b B1;

B1.f(); ————— aucia ia?aii?aaeaeaiiie b::f()

B1.g(); ————— aucia ianeaaeoaiie a::f()

}

Iiiyoea “ianeaaeiaaiey” i?aaeiieaaaao /oi i?e auciaa a
i?iecaiaeiii eeanna ooieoeee, ianeaaeoaiuo ec aaciaiai, o?aineyoi?
i?iecaiaeeo i?aia?aciaaiea nnueee this ia iauaeo i?iecaiaeiiai eeanna a

nnueeo ia aoiaeyuee a iaai iauaeo aaciaiai eeanna, o/eouaay ?aciauaiea
iineaaeiaai a iauaeoa i?iecaiaeiiai eeanna.

Acaeiiioiioaiea eiino?oeoi?ia e aeano?oeoi?ia aaciaiai e
i?iecaiaeiiai eeannia aiaeiae/iu auoa iienaiiui:

– anee eiino?oeoi? i?iecaiaeiiai eeanna ii?aaeaeai iau/iui ia?acii,
oi nia/aea aucuaaaony eiino?oeoi? aaciaiai eeanna aac ia?aiao?ia, a
caoai eiino?oeoi? i?iecaiaeiiai eeanna. Aeano?oeoi?u

aucuaathony a ia?aoiii ii?yaeea – nia/aea aeey i?iecaiaeiiai, caoai

aeey aaciaiai;

– a caaieiaea eiino?oeoi?a i?iecaiaeiiai eeanna iiaeao auoue

yaii oeacai aucia eiino?oeoi?a aaciaiai eeanna n ia?aiao?aie. Ii

iiaeao auoue aac eiaie, a iiaeao auoue n eiaiai aaciaiai eeanna. Anee

i?iecaiaeiue eeann aeeth/aao a naay iauaeou ianeieueeeo aaciauo

eeannia, oi a auciau eiino?oeoi?ia aaciauo eeannia aeieaeiu auoue

ia?a/eneaiu /a?ac caiyooth e aeieaeiu eiaoue eiaia aaciauo eeannia.

6.3 I?aaa aeinooia a i?iecaiaeiuo eeannao

————————————–

I?iecaiaeiue eeann aeeth/aao a naay eae i?eaaoioth, oae e
ioaee/ioth /anoue aaciaiai eeanna. I?e yoii aaaeii, A eaeoth /anoue
i?iecaiaeiiai eeanna, i?eaaoioth eee ioaee/ioth, iiiaaeoo
niioaaonoaothuea /anoe aaciaiai eeanna. Io yoiai caaeneo aeinooiiinoue
yeaiaioia

aaciaiai eeanna, eae ec yeaiaioia-ooieoeee i?iecaiaeiiai eeanna,

oae e ecaia – /a?ac iauaeou i?iecaiaeiiai eeanna. Caeanue aiciiaeiu

neaaeothuea aa?eaiou:

– i?eaaoiay /anoue aaciaiai eeanna A anaaaea aeeth/aaony a
i?eaaoioth /anoue i?iecaiaeiiai eeanna B, ii i?e yoii iaiin?aaenoaaiii

iaaeinooiia ec yeaiaioiaooieoeee eeanna B. Yoi niioaaonoaoao oiio

oaeoo, /oi a eeanna B ?ac?aoaaony ?aaioaoue n aaciaui iauaeoii

eeanna A oieueei ?ac?aoaiiuie a eeanna A n?aaenoaaie, oi anoue /a?ac
yeaiaiou-ooieoeee eeanna A. Eneeth/aiea ninoaaeyao iauyaeaiea anaai
eeanna B ae?oaeanoaaiiui a eeanna A;

– ii oiie/aieth, oi anoue i?e eniieueciaaiee caaieiaea aeaea

class B : A { }

ioaee/iay /anoue eeanna A iiiaaeaao a i?eaaoioth /anoue eeanna B.
Yoi cia/eo, /oi yeaiaiou-ooieoeee eeanna A aeinooiiu ec
yeaiaioia-ooieoeee eeanna B, ii ia iiaoo auoue aucaaiu ecaia, oi anoue
i?e ia?auaiee e iauaeoai eeanna B. Oi anoue aeey aiaoiaai iieueciaaoaey
eeanna B eioa?oaen eeanna A cae?uaaaony;

– a i?ioeaiii neo/aa, i?e iauyaeaiee

class B : public A { }

ioaee/iay /anoue eeanna A iiiaaeaao a ioaee/ioth /anoue eeanna B,
e aiaoiee iieueciaaoaeue i?e ?aaioa n iauaeoaie eeanna B iiaeao
i?eiaieoue eioa?oaenu eae i?iecaiaeiiai, oae e aaciaiai eeannia;

– e iaeiiaoe, a ii?aaeaeaiee ioaee/iie /anoe eeanna B iiaeii
yaii oeacaoue yeaiaiou-ooieoeee (a oaeaea aeaiiua) ioaee/iie /anoe
aaciaiai eeanna A, eioi?ua iiiaaeatho a ioaee/ioth /anoue eeanna B, oi
anoue auiieieoue i?aaeuaeouaa aeaenoaea naeaeoeaii ii ioiioaieth e
ioaeaeueiui yeaiaioai (i?e yoii oeacuaaaony oieueei eiy yeaiaioa):

class B : A {

public:

public A::fun;

}

Ia?a/eneaiiua aa?eaiou ecia?aaeaiu ia noaia:

class A class B

———–¬ —————-¬

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

+———-+ ¦ (iaaeinooiai B)¦

¦ public ¦ class B:A +—————+

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

¦ ¦ ¦ (aeinooiai B) ¦

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

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

¦ ¦ class B : A { … ¦ ¦

¦ ¦ public A::newadr; ¦ <---- Aeinooi e iauaeoai¦ ----------------------> i?iecaiaeiiai eeanna

L———– L—————

Ec ?anniio?aiiuo aa?eaioia aeaeii, /oi i?eaaoiay /anoue aaciaiai
eeanna iaaeinooiia a ethaii i?iecaiaeiii eeanna, /oi anoanoaaiii
neaaeoao ec naienoa cae?uoinoe ii?aaeaeaiey eeanna. Iaeiaei ii

aiaeiaee n ae?oaeanoaaiiinoueth aaciaue eeann iiaeao ?ac?aoeoue aeinooi

e naiei yeaiaioai ee/iie /anoe a i?iecaiaeiuo eeannao. Yoi aeaeaaony
i?e iiiiue iauyaeaiey caueuaiiuo (protected) yeaiaioia.

Yeaiaio n iaoeie protected a aaciaii eeanna aoiaeeo a i?eaaoioth
/anoue aaciaiai eeanna. E?iia oiai, ii aeinooiai e a i?eaaoiie

/anoe i?iecaiaeiiai eeanna. Anee aea aaciaue eeann aeeth/aaony a

i?iecaiaeiue eae public, oi caueuaiiue yeaiaio noaiiaeony caueuaiiui e a
i?iecaiaeiii eeanna, oi anoue iiaeao eniieueciaaoueny a iineaaeothueo
i?iecaiaeiuo eeannao. Neacaiiia iiyniei i?eia?ii e noaiie:

class A

{

int a1; // Iau/iue i?eaaoiue yeaiaio

protected: int a2; // Caueuaiiue i?eaaoiue yeaiaio

public:

}

class B : A // a1,a2 a i?eaaoiie /anoe B

{

void x();

}

void B::x()

{

a1 = 5; // Ioeaea: a1 iaaeinooiai a B

a2 = 3; // a2 aeinooiai a i?eaaoiie /anoe B

}

class B : public A // a2 aeinooiai e caueuai a i?eaaoiie

{ // /anoe B, iayaii eiaao ianoi

// protected: int a2;

}

class A class B

———–¬ —————-¬

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

+———-+ ¦ (iaaeinooiai B)¦

¦ protected¦ class B:A +—————+

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

¦ ===============¬ ¦ (aeinooiai B) ¦

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

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

¦ ¦ ¦===============¦

¦ ¦ ¦ public ¦

6.4 Nnueee ia iauaeou aaciaiai e i?iecaiaeiiai eeannia

—————————————————-

Ec eeanne/aneiai Ne ecaanoii, /oi iooai i?enaaeaaiey nnueeai
?acee/iiai oeia iaeiiai e oiai aea cia/aiey (aae?ana) iiaeii ?aaioaoue n
iauae iaiyoueth eae n ?acee/iuie no?oeoo?aie aeaiiuo. I?e yoii
i?aia?aciaaiea oeia e i?enaaeaaiea ia iaiytho cia/aiey nnueee, oi anoue
aae?ana iaiyoe.

I?eiaieoaeueii e aaciaiio e i?iecaiaeiiio eeanno iiaeii
neacaoue, /oi, i?aia?acoy nnueeo ia iauaeo i?iecaiaeiiai eeanna e
nnueea ia iauaeo aaciaiai eeanna, iu iieo/aai aeinooi e aeiaeaiiiio
iauaeoo aaciaiai eeanna. Ii i?e oaeii o?aeoiaaiee i?aia?aciaaiey oeia
nnueee o?aineyoi?o iaiaoiaeeii o/eouaaoue ?aciauaiea iauaeoa aaciaiai
eeanna a i?iecaiaeiii, /oi ii e aeaeaao. A ?acoeueoaoa i?e oaeii
i?aia?aciaaiee (i?enaaeaaiee) cia/aiea nnueee (aae?an iaiyoe) iiaeao
ieacaoueny ia ?aaiui enoiaeiiio. Aaeaeo oiai, /oi oaeie ia?aoiae io
iauaeoa i?iecaiaeiiai eeanna e aaciaiio /anoi ano?a/aaony e
ei??aeoe?oaony o?aineyoi?ii, yoi i?aia?aciaaiea oeia nnueee a Ne++
iiaeao auouee auiieiaii iayaii (inoaeueiua i?aia?aciaaiey

oeiia nnueie aeieaeiu auoue yaiyie)

Iiai/iue yooaeo oaeiai i?aia?aciaaiey ninoieo a oii, /oi
o?aineyoi? “caauaaao” ia iauaeoa i?iecaiaeiiai eeanna e aianoi
ia?aii?aaeaeaiiuo a iai ooieoeee aucuaaao ooieoeee aaciaiai eeanna.

class A

{

public: void f1();

};

class B : A

{

public: void f1(); // Ia?aii?aaeaeaia a eeanne B

void f2(); //

};

A *pa;

B *pb;

B x;

pa = &x; // Iayaiia i?aia?aciaaiea nnueee

// ia iauaeo eeanna B a nnueeo

// ia iauaeo eeanna A

pa->f1(); // Aucia ooieoeee ec aeiaeaiiiai

// iauaeoa aaciaiai eeanna A::f1(),

// oioy iia ia?aii?aaeaeaia

Ia?aoiia i?aia?aciaaiea io nnueee ia aaciaue eeann e nnueea

ia i?iecaiaeiue iiaeao auoue naeaeaii oieueei yaii. I?e yoii
ei??aeoiinoue oaeiai i?aia?aciaaiey caaeneo io i?ia?aiiu:

pb = (B*) pa; // Ia?aoiia i?aia?aciaaiea – yaiia

pb ->f2(); // Ei??aeoii, anee iiae “pa” aue

// iauaeo eeanna “B”

6.5 I?eioeei iauaeoii-i?eaioe?iaaiiiai i?ia?aiie?iaaiey

——————————————————

Iiiyoea i?iecaiaeiiai eeanna yaeyaony iniiaie
iauaeoiii?eaie?iaaiiiai iiaeoiaea e i?ia?aiie?iaaieth, eioi?ia iiaeii
ii?aaeaeeoue

eae i?ia?aiie?iaaiea “io eeanna e eeanno”. O?aaeeoeeiiiia
i?ia?aiie?iaaiea “io ooieoeee e ooieoeee” i?aaeiieaaaao, /oi aiiaue
?ac?aaaouaaaiua no?oeoo?u aeaiiuo aeeth/atho a naay ii?aaeaeaiiua
?aiaa, a

iiaua ooieoeee aeeth/atho auciau ?aiaa ii?aaeaeaiiuo.

I?e ?ac?aaioea iauaeoii-i?eaioe?iaaiiie i?ia?aiiu i?ia?aiieno
nicaeaao i?iecaiaeiua eeannu, eioi?ua aaoiiaoe/anee ianeaaeotho

ana naienoaa aaciauo, a caoai ia?aii?aaeaeyao iaeioi?ua eo ooieoeee

e aeiaaaeyao iiaua. A i?eioeeia ie/oi ia i?aiyonoaoao ia ethaii

o?iaia ?ac?aaioee ia?aeoe e o?aaeeoeeiiiiio i?ia?aiie?iaaieth e
nicaeaaaoue eeiaeioth i?ia?aiio, eniieuecoy iauaeou oaea
nouanoaothueo

eeannia. Neaaeiaaiea aea oaoiieiaee iauaeoii-i?eaioe?iaaiiiai
i?ia?aiie?iaaiey “aei eiioea” i?aaeiieaaaao, /oi i?eeeaaeiay
i?ia?aiia

i?aaenoaaeyao niaie eeann naiiai aa?oiaai o?iaiy, a aa auiieiaiea

– nicaeaiea iauaeoa yoiai eeanna eee auiieiaiea aeey iaai iaeioi?ie
ooieoeee oeia “run”.

Eaeoeey 7. Ae?ooaeueiua ooieoeee.

—————————–

7.1 Iiiyoea ae?ooaeueiie ooieoeee

——————————

Aeinoaoi/ii /anoi i?ia?aiienoo o?aaoaony nicaeaaaoue no?oeoo?u
aeaiiuo, aeeth/athueo a naay ia?aiaiiia /enei iauaeoia ?acee/iuo oeiia.
Aeey i?aaenoaaeaiey eo a i?ia?aiiao eniieuecothony nienee eee ianneau
nnueie ia yoe iauaeou. Iauaeou ?aciuo eeannia eiatho niioaaonoaaiii
?acee/iua oeiu nnueie, a aeey o?aiaiey a ianneaa eee nienea o?aaoaony
iaeei oei nnueie. Aeey i?aiaeieaiey yoiai i?ioeai?a/ey ana yoe eeannu
iauaeoia o?aaoaony naeaeaoue i?iecaiaeiuie io iaeiiai e oiai aea
aaciaiai eeanna, a i?e caiene a iannea i?aia?aciauaaoue nnueeo ia
iauaeo i?iecaiaeiiai eeanna a nnueeo ia iauaeo aaciaiai.

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]; // Iannea nnueie ia iauaeou AE

p[0] = &B1; // Nnueee ia iauaeou AE a

p[1] = &C1; // iauaeoao IE

p[2] = &A1;

Iaeiaei i?e oaeii i?aia?aciaaiee oeia “nnueea ia iauaeo IE” e

oeio “nnueea ia iauaeo AE” i?ienoiaeeo iioa?y eioi?iaoeee i oii,

eaeie iauaeo i?iecaiaeiiai eeanna “ie?oaeaao” aeinooiiue /a?ac nnueeo
iauaeo aaciaiai eeanna. Iiyoiio aianoi ia?aii?aaeaeaiiuo ooieoeee a
i?iecaiaeiuo eeannao aoaeoo aucuaaoueny ooieoeee a aaciaii, oi

anoue

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

p[1]->f(); // ai anao neo/ayo, oioy f()

p[2]->f(); // ia?aii?aaeaeaiu

Iaeiaei ii eiaeea iinoaaeaiiie caaea/e o?aaoaony, /oiau aucuaaaiay
ooieoeey niioaaonoaiaaea oiio iauaeoo, eioi?ue ?aaeueii iaoiaeeony iiae
nnueeie. Iaeaieaa i?inoi yoi naeaeaoue oae:

– o?aieoue a iauaeoa aaciaiai eeanna eaeaioeoeeaoi?
“ie?oaeathuaai” aai i?iecaiaeiiai eeanna;

– a nienea eee oaaeeoea o?aieoue nnueee ia iauaeo aaciaiai

eeanna;

– i?e auciaa ooieoeee ii nnueea ia iauaeo aaciaiai eeanna

eaeaioeoeoee?iaaoue oei i?iecaiaeiiai eeanna e yaii aucuaaoue aeey iaai

ia?aii?aaeaeaiioth ooieoeeth;

– eaeaioeoeeaoi? eeanna onoaiaaeeaaoue i?e nicaeaiee iauaeoa ,

oi anoue a aai eiino?oeoi?a.

class a

{

public: int id; // Eaeaioeoeeaoi? eeanna

void f();

void newf(); // Iiaay ooieoeey f() n eaeaioeoeeaoeeae IE

}

a::a() // Eiino?oeoi?u iauaeoia

{ …

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(); // Aucia b::f() aeey B1

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

p[2]->newf(); // Aucia a::f() aeey A1

Ionthaea neaaeoao ii?aaeaeaiea ae?ooaeueiie ooieoeee. Ae?ooaeueiay
ooieoeey (AO) – yoi ooieoeey, ii?aaeaeyaiay a aaciaii e ianeaaeoaiay
eee ia?aii?aaeaeyaiay a i?iecaiaeiuo eeannao. I?e auciaa aa ii
nnueea ia iauaeo aaciaiai eeanna i?ienoiaeeo aucia oie ooieoeee,
eioi?ay niioaaonoaoao eeanno iauaeoa, aeeth/athuaio a naay aeaiiue
iauaeo aaciaiai eeanna.

Oaeei ia?acii, anee i?e i?aia?aciaaiee oeia “nnueea ia IE” e oeio
“nnueea ia AE” i?ienoiaeeo iioa?y eioi?iaoeee ia iauaeoa i?iecaiaeiiai
eeanna, oi i?e auciaa ae?ooaeueiie ooieoeee i?ienoiaeeo ia?aoiue
i?ioeann iayaiiai ainnoaiiaeaiey oeia iauaeoa.

?aaeecaoeey iaoaiecia ae?ooaeueiuo ooieoeee caeeth/aaony a
nicaeaiee eiiieeyoi?ii oaaeeoeu aae?ania ae?ooaeueiuo ooieoeee
(nnueie).

Oaeay oaaeeoea nicaeaaony aeey aaciaiai eeanna e aeey eaaeaeiai
aeeth/aiey aaciaiai eeanna a i?iecaiaeiue. A iauaeoa aaciaiai eeanna
nicaeaaony aeiiieieoaeueiue yeaiaio – nnueea ia oaaeeoeo aae?ania
aai ae?ooaeueiuo ooieoeee. Yoa nnueea onoaiaaeeaaaony eiino?oeoo?ii i?e
nicaeaiee iauaeoa i?iecaiaeiiai eeanna. I?e auciaa ae?ooaeueiie
ooieoeee ii nnueea ia iauaeo aaciaiai eeanna ec iauaeoa aa?aony
nnueea ia oaaeeoeo ooieoeee e ec iaa aa?aony aae?an ooieoeee ii
oeene?iaaiiiio niauaieth. Ieaea eeethno?e?oaony ?aaeecaoeey yoiai
iaoaiecia (iiae/a?eioou yeaiaiou, nicaeaaaaiua iayaii eiiieeyo?ii).

class A

{

——> void (**ftable)(); // Nnueea ia oaaeeoeo aae?ania

// ae?ooaeueiuo ooieoeee

public:

virtual void x();

virtual void y();

virtual void z();

A();

~A();

};

// Oaaeeoea aae?ania ooieoeee eeanna A

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

A::A()

{

——> ftable = TableA; // Onoaiiaea oaaeeoeu aeey eeanna A

}

class B : public A

{

public:

void x();

void z();

B();

~B();

};

// Oaaeeoea aae?ania ooieoeee eeanna A

// a eeanna B

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

¦ L ia?aii?aaeaeyaony a B

B::B() L—— ianeaaeoaony ec A

{

–> ftable = TableB; // Onoaiiaea oaaeeoeu aeey eeanna B

}

void main()

{

A* p; // Nnueea p aaciaiai eeanna A

B nnn; // nnueaaony ia iauaeo i?iecaiaep = &nnn;
// iiai eeanna B

?aaeecaoeey

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

}

p nnn TableB B::z()

—–¬ ——–>–B—–¬ —–>———¬ —>———-¬

¦ —— ftable¦–A—¬¦ ¦ 0+——–+ ¦ ¦ ¦

L—– ¦¦ —— 1+——–+ ¦ ¦ ¦

¦+—–+¦ 2¦ ——— L———
¦¦ ¦¦ L——–

7.2 Aano?aeoiua eeannu

———————

Anee aaciaue eeann eniieuecoaony oieueei aeey ii?iaeaeaiey
i?iecaiaeiuo eeannia, oi ae?ooaeueiua ooieoeee a aaciaii eeanna
iiaoo

auoue “ionouie”, iineieueeo ieeiaaea ia aoaeoo aucaaiu aeey iauaeoa

aaciaiai eeanna. Oaeie aaciaue eeann iacuaaaony aano?aeoiui.
Ae?ooaeueiua ooieoeee a ii?aaeaeaiee eeanna iaicia/athony neaaeothuei
ia?acii:

class base

{

public:

virtual print() =0;

virtual get() =0;

}

Anoanoaaiii, /oi ii?aaeaeyoue oaea yoeo ooieoeee ia o?aaoaony.

7.3 Iiiaeanoaaiiia ianeaaeiaaiea e ae?ooaeueiua ooieoeee

—————————————————

Iiiaeanoaaiiui ianeaaeiaaieai iacuaaaony i?ioeann nicaeaiey
i?iecaiaeiiai eeanna ec aeaoo e aieaa aaciauo. A yoii neo/aa
i?iecaiaeiue eeann ianeaaeoao aeaiiua e ooieoeee anao naieo aaciauo
eeannia.

Nouanoaaiiui aeey ?aaeecaoeee iiiaeanoaaiiiai ianeaaeiaaiey yaeyaony

oi, /oi aae?ana iauaeoia aoi?iai e o.ae. aaciauo eeannia ia niaiaaeatho
n aae?anii iauaeoa i?iecaiaeiiai e ia?aiai aaciaiai eeannia,

oi anoue eiatho oeene?iaaiiua niauaiey ioiineoaeueii ia/aea iauaeoa:

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———-

I?aia?aciaaiea nnueee ia iauaeo i?iecaiaeiiai eeanna e nnueea

ia iauaeo aaciaiai eeanna o?aaoao aeiaaaeaiey e oeacaoaeth oaeouaai

iauaeoa this niioaaonoaothuaai niauaiey (db,dc), ia?aoiia i?aia?aciaaiea
– au/eoaiea yoiai aea niauaiey. Oaeia aeaenoaea auiieiyaony

eiiieeyoi?ii, eiaaea a iauaeoa i?iecaiaeiiai eeanna ianeaaeoaony

ooieoeey ec aoi?iai e o.ae. aaciaiai eeanna, iai?eia? i?e ii?aaeaeaiee a
eeanna “b” ooieoeee “f()” e aa ianeaaeiaaiee a eeanna “d” aucia D1.f()
aoaeao ?aaeeciaai neaaeothuei ia?acii:

this = &D1; // Aae?an iauaeoa i?iecaiaeiiai eeanna

this = (char*)this + db // Aae?an iauaeoa eeanna b a iai

b::f(this); // Aucia ooieoeee a eeanna b ni naiei

// iauaeoii

?anniio?ei iniaaiiinoe iaoaiecia ae?ooaeueiuo ooieoeee i?e

iiiaeanoaaiiii ianeaaeiaaiee. Ai-ia?auo, ia eaaeaeue aaciaue eeann a

i?iecaiaeiii eeanna nicaeaaony naiy oaaeeoea ae?ooaeueiuo ooieoeee (a

iaoai neo/aa – aeey “a” a “d”, aeey “b” a “d” e aeey “c” a “d”).

Ai-aoi?uo, anee ooieoeey aaciaiai eeanna ia?aii?aaeaeaia a
i?iecaiaeiii, oi i?e auciaa ae?ooaeueiie ooieoeee o?aaoaony
i?aia?aciaaoue

nnueeo ia iauaeo aaciaiai eeanna a nnueeo ia iauaeo i?iecaiaeiiai,

oi anoue aeey aoi?iai e o.ae. aaciauo eeannia au/anoue ec this
niioaaonoaothuaa niauaiea. Aeey yoiai o?aineyoi? aeeth/aao
niioaaonoaothuee eiae, ei??aeoe?othuee cia/aiea this a aeaea “caieaou”,
ia?aaeathuae oi?aaeaiea eiiaiaeie ia?aoiaea e ia?aii?aaeaeyaiie
ooieoeee.

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() ianeaaeothony

public: void g(); // g() ia?aii?aaeaeyaony

void h(); // h() ia?aii?aaeaeyaony

}

a A1;

b B1;

c C1;

pa = &A1;

pb = &B1;

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

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

pa = &C1;

pb = &C1;

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

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

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

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

Oaaeeoeu ae?ooaeueiuo ooieoeee aeey aeaiiiai i?eia?a eiatho aeae:

A1

-a—-¬ Oaaeeoea AO aeey “a”

¦ ————>——–¬

+—–+ ¦a::f() ¦

L—— +——-+

¦a::g() ¦

L——- B1

-b—-¬ Oaaeeoea AO aeey “b”

¦ ————>——–¬

+—–+ ¦b::h() ¦

L—— +——-+

¦b::t() ¦

L——- C1

T –c—–¬ Oaaeeoea AO aeey “a” a “c”

¦ ¦–a—¬¦ ——–¬

db ¦ ¦¦ ———–>¦a::f() ¦

¦ ¦L——¦ +——-+

+ ¦–b—¬¦ ¦c::g() ¦

¦¦ ——-¬ L——- ¦L——¦ ¦ Oaaeeoea AO
aeey “b” a “c”

¦ ¦ ¦

¦ ¦ L—>——–¬ “Caieaoa” aeey c::h()

L——– ¦ xxx()—–>–xxx()—————-¬

+——-+ ¦ this=(char*)this – db¦

¦b::t() ¦ ¦ goto c::h ¦

L——– L———————-

Ae?oaei aa?eaioii ?aoaiey i?iaeaiu yaeyaony o?aiaiea iaiaoiaeeiuo
niauaiee a naieo oaaeeoeao ae?ooaeueiuo ooieoeee.

7.4. Ae?ooaeueiua aaciaua eeannu

——————————

A i?ioeanna ea?a?oe/aneiai ii?aaeaeaiey i?iecaiaeiuo eeannia

iiaeao iieo/eoueny, /oi a iauaeo i?iecaiaeiiai eeanna aieaeoo

ianeieueei iauaeoia aaciaiai eeanna, iai?eia?

class base {}

class a : public base {}

class b : public base {}

class c : a, b {}

A eeanna “c” i?enoonoaotho aeaa iauaeoa eeanna base. Aeey
eneeth/aiey oaeiai aeoaee?iaaiey iauaeo aaciaiai eeanna aeieaeai
auoue

iauyaeai ae?ooaeueiui

class a : virtual public base {}

class b : virtual public base {}

class c : public a, public b {}

a A1;

b B1;

c C1;

Iauaeo iaueiiaaiiiai aaciaiai eeanna ?aniieaaaaony, eae i?aaeei, a
ia/aea iauaeoa i?iecaiaeiiai eeanna e eiaao oeene?iaaiiia

niauaiea. Anee aea aaciaue eeann yaeyaony ae?ooaeueiui, oi o?aaoaony
aai aeeiaie/aneia ?aciauaiea. Oiaaea a iauaeoa i?iecaiaeiiai

eeanna ia niioaaonoaothuai ianoa ?aciauaaony ia iauaeo aaciaiai

eeanna, a nnueea ia iaai, eioi?ay onoaiaaeeaaaony eiino?oeoi?ii.

Aeey auoai?eaaaeaiiiai i?eia?a eiaai

A1 B1 C1

–a——¬ –b—–¬ –c—————¬

¦ ——¬ ¦ ——¬ ¦ –a——-¬ ¦

+——–+ ¦ +——-+ ¦ ¦ ¦ ——-¬ ¦

¦ ¦ ¦ ¦ ¦ ¦ ¦ +———+ ¦ ¦

¦-base–¬<---- ¦-base-¬<---- ¦ ¦ ¦ ¦ ¦¦L-------¦ ¦L------¦ ¦ L---------- ¦ ¦L--------- L-------- ¦ --b-------¬ ¦ ¦¦ ¦ ------¬¦ ¦¦ +---------+ ¦¦ ¦¦ ¦ ¦ ¦¦ ¦¦ L---------- ¦¦ ¦¦ -base---¬<---¦ ¦¦ L--------<---- ¦L-----------------Oaaeeoeu ae?ooaeueiuo ooieoeee ?aniieaaathony a eaaeaeii aaciaiieeanna iau/iui ia?acii.Eaeoeey 8. I?eia? eniieueciaaiey ae?ooaeueiuo ooieoeee-------------------------------------------------?aeyoeeiiiay aaca aeaiiuo (?AAe) i?aaenoaaeyao niaie oaaeeoeo,ninoiyuoth ec i?iecaieueiiai eiee/anoaa no?ie e noieaoeia. A ea/anoaa yeaiaioia, o?aiyueony a eeaoea oaeie oaaeeoeu iiaoo auoue oeaeua,aauanoaaiiua /enea, no?iee iinoiyiiie e ia?aiaiiie aeeeiu, cia/aiey aeaou e a?aiaie. Ana oeiu yeaiaioia a iaeiii noieaoea aeieaeiuniaiaaeaoue. Eiee/anoai no?ie e noieaoeia, iacaaiey noieaoeia e oeiuyeaiaioia a ieo ca?aiaa ia ecaanoiu e ii?aaeaeythony aeeiaie/anee,oi anoue a i?ioeanna ?aaiou i?ia?aiiu.A iaoai i?eia?a ?anniio?ei ?AAe, oeaeeeii ?aciauaiioth a iaiyoe.8.1 I?aaiecaoeey ?AAe a aeaea oaaeeoe nnueie ia iauaeou--------------------------------------------------I?aaeaea anaai, ii?aaeaeei yeaiaiou, ?aciauaiiua a eeaoeao oaaeeoeu, eae iauaeou niioaaonoaothueo yeaiaioa?iuo eeannia:- class string - no?iee ia?aiaiiie aeeeiu;- class integer - oeaeua /enea;- class real - aauanoaaiiua /enea;- class dat - aeaoa;- class time - a?aiy e o.ae.Ia?a/aiue oaeeo eeannia iiaeao auoue ?anoe?ai. Aeey eaaeaeiai ecieo iaiaoiaeeii ii?aaeaeeoue iiiaeanoai ooieoeee e iia?aoeee, eioi?ueaoaeao eniieueciaaoueny i?e ?aaioa n oaaeeoeae: aaiae e auaiae cia/aieyiauaeoa, n?aaiaiea iauaeoia, nicaeaiea eiiee iauaeoa, i?eaaaeaiea eaaciaui oeiai aeaiiuo (int, long, char*), a?eoiaoe/aneeo iia?aoeeeiaae iauaeoaie e oeaeuie e o.ae.. Eioa?i?aoaoeey iineaaeieo aeieaeiaauoue naiy aeey eaaeaeiai oeia iauaeoia. Eaaeaeue ec yoeo eeannia naeaeaai i?iecaiaeiui io iaeioi?iai aaciaiai eeanna, ?ieue eioi?iai aoaeao ?anniio?aia iicaeiaa.A ea/anoaa i?eia?a ?anniio?ei eeann no?ie ia?aiaiiie aeeeiu.class string : base{char *s; // Nnueea ia no?ieoint sz; // Aeeeia no?ieepublic: int GET(); // Aaiae no?ieevoid PUT(); // Auaiae no?ieeint CMP(base*); // N?aaiaiea no?iechar *NAME(); // Aica?auaao eiy eeanna no?iebase *COPY(); // Aica?auaao eiieth iauaeoaoperator long(); // I?aia?aciaaiea e oeio long // aica?auaao aeeeio no?ieeoperator char*(); // I?aia?aciaaiea e oeio char* // aica?auaao eiieth no?ieebase& operator+(char*); // Iia?aoeey "+ no?iea"// i?eniaaeeiyao no?ieostring();~string();}//------------------------------------------------------string::string() // Eiino?oeoi?{s = NULL; // No?iea ionoaysz = 0;}//------------------------------------------------------string::~string( ) // Aeano?oeoi?{if (s !=NULL) delete s; // Inaiaiaeeoue iaiyoue}//------------------------------------------------------int string::GET(){char ss[80];if (s !=NULL) delete s;gets(ss); // Aaiae no?iee e ?aciauaiea aas = new char[sz = strlen(ss)+1];// a aeeiaie/aneie iaiyoestrcpy(s,ss);return(1); // Aaiae anaaaea i?aaeeueiue}//------------------------------------------------------void string::PUT(){ puts(s); }//------------------------------------------------------int string::CMP(base* two) // N?aaiaiea no?ie ii aeoaaeoo{string *p = (string*) two; // I?aia?aciaaoue nnueeo iareturn(strcmp(s, p->s); // aoi?ie iauaeo e eeanno no?ie

} // (ia?aoiae io AE e IE)

//——————————————————-char
*string::NAME() // Aica?auaao eiy eeanna no?ie

{ return(“No?iea”); }

//——————————————————-base
*string::COPY() // Nicaeaiea eiiee iauaeoa

{ // aac eiie?iaaiey cia/aiey

string *p = new string; //

return(p); //

}

//——————————————————-string::operato
r long() // I?aia?aciaaiea e oeio long {
// aica?auaao aeeeio no?iee

return (sz);

}

//——————————————————-string::operato
r char*() // I?aia?aciaaiea e oeio char* {
// aica?auaao oaenoiaia i?aaenoaachar *p = new char[sz];
// eaiea cia/aiey iauaeoa

strcpy(p,s);

return(p);

}

//——————————————————–base&
string::operator+(char* two) // Iia?aoeey “+ no?iea”

{ // Eiieaoaiaoeey no?iee a iauaeoa

char ss[80]; // e aoiaeiie no?iee

strcpy(ss,s); //

strcat(ss,two);

delete s;

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

strcpy(s,ss);

return(*(base*)this); // Aica?aoeoue iayaioth nnueeo ia iauaeo

} // aeiaeaiiiai aaciaiai eeanna

//——————————————————-

Aaciaue eeann “base” iaiaoiaeei eneeth/eoaeueii aeey iaania/aiey

eaeaioe/iiai aeinooia e ethaiio yeaiaioo aacu aeaiiuo iacaaeneii io

aai eeanna. Yoi aano?aeoiue eeann, niaea?aeauee iauyaeaiea anao
auoaia?a/eneaiiuo ooieoeee e iia?aoeee ae?ooaeueiuie.

class base

{

public:

virtual int GET()=0; // Aaiae cia/aiey iauaeoa

virtual void PUT()=0; // Auaiae cia/aiey iauaeoa

virtual int CMP(base*)=0; // N?aaiaiea cia/aiee iauaeoia

virtual char *NAME()=0; // Aica?auaao eiy eeanna

virtual base *COPY()=0; // Aica?auaao eiieth iauaeoa

virtual operator long()=0; // I?aia?aciaaiea e oeio long

virtual operator char*()=0; // I?aia?aciaaiea e oeio char*

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

// Iia?aoeey “+ no?iea”

virtual ~base(); // Ae?ooaeueiue aeano?oeoi? aeey

// ?ac?ooaiey iauaeoa IE ii

}; // nnueea ia AE

Naia aeaoia?iay oaaeeoea iauaeoia i?aaieciaaia o?aaeeoeeiiiui

aeey no?oeoo? ia?aiaiiie ?acia?iinoe niiniaii:

– yeaiaio AAe nicaeaaony a aeeiaie/aneie iaiyoe i?e aeiaaaeaiee

no?iee e AAe;

– no?iea AAe i?aaenoaaeaia ianneaii nnueie ia iauaeou eeanna

base. Nai iannea oaeaea nicaeaaony a aeeiaie/aneie iaiyoe i?e
aeiaaaeaiee iiaie no?iee a AAe;

– nnueee ia no?iee nia?aiu a iannea, eioi?ue nicaeaaony

eiino?oeoi?ii aacu aeaiiuo e caiieiyaony i?e auciaa ooieoeee aeiaaaeaiey
no?iee (oaaeeoea no?ie AAe);

– iauaeo eeanna AAe (table) niaea?aeeo nnueeo TBL ia oaaeeoeo

no?ie.

Iniai neaaeoao inoaiiaeoueny ia niiniaa iacia/aiey noieaoeai

oeiia niaea?aeaueony a ieo yeaiaioia AAe (eee eeannia iauaeoia). Yoi

aeaeaaony i?e iiiiue no?iee caaieiaea AAe – head. Yoio iannea
niaea?aeeo nnueee ia iauaeou, eeannu eioi?uo eaeaioeoeoee?otho oeiu
yeaiaioia a niioaaonoaothueo noieaoeao. I?e nicaeaiee iiaie no?iee AAe

ae?ooaeueiie ooieoeeae COPY nicaeathony eiiee iauaeoia ec no?iee
caaieiaea AAe, aeey eioi?uo caoai aucuaaaony ae?ooaeueiay ooieoeey
aaiaea cia/aiee GET.

No?iea caaieiaea nicaeaaony eiino?oeoi?ii iauaeoa eeanna AAe.

Eiaaony iaith oeiia yeaiaioia, eioi?ia i?aaenoaaeyao niaie iannea

nnueie (TYPE) ia iauaeou eeannia string,integer,dat e o.ae.. Ye?aiiia
iaith no?ieony i?e iiiiue auciaa ae?ooaeueiie ooieoeee auaiaea

eiaie eeanna TYPE[i]->NAME(). Iinea auai?a no?iee iaith nnueea ia

niioaaonoaothuee aua?aiiue iauaeo ia?aiineony a no?ieo caaieiaea AAe.

class table

{

int nc; // Eiee/anoai noieaoeia

int nr; // Eiee/anoai no?ie

char **names; // Eiaia no?iaoeia

base **head; // No?iea iauaeoia caaieiaea AAe

// aeey iauyaeaiey oeiia iauaeoia

base ***TBL; // Oaaeeoea no?ie AAe

public:

void append(); // Aeiaaaeaiea no?iee a AAe

void sort(int); // Ni?oe?iaea ii cia/aieyi noieaoea

long averrage(int); // Iiaen/ao n?aaeiaai a?eoiaoe/aneiai

// aeey noieaoea

base& operator()(int,int);

// Auai? iauaeoa ec AAe

table(); // Eiino?oeoi? – nicaeaiea AAe

~table(); // Aeano?oeoi? – oaeaeaiea AAe

}

iauaeo AAe

TBL Iannea no?ie AAe

–¬ ———¬0

¦——->+——–+.. Yeaiaio AAe

L– +——–+i No?iea AAe string

base*** ¦ ———–>———¬0 integer

+——–+ +——–+.. real

+——–+ +——–+j –dat——-¬

base** ¦ ————–>-base—–¬¦

+——–+ ¦L———-¦

base* ¦ ¦

L———–
base

head No?iea caaieiaea AAe

–¬ S0

¦————–>———¬0 -string—¬

L– ¦ ——————>-base—¬¦

base** +——–+ ———->L——–¦

¦ ————–¬ L———
+——–+ ¦ ¦ D0

¦ ——— ¦ -dat——¬

+——–+ L—>-base—¬¦

base* ¦L——–¦

L———//——————————————————//
Iaith eeannia iauaeoia (oeiia noieaoeia)

string S0;

dat D0;

time T0;

integer I0;

base *TYPE[] = {

(base*) &S0;

(base*) &D0;

(base*) &T0;

(base*) &I0;

};

//—————————————————–// Nicaeaiea
no?oeoo?u AAe

#define MAXCOL 30

#define MAXREC 1000

table::table()

{

int i,j,n;

char ss[80];

names = new char*[MAXCOL]; // Oaaeeoea aae?ania eiai noieaoeia

head = new base*[MAXCOL]; // Oaaeeoea nnueie ia iauaeou

for (nc=0; ncNAME() );

}

//—— auai? oeia noieaoea – n

head[nc] = TYPE[n]; // Nnueea ia iauaeo n eeannii,

// niioaaonoaothuei eeanno

// iauaeoia noieaoea

TBL = new base**[MAXREC];

nr = 0; // Oaaeeoea nnueie ia no?iee AAe

}

}

//——————————————————//
Aeano?oeoi? AAe

tabe::~table()

{

int i,j;

for (i=0; iCOPY();

printf(“Noieaaoe %s oeia %s :”,names[i],head[i]->NAME());

// Auaiae iiaeneacee eiaie e oeia noieaoea

while(TBL[nr][i]->GET() ==0);// Aaiae cia/aiey iiaiai iauaeoa

}

nr++;

}

//——————————————————-//
Iaoiaeaeaiea n?aaeiaai a?eoiaoe/aneiai ii caaeaiiiio noieaoeo

long table::averrage(int n)

{

long r;

int i;

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

for (r=0, i=0; iCMP(TBL[i+1][n]) <0){p = TBL[i][n];TBL[i][n] = TBL[i+1][n];TBL[i+1][n] = p;k++;}}while (k); // Iiea anoue ia?anoaiiaee}//----------------------------------------------------------// Auai? yeaiaioa (i,j) a AAe - aica?auaao iayaioth nnueeo ia// AE iauaeoa, ii eioi?ie aiciiaeai aucia ethaie ae?ooaeueiie// ooieoeeestatic base empty; // Ionoie iauaeo aeey ioeai/iiai auai?abase& table::opertor()(int i,int j){if (i<0 || i >= nr) return(empty);

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

return (*TBL[i][j]); // Aica?aoeoue iayaioth nnueeo ia iauaeo

}

//——————————————————–// I?eia?
?aaiou n eeannii ?AAe

void main()

{

int i,j;

table R; // Nicaeaiea AAe

for (i=0; i<10; i++)R.append(); // Aaiae 10-oe no?ieR.sort(1); // Ni?oe?iaea ii ia?aiio noieaoeoscanf("%d %d", &i, &j); // Auaiae cia/aiey yeaiaioaR(i,j).PUT();R(1,2) + "1234"; // Iia?aoeey "iauaeo + no?iea"// aeey yeaiaioa 1,2}Eaeoeey 9. Oaaeiiu.-----------------Aeiaieueii /anoi eeannu nicaeathony aeey iauaaeeiaieyiiiaeanoaa yeaiaioia aeaiiuo, eioi?ua aioo?e iauaeoa iiaooauoue naycaiu ianneaii nnueie, nieneii, aea?aaii e o.ae..I?e yoii iauaeo eeanna niaea?aeeo nnueeeOaeei ia?acii, o?aaoaony ii?aaeaeeoue iaeioi?ia iiiaeanoai eaeaioe/iuo eeannia n ia?aiao?eciaaiiui oeiii aioo?aiieo yeaiaioia. Oi anoue aeieaeia auoue caaeaia caaioiaeaeeanna (oaaeii), a eioi?ii a aeaea ia?aiao?a caaeai oei(eeann) aoiaeyueo a iaai aioo?aiieo yeaiaioia aeaiiuo. Oiaaeai?e nicaeaiee iauaeoa iaiaoiaeeii aeiiieieoaeueii oeacuaaoue eeiie?aoiue oei aioo?aiieo yeaiaioia a ea/anoaa ia?aiao?a.Nicaeaiea iauaeoa nii?iaiaeaeaaony oaeaea e nicaeaieainiioaaonoaothuaai eiie?aoiiai eeanna aeey caaeaiiiai eiieo?aoiiai oeia.I?eiyoue a Ne++ niinia ii?aaeaeaiey iiiaeanoaa eeannian ia?aiao?eciaaiiui aioo?aiiei oeiii aeaiiuo (eia/a, iae?iii?aaeaeaiea) iacuaaaony oaaeiiii (template). Neioaenenoaaeiia ?anniio?ei ia i?eia?a oaaeiia eeanna aaeoi?ia,niaea?aeaueo aeeiaie/aneee iannea nnueie ia ia?aiaiiua caaeaiiiai oeia.--- ia?aiao? oaaeiia - eeann "T", aioo?aiiee¦ oei aeaiiuo¦ --- eiy a?oiiu oaaeiiiuo eeanniatemplate class vector

{

int tsize; // Iauaa eiee/anoai yeaiaoia

int csize; // Oaeouaa eiee/anoai yeaiaioia

T **obj; // Iannea nnueie ia ia?aiao?eciaai // iua iauaeou oeia “T”

public:

T *operator[](int);

// iia?aoi? [int] aica?auaao nnueeo

// ia ia?aiao?eciaaiiue iauaeo

// eeanna “T”

void insert(T*); // ooieoeey aeeth/aiey iauaeoa oeia “T”

int extract(T*); //

};

Aeaiiue oaaeii iiaeao eniieueciaaoueny aeey ii?iaeaeaiey

iauaeoia-aaeoi?ia, eaaeaeue ec eioi?uo o?aieo iauaeou ii?aaeaeaiiiai
oeia. Eiy eeanna i?e yoii ninoaaeyaony ec eiaie

oaaeiia “vector” e eiaie oeia aeaiiuo (eeanna), eioi?ue iiaenoaaeyaony
aianoi ia?aiao?a “O”:

vector a;

vector b;

extern class time;

vector

Caiaoei, /oi o?aineyoi?ii i?e ii?aaeaeaiee eaaeaeiai

aaeoi?a n iiaui oeiii iauaeoia aaia?e?oaony iienaiea iiaiai

eeanna ii caaeaiiiio oaaeiio (anoanoaaiii, iayaii a i?ioeanna

o?aineyoeee):

class vector

{

int tsize;

int csize;

int **obj;

public:

int *operator[](int);

void insert(int*);

int index(int*);

};

Aeaeaa neaaeoao i/aaeaeiia ooaa?aeaeaiea, /oi yeaiaiouooieoeee oaaeiia
oaeaea aeieaeiu auoue ia?aiao?eciaaiu, oi anoue

aaia?e?iaaoueny aeey eaaeaeiai iiaiai oeia aeaiiuo. Aeaenoaeoaeueii,
yoi oae: yeaiaiou-ooieoeee oaaeiia eeannia a naith

i/a?aaeue oaeaea yaeythony oaaeiiiuie ooieoeeyie n oai aea naiui

ia?aiao?ii. Oi aea naiia eanaaony ia?aii?aaeaeyaiuo iia?aoi?ia:

— ia?aiao? oaaeiia – eeann “T”, aioo?aiiee

¦ oei aeaiiuo

¦ — eiy yeaiaioa-ooieoeee eee

¦ ¦ iia?aoi?a – ia?aiao?eciaaii

¦ ¦

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::operator[](int n)

{

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

return (obj[n]);

}

int vector::index(int *pobj)

{

int n;

for (n=0; n class FIFO

{

int fst,lst; // Oeacaoaee ia ia/aei-eiiaoe

// i/a?aaee

T queue[size]; // Iannea iauaeoia eeanna “T”

// ?acia?iinoe “size”

public:

T from(); // Ooieoeee aeeth/aiey-eneeth/aiey

void into(T); //

FIFO(); // Eiino?oeoi?

};

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;

}

I?eia? eniieueciaaiey:

FIFO<100> a;

FIFO<20> b;

struct x {};

FIFO<50> c;

I?eia? naaia?e?iaaiiiai eiiieeyoi?ii eeanna aeey iauaeoa “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;

}

PAGE

PAGE 27

Нашли опечатку? Выделите и нажмите CTRL+Enter

Похожие документы
Обсуждение

Оставить комментарий

avatar
  Подписаться  
Уведомление о
Заказать реферат
UkrReferat.com. Всі права захищені. 2000-2019