W tym samouczku nauczymy się zastępowania funkcji w C ++ na przykładach.
Jak wiemy, dziedziczenie jest cechą OOP, która pozwala nam tworzyć klasy pochodne z klasy bazowej. Klasy pochodne dziedziczą funkcje klasy bazowej.
Załóżmy, że ta sama funkcja jest zdefiniowana zarówno w klasie pochodnej, jak i w klasie bazowej. Jeśli teraz wywołamy tę funkcję za pomocą obiektu klasy pochodnej, zostanie wykonana funkcja klasy pochodnej.
Jest to znane jako przesłanianie funkcji w C ++. Funkcja w klasie pochodnej przesłania funkcję w klasie bazowej.
Przykład 1: przesłanianie funkcji 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; )
Wynik
Funkcja pochodna
Tutaj ta sama funkcja print()
jest zdefiniowana w obu Base
i Derived
klasach.
Tak więc, kiedy wywołujemy print()
z Derived
obiektu der1, print()
from Derived
jest wykonywany przez nadpisanie funkcji w Base
.

Dostęp do funkcji przesłoniętej w C ++
Aby uzyskać dostęp do przesłoniętej funkcji klasy bazowej, używamy operatora rozpoznawania zakresu ::
.
Możemy również uzyskać dostęp do przesłoniętej funkcji, używając wskaźnika klasy bazowej, aby wskazać obiekt klasy pochodnej, a następnie wywołując funkcję z tego wskaźnika.
Przykład 2: C ++ Access Overridden Function do klasy bazowej
// 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; )
Wynik
Pochodna funkcja bazowa funkcji
Tutaj to stwierdzenie
derived2.Base::print();
uzyskuje dostęp do print()
funkcji klasy Base.

Przykład 3: C ++ Call Przesłonięta funkcja z klasy pochodnej
// 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; )
Wynik
Pochodna funkcja bazowa funkcji
W tym programie nadpisaną funkcję nazwaliśmy wewnątrz Derived
samej klasy.
class Derived : public Base ( public: void print() ( cout << "Derived Function" << endl; Base::print(); ) );
Zwróć uwagę na kod Base::print();
, który wywołuje zastąpioną funkcję wewnątrz Derived
klasy.

Przykład 4: C ++ funkcja przesłonięta wywołanie przy użyciu wskaźnika
// 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.