У овом упутству ћемо научити о замени функција у Ц ++ уз помоћ примера.
Као што знамо, наслеђивање је особина ООП-а која нам омогућава да креирамо изведене класе од основне класе. Изведене класе наслеђују особине основне класе.
Претпоставимо да је иста функција дефинисана и у изведеној и у базираној класи. Ако ову функцију позовемо помоћу објекта изведене класе, извршава се функција изведене класе.
Ово је познато као замена функције у Ц ++. Функција у изведеној класи поништава функцију у основној класи.
Пример 1: Замена функције Ц ++
// 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; )
Оутпут
Изведена функција
Овде је иста функција print()
је дефинисана у оба Base
и Derived
класама.
Дакле, када позивамо print()
из Derived
објекта изведеног1, print()
из Derived
се извршава надјачавањем функције у Base
.

Приступ замењеној функцији у Ц ++
Да бисмо приступили надјачаној функцији основне класе, користимо оператер резолуције опсега ::
.
Такође можемо приступити надјачаној функцији тако што ћемо помоћу показивача основне класе указати на објекат изведене класе, а затим позвати функцију из тог показивача.
Пример 2: Ц ++ функција замењена приступом основној класи
// 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; )
Оутпут
Изведена функција Основна функција
Ево, ова изјава
derived2.Base::print();
приступа print()
функцији класе Басе.

Пример 3: Функција замењена позивом Ц ++ из изведене класе
// 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; )
Оутпут
Изведена функција Основна функција
У овом програму позвали смо надјачану функцију унутар саме Derived
класе.
class Derived : public Base ( public: void print() ( cout << "Derived Function" << endl; Base::print(); ) );
Обратите пажњу на код Base::print();
који позива замењену функцију унутар Derived
класе.

Пример 4: Функција замене позива Ц ++ помоћу показивача
// 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.