Qhov txawv ntawm Kev Tshaj Tawm thiab Kev Tshaj Tawm hauv C

Cov txheej txheem:

Qhov txawv ntawm Kev Tshaj Tawm thiab Kev Tshaj Tawm hauv C
Qhov txawv ntawm Kev Tshaj Tawm thiab Kev Tshaj Tawm hauv C

Video: Qhov txawv ntawm Kev Tshaj Tawm thiab Kev Tshaj Tawm hauv C

Video: Qhov txawv ntawm Kev Tshaj Tawm thiab Kev Tshaj Tawm hauv C
Video: Koj Hais Dab Tsi?/Suabci Kwm & Ab Meej Ham/Official MV 2023 2024, Hlis ntuj nqeg
Anonim

Qhov Tseem Ceeb Qhov Sib Txawv - Overriding vs Overloading hauv C

Qhov sib txawv tseem ceeb ntawm overriding thiab overloading nyob rau hauv C yog qhov khi ntawm overridden txoj kev hu rau nws cov ntsiab lus tshwm sim ntawm runtime thaum lub binding ntawm overloaded txoj kev hu rau nws cov ntsiab lus tshwm sim ntawm compile lub sij hawm.

C yog hom lus programming dav dav tsim los ntawm Microsoft. Qhov txiaj ntsig tseem ceeb ntawm Cyog tias nws txhawb nqa Object Oriented Programming (OOP). Ib tug ncej ntawm OOP yog Polymorphism. Nws muab cov khoom kom muaj ntau tus cwj pwm. Muaj ob hom hauv Polymorphism hu ua overriding thiab overloading. Kab lus no tham txog qhov sib txawv ntawm txoj kev overriding thiab overloading hauv C.

Dab tsi yog Kev Tshaj Tawm hauv C?

Muaj ib lub tswv yim tseem ceeb hauv OOP yog qub txeeg qub teg. Nws yog siv cov cwj pwm thiab cov txheej txheem ntawm cov chav kawm uas twb muaj lawm. Nws txhim kho code reusability. Cov chav kawm uas twb muaj lawm yog lub hauv paus chav kawm, thiab chav kawm tshiab yog hu ua chav muab tau los. Nyob rau hauv overriding ntawm polymorphism, yuav tsum muaj ib tug hauv paus chav kawm ntawv thiab ib tug derived chav kawm ntawv. Kev khi ntawm txoj kev overridden hu rau lub ntsiab lus tshwm sim thaum lub sijhawm khiav. Ib qho piv txwv yog raws li hauv qab no.

Qhov txawv ntawm Overriding thiab Overloading hauv C
Qhov txawv ntawm Overriding thiab Overloading hauv C

Daim duab 01: C Program nrog Overriding

Raws li cov kev pab cuam saum toj no, cov chav kawm Shape yog cov hauv paus chav kawm, thiab nws muaj cov txheej txheem tso saib. Chav Rectangle thiab Daim duab peb sab yog muab los ntawm chav kawm. Cov chav kawm uas tau txais los no kuj muaj tib txoj kev qhia nrog lawv tus kheej siv.

Ua ntej, qhov siv tau hloov pauv obj yog tsim. Nws taw rau qhov khoom zoo. Yog li, cov qauv qhia ntawm Shape class yuav ua tiav. Tom qab ntawd, qhov kev siv cov ntsiab lus sib txawv rau lub Rectangle object. Yog li, cov txheej txheem zaub ntawm Rectangle chav kawm yuav ua tiav. Thaum kawg, qhov kev siv cov ntsiab lus sib txawv mus rau Daim duab peb sab khoom. Yog li, cov txheej txheem tso saib daim duab peb sab chav kawm yuav ua tiav. Cov txheej txheem qhia hauv chav kawm yog overridden los ntawm cov txheej txheem tso saib ntawm cov chav kawm muab tau.

Txoj kev khiav yog txiav txim siab ntawm lub sijhawm ua haujlwm. Chav kawm Shape yog sau nrog 'virtual' lo lus tseem ceeb. Cov chav kawm Rectangle thiab daim duab peb sab yog sau nrog 'override' lo lus tseem ceeb. Yog tias cov lus tseem ceeb no tsis siv, cov zis yuav luam tawm cov ntsiab lus ntawm cov qauv duab ntawm Shape class rau txhua tus.

Dab tsi yog overloading hauv C?

Nyob rau hauv overloading, ntau txoj kev muaj tib lub npe tab sis muaj qhov sib txawv. Cov tsis muaj peev xwm ua tau ntau hom. Cov txheej txheem kuj tuaj yeem muaj ntau tus lej sib txawv. Txoj kev overloading tshwm sim hauv tib chav kawm. Txoj kev khi ntawm cov txheej txheem overloaded rau lub ntsiab lus tshwm sim thaum sau ua ke. Xa mus rau qhov program C hauv qab no.

Qhov sib txawv tseem ceeb ntawm Overriding thiab Overloading hauv C
Qhov sib txawv tseem ceeb ntawm Overriding thiab Overloading hauv C

Daim duab 02: C Program nrog Overloading

Raws li txoj haujlwm saum toj no, chav kawm A muaj ob txoj hauv kev nrog tib lub npe hu ua sum. Lawv muaj ntau hom kev ntsuas. Hauv qhov kev zov me nyuam tseem ceeb, ib qho khoom ntawm A raug tsim. Qhov sum (2, 3) yuav hu ua sum method with integers. Tus lej (5.1, 7.94) yuav hu rau txoj kev suav nrog ob qhov txiaj ntsig. Ob txoj kev muaj tib lub npe thiab tib tus lej ntawm cov tsis. Tab sis cov hom parameter txawv. Txoj kev xav tau yog hu ua raws li. Overloading kuj tuaj yeem tshwm sim yog tias cov npe ntawm cov npe thiab cov hom tsis sib xws yog tib yam tab sis tus lej ntawm qhov tsis sib txawv.

Dab tsi yog qhov zoo sib xws ntawm Kev Tshaj Tawm thiab Kev Tshaj Tawm hauv C?

Ob qho tib si overriding thiab overloading hauv C yog hom polymorphism

Qhov txawv ntawm Kev Tshaj Tawm thiab Kev Tshaj Tawm hauv C yog dab tsi?

Kev Tshaj Tawm vs Kev Tshaj Tawm hauv C

Kev Tshaj Tawm hauv C yog los muab ib qho kev siv tshwj xeeb hauv cov txheej txheem muab los ntawm chav kawm rau ib txoj hauv kev uas twb muaj lawm hauv chav kawm. Kev Tshaj Tawm hauv C yog tsim ntau txoj hauv kev nrog tib lub npe nrog kev siv sib txawv.
Parameters
Nyob rau hauv C Overriding, txoj kev muaj tib lub npe, tib yam parameter thiab ib tug tib tus naj npawb ntawm parameters. Nyob rau hauv C Kev Tshaj Tawm, cov txheej txheem muaj tib lub npe tab sis muaj tus lej sib txawv lossis ntau hom kev txwv.
Nyob rau hauv C, overriding tshwm sim nyob rau hauv lub hauv paus chav kawm ntawv thiab cov derived chav kawm. Hauv C, overloading tshwm sim nyob rau hauv tib chav kawm.
Lub Sijhawm Ua Haujlwm
Kev khi ntawm txoj kev overridden hu rau nws lub ntsiab lus tshwm sim ntawm runtime. Kev khi ntawm txoj kev overloaded hu rau nws lub ntsiab lus tshwm sim ntawm lub sijhawm sib sau.
Kev hla dhau yog hu ua runtime polymorphism, dynamic polymorphism lossis lig khi. Kev Tshaj Tawm yog hu ua suav nrog lub sijhawm polymorphism, static polymorphism lossis kev khi ntxov.

Summary – Overriding vs Overloading in C

Kev Tshaj Tawm thiab Kev Tshaj Tawm yog ob hom polymorphism. Qhov sib txawv ntawm overriding thiab overloading nyob rau hauv Cyog qhov khi ntawm txoj kev overridden hu rau nws cov ntsiab lus tshwm sim ntawm runtime thaum lub khi ntawm txoj kev overloaded hu rau nws cov ntsiab lus tshwm sim ntawm compile lub sij hawm.

Pom zoo: