Object-Oriented Programming C++

download Object-Oriented Programming C++

of 247

  • date post

    13-Apr-2015
  • Category

    Documents

  • view

    43
  • download

    9

Embed Size (px)

description

Σημειώσεις αντικειμενοστραφούς προγραμματισμού για το Κ10 της Πληροφορικής και Τηλεπικοινωνιών το ΕΚΠΑ

Transcript of Object-Oriented Programming C++

EJNIKO KAI KAPODISTRIAKO PANEPISTHMIO AJHNWN TMHMA PLHROFORIKHS KAI THLEPIKOINWNIWN

IZAMPW KARALH AJHNA 2010

1

C++ ( g++) / C++ ( C++) / ( C++) ( C++) C++,

2

:Ja mporosate na mac dieukrinsete t akribc exuphrete o antikeimenostrafc programmatismc; 'Oqi pc ulopoietai (me klseic, antikemena klp), all poia enai h praktik qrhsimthta kai h ousiastik diafor stic dunatthtc tou ap ton diadikasiak. Giat dhlad kpoioc ja qrhsimopoisei pq th C++ gia na ftixei na prgramma, ant gia th C. An kai parakolouj to mjhma, den qw katalbei thn ousiastik diafor metax twn do eidn programmatismo. Ta do prta majmata estazan a sthn angkh parxhc tou antikeimenostrafoc programmatismo. Epshc, sto mjhma thc Paraskeuc b gine upenjmish thc angkhc orismo nwn tpwn, thc anaparstasc touc san klseic sto antikeimenostrafc montlo kai h ulopohsh twn klsewn sth C++ pou katpin estisame. Se kje perptwsh, mwc, de blptei na xekajarsoume kai pli ta prgmata mia pou to jewr shmantik na qoun diasafhniste kai mia pou jummai ki eg, xekinntac ap glssec antikeimenostrafoc programmatismo ki qi ap efarmogc, ti eqa thn dia agwna. Na xekajarsw ti sta plasia tou majmatoc milme gia antikeimenostraf programmatism pnw ap diadikastik programmatism. Epame loipn ti to ti enai katllhlo na epilxoume: glssa diadikastiko programmatismo (p.q. C) glssa antikeimenostrafoc (p.q. C++) exarttai ap th fsh tou problmatoc pou pme na antimetwpsoume. Prte, gia pardeigma,a . . . kai exakoloujon na estizoun b dhlad, to eisagwgik mjhma gia touc tpouc pou orzontai ap to

qrsth

3

to exc prblhma: Gryte na prgramma to opoo na taxinome N onmata. Ed ti qoume: h taxinmhsh apotele mia diadikasa pou baszetai se mia sugkekrimnh mjodo (algrijmo) taxinmhshc. Aut efarmzetai se kpoia dedomna pou enai apl strings sto pardeigma. Epshc, mporome na pome ti baszetai se mia diadikasa pou pragmatopoie sgkrish metax do strings. Antstoiqo pardeigma ja tan, p.q. na epiluje na arijmhtik prblhma me qrsh miac arijmhtikc mejdou. Sta parapnw problmata h mfash enai sthn ulopohsh diadikasin oi opoec efarmzontai pnw sta dedomna. 'Ara, qreiazmaste glssec pou oi glwssikc domc touc (ta language constructs touc) dieukolnoun thn anaparstash (ulopohsh) diadikasin. Prte, tra to pardeigma thc ulopohshc miac efarmogc grafikn, p.q. kinomena sqdia. Ed ti qoume: qoume kpoiec ontthtec pou tic sqetzoume me mia sumperifor. San sumperifor jewrome na snolo diadikasin (ston antikeimenostraf diadikastik programmatism) pou mporon na diexaqjon an autc oi ontthtec lboun kpoia mhnmata. Sto pardeigma twn kinoumnwn sqedwn qoume skhnc pou periqoun kinomena sqdia ta opoa la qoun kpoia genik sumperifor (p.q. metaknhsh, strof, lma klp.) all kai kpoia exeidikeumnh (p.q. o Roadrunner qei aut to spinrisma). Anlogo pardeigma enai oi efarmogc prosomowshc, p.q. prosomowsh diexagwgc agnwn stbou gia peiramatismoc se angkec se bohjhtik proswpik. Dete epshc kai th deterh skhsh thc prthc omdac g . Se aut ta problmata, tra, h mfash enai stic ontthtec. Oi diadikasec sqetzontai me autc kai ofeloun na tic sunodeoun.g Aplok prosomowsh polukatoikac.

4

'Ara, oi glssec pou qreiazmaste gia tic ulopoiseic ofeloun na prosfroun glwssikc domc oi opoec na epitrpoun orism ontottwn kai susqetism touc me tic diadikasec pou antistoiqon sth sumperifor touc (p.q. class sth C++). Epshc, prpei na epitrpoun to susqetism metax ontottwn (genikterh exeidkeush) msw tou mhqanismo thc klhronomikthtac. Oi ontthtec qoun mia exwterik sumperifor all den epiblloun sugkekrimnh eswterik anaparstash. Kal, loipn, enai o mhqanismc orismo twn ontottwn na epitrpei dikrish tou t enai qrhsimopoisimo ap ton xw ksmo kai tou t apotele apfash tou programmatist kai ra enai leptomreia ulopohshc. To detero prpei na egklwbzetai kai na mhn enai prosbsimo ap to uploipo prgramma. O mhqanismc tou egklwbismo knei aut th doulei. 'Etsi gnetai dunat an psa stigm na allxoume thn ulopohsh miac ontthtac qwrc na allxoume tpota sto uploipo prgramma, arke na diathrsoume th diepaf thc sumperiforc tou. Ed mwc prpei na sumplhrsw to exc: h qrsh tou antikeimenostrafoc programmatismo epektjhke kai se perioqc problhmtwn pou h antimetpis touc apaite diforec domc dedomnwn, pwc stobec, ourc klp. (p.q. leitourgik sustmata, epexergastc keimnou klp.). Akma, mporome na pome plon ti qei xefgei ki ap th qrsh tou gia orism nwn tpwn dedomnwn ki qei katalxei akma kai mia olklhrh diadikasa na snolo diadikasin na paketretai se mia klsh qrhsimopointac thn san mhqanism apkruyhc plhroforac (p.q. h prosggish thc Java). Elpzw me ta parapnw ta prgmata na xekajarzoun kaltera.

5

()

(procedural programming) : ... : Algol60, Algol68, FORTRAN, Pascal, C ..

(modular programming) (modules)

6

(. [Meyer]) : : Modula -2, Modula -3 ..

(data abstraction) , : stack (top, pop, push) : Ada, C++, Java ..

(Object oriented programming) , ,

7

:"Shape" (color, surface, rotation) /\ / \ / \ "Circle" "Triangle"

: Simula, C++, Java ..

8

ADA Smalltalk (1972) Modula -3 C++ (1980) Java C# SIMULA (1967): class , ,

9

, , (encapsulation) (information hiding) () (inheritance)

10

( ) ( ) (reusability) (stack of(X)) (lenghth of list of(X)) ( + , + ) (overloading)

11

: : : : : 1. 2. . ( ): .

12

(structural) : attributes, data members (behavioral) : member functions, procedures, methods/messages Object Oriented Design is the construction of software systems as structured collections of abstract data type implementations [Meyer] : stack: array student: struct () : :

13

C++ C++ (=increment of C) C Bjarne Stroustrup, 1980, Bell Labs C C++ 1983 OO Simula67 (C++ =? C + Simula67) (scope) - , , , , . . .

14

C++ int main() { return 0; }

#include using namespace std; int main() { cout