Kev coj tus kheej kev ua qauv paradigm yog nthuav dav hauv txhua yam cuab yeej niaj hnub thiab cov lus hais rau kev tsim software. Kev lag luam tus qauv niaj hnub no yog lub hom phiaj-kawm ua lus C ++. Koj tuaj yeem tsim qhov piv txwv ntawm chav kawm hauv C ++ hauv ntau txoj kev sib txawv.
Nws yog qhov tsim nyog
C ++ compiler
Cov Lus Qhia
Kauj ruam 1
Instantiate cov chav kawm ua qhov pib sib txawv nyob rau hauv cov thaj tsam hauv nroog tau txhais los ntawm kev ua haujlwm, chav kawm hom, lossis kab lus thaiv. Siv cov lus tshaj tawm lossis qhov tseem ceeb txhais ntawm chav kawm ntawm qhov chaw raug xaiv hauv koj qhov kev kawm. Yog tias tsim nyog, hu ncaj qha rau ib tus kws tsim qauv nrog cov ntsuas. Tsim ib qho khoom siv kev siv cov cai zoo ib yam li cov hauv qab no: tsis lees paub CMyClass:: SomeMethod () {COtherClass oSomeObject1; // tsim kom tau ib qho khoom siv uas tsis yog tus tsim qauv COtherClass oSomeObject2 (1980, "Victor V. Vakchturov"); // tsim cov khoom siv siv tus tsim nrog cov ntsiab lus} Nco rau cov khoom ntawm cov chav kawm tsim nyob rau hauv txoj kev zoo sib xws, raws li rau lwm qhov pib-hloov pauv, tau faib rau ntawm pawg. Yog li no, thaum koj tawm ntawm qhov kev txiav txim siab thiab tshem tawm cov theem thav duab, qhov khoom yuav raug rhuav tshem (nrog rau kev hu mus rau destructor).
Kauj ruam 2
Tsim tus piv txwv ntawm chav kawm nyob rau hauv qhov heap siv tus neeg tshiab. Txhais tau qhov sib txawv ntawm hom pointer rau cov khoom ntawm chav kawm kom nrawm. Muab nws tus nqi uas yog txiaj ntsig ntawm kev ntsuas tus neeg ua haujlwm tshiab. Hu rau tus kws tsim nyog. Siv cov cim snippet zoo ib yam li cov hauv qab no: CSomeClass * poSomeObject; // txhais ntawm lub pointer rau cov khoom ntawm chav kawm CSomeClasspoSomeObject = tshiab CSomeClass; // Tsim kom muaj hom phiaj ntawm chav kawm CSomeClass * poSomeObject_2 = tshiab CSomeClass (111, "3V"); // tsim nrog hu rau tus kws tsim kho nrog cov kev txwv Thaum tsim cov khoom los ntawm hom no, lub cim xeeb faib cov txheej txheem txhais los ntawm tus neeg teb xov tooj tshiab yog siv (yog tias nws tsis dhau thiab nws tus kheej kev faib lub luag haujlwm tsis tau teeb tsa), yog li qhov chaw nyob ntawm yam khoom tshiab tsis tau paub ua ntej. Txhua yam khoom tsim nyob rau hauv txoj kev no yuav tsum raug qhia meej meej tshem tawm siv tus neeg teb xov tooj rho tawm.
Kauj ruam 3
Tsim ua piv txwv ntawm chav kawm uas siv tus neeg teb xov tooj tshiab ntawm kev faib cov kheej ntawm tus kheej ntawm lub cim xeeb. Siv cov cai zoo ib yam li cov hauv qab no: khoob * p0 = malloc (sizeof (CSomeClass)); // nco tseg lub chaw khoob * p1 = malloc (sizeof (CSomeClass)); // nco tseg lub zog tshiab (p0) CSomeClass; // pib qhov khoom ntawm lub cim xeeb tseg (tus qauv tsim lub qub) tshiab (p1) CSomeClass (111, "abc"); Ua ntej cov khoom tsim los ntawm cov qauv no, koj yuav tsum qhia meej lawv lub destructor: ((CSomeClass *) p0) -> ~ (); Tsim cov khoom ntawm txoj kev no yog siv nyob rau hauv cov thawv ntim qauv ntawm ntau lub tsev qiv ntawv (xws li STL).