V tomto tutoriálu se pomocí příkladů dozvíme o přepsání funkcí v C ++.
Jak víme, dědičnost je vlastnost OOP, která nám umožňuje vytvářet odvozené třídy ze základní třídy. Odvozené třídy dědí funkce základní třídy.
Předpokládejme, že stejná funkce je definována jak v odvozené třídě, tak v založené třídě. Nyní, když tuto funkci zavoláme pomocí objektu odvozené třídy, provede se funkce odvozené třídy.
Toto je známé jako přepsání funkcí v C ++. Funkce v odvozené třídě přepíše funkci v základní třídě.
Příklad 1: Přepsání funkce 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ýstup
Odvozená funkce
Zde je stejná funkce print()
definována v obou Base
a Derived
třídách.
Když tedy voláme print()
z Derived
objektu derived1, je print()
from Derived
provedeno přepsáním funkce in Base
.
![](https://cdn.wiki-base.com/1143898/c_function_overriding.png.webp)
Přístup k přepsané funkci v C ++
Pro přístup k přepsané funkci základní třídy používáme operátor rozlišení oboru ::
.
K přepsané funkci můžeme také přistupovat pomocí ukazatele základní třídy, který ukazuje na objekt odvozené třídy a následným voláním funkce z tohoto ukazatele.
Příklad 2: Přepsaná funkce přístupu C ++ k základní třídě
// 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ýstup
Odvozená funkce Základní funkce
Tady, toto prohlášení
derived2.Base::print();
přistupuje k print()
funkci třídy Base.
![](https://cdn.wiki-base.com/1143898/c_function_overriding_2.png.webp)
Příklad 3: Přepsaná funkce volání C ++ z odvozené třídy
// 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ýstup
Odvozená funkce Základní funkce
V tomto programu jsme nazvali přepsanou funkci uvnitř samotné Derived
třídy.
class Derived : public Base ( public: void print() ( cout << "Derived Function" << endl; Base::print(); ) );
Všimněte si kódu Base::print();
, který volá přepsanou funkci uvnitř Derived
třídy.
![](https://cdn.wiki-base.com/1143898/c_function_overriding_3.png.webp)
Příklad 4: Přepsaná funkce volání C ++ pomocí ukazatele
// 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.