V tomto tutoriále sa pomocou príkladov dozvieme o prepísaní funkcií v C ++.
Ako vieme, dedičnosť je vlastnosť OOP, ktorá nám umožňuje vytvárať odvodené triedy zo základnej triedy. Odvodené triedy dedia vlastnosti základnej triedy.
Predpokladajme, že rovnaká funkcia je definovaná v odvodenej aj v základnej triede. Teraz, ak túto funkciu zavoláme pomocou objektu odvodenej triedy, vykoná sa funkcia odvodenej triedy.
Toto sa v C ++ označuje ako prepísanie funkcie . Funkcia v odvodenej triede prepíše funkciu v základnej triede.
Príklad 1: Prepísanie funkcie C ++
// 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; )
Výkon
Odvodená funkcia
Tu je rovnaká funkcia print()
definovaná v obidvoch triedach Base
a Derived
triedach.
Takže, keď voláme print()
z Derived
objektu odvodené1, funkcia print()
od Derived
sa vykoná prepísaním funkcie v Base
.
Prepísaná funkcia prístupu v C ++
Na prístup k prepísanej funkcii základnej triedy používame operátor rozlíšenia rozsahu ::
.
K prepísanej funkcii môžeme pristupovať aj tak, že použijeme ukazovateľ základnej triedy, aby sme ukazovali na objekt odvodenej triedy, a potom funkciu zavoláme z tohto ukazovateľa.
Príklad 2: C ++ prepísaná funkcia prístupu k základnej triede
// 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; )
Výkon
Odvodená funkcia Základná funkcia
Tu toto vyhlásenie
derived2.Base::print();
pristupuje k print()
funkcii triedy Base.
Príklad 3: Prepísaná funkcia volania C ++ z odvodenej triedy
// 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; )
Výkon
Odvodená funkcia Základná funkcia
V tomto programe sme nazvali prepísanú funkciu vo vnútri samotnej Derived
triedy.
class Derived : public Base ( public: void print() ( cout << "Derived Function" << endl; Base::print(); ) );
Všimnite si kód Base::print();
, ktorý volá prepísanú funkciu vo vnútri Derived
triedy.
Príklad 4: Prepísaná funkcia volania C ++ pomocou ukazovateľa
// 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.