Tässä opetusohjelmassa opitaan funktioiden ohittamisesta C ++: ssa esimerkkien avulla.
Kuten tiedämme, perintö on OOP: n ominaisuus, jonka avulla voimme luoda johdettuja luokkia perusluokasta. Johdetut luokat perivät perusluokan ominaisuudet.
Oletetaan, että sama funktio on määritelty sekä johdetussa luokassa että perusluokassa. Jos kutsumme tätä funktiota käyttämällä johdetun luokan objektia, johdetun luokan funktio suoritetaan.
Tätä kutsutaan funktion ohittavaksi C ++: ssa. Johdetun luokan funktio ohittaa perusluokan funktion.
Esimerkki 1: C ++ -toiminnon ohittaminen
// C++ program to demonstrate function overriding #include using namespace std; class Base ( public: void print() ( cout << "Base Function" << endl; ) ); class Derived : public Base ( public: void print() ( cout << "Derived Function" << endl; ) ); int main() ( Derived derived1; derived1.print(); return 0; )
Tuotos
Johdettu funktio
Tässä sama funktio print()
määritellään molemmissa Base
ja Derived
luokissa.
Joten, kun me kutsumme print()
päässä Derived
objektin derived1, The print()
mistä Derived
suoritetaan ohittamalla toimintoa Base
.

Käytä ohitettua toimintoa C ++: ssa
Pääsemme perusluokan ohitettuun toimintoon käytämme laajuuden tarkkuusoperaattoria ::
.
Voimme myös käyttää ohitettua toimintoa käyttämällä perusluokan osoitinta osoittamaan johdetun luokan objektille ja kutsumalla sitten funktio kyseisestä osoittimesta.
Esimerkki 2: C ++ Access Overridden -toiminto perusluokkaan
// C++ program to access overridden function // in main() using the scope resolution operator :: #include using namespace std; class Base ( public: void print() ( cout << "Base Function" << endl; ) ); class Derived : public Base ( public: void print() ( cout << "Derived Function" << endl; ) ); int main() ( Derived derived1, derived2; derived1.print(); // access print() function of the Base class derived2.Base::print(); return 0; )
Tuotos
Johdettu funktio Perusfunktio
Tässä tämä lausunto
derived2.Base::print();
käyttää print()
Base-luokan toimintoa.

Esimerkki 3: C ++ -puhelun ohitettu toiminto johdetusta luokasta
// C++ program to call the overridden function // from a member function of the derived class #include using namespace std; class Base ( public: void print() ( cout << "Base Function" << endl; ) ); class Derived : public Base ( public: void print() ( cout << "Derived Function" << endl; // call overridden function Base::print(); ) ); int main() ( Derived derived1; derived1.print(); return 0; )
Tuotos
Johdettu funktio Perusfunktio
Tässä ohjelmassa olemme kutsuneet ohitetun funktion Derived
itse luokan sisällä.
class Derived : public Base ( public: void print() ( cout << "Derived Function" << endl; Base::print(); ) );
Huomaa koodi Base::print();
, joka kutsuu ohitetun toiminnon Derived
luokan sisällä .

Esimerkki 4: C ++ - puhelun ohitettu toiminto osoittimen avulla
// C++ program to access overridden function using pointer // of Base type that points to an object of Derived class #include using namespace std; class Base ( public: void print() ( cout << "Base Function" << endl; ) ); class Derived : public Base ( public: void print() ( cout << "Derived Function"
Output
Base Function
In this program, we have created a pointer of
Base
type named ptr. This pointer points to the Derived
object derived1.
// pointer of Base type that points to derived1 Base* ptr = &derived1;
When we call the
print()
function using ptr, it calls the overridden function from Base
.
// call function of Base class using ptr ptr->print();
This is because even though ptr points to a
Derived
object, it is actually of Base
type. So, it calls the member function of Base
.
In order to override the
Base
function instead of accessing it, we need to use virtual functions in the Base
class.