Bu eğitimde, örnekler yardımıyla C ++ 'da işlev geçersiz kılma hakkında bilgi edineceğiz.
Bildiğimiz gibi, miras, bir temel sınıftan türetilmiş sınıflar oluşturmamıza izin veren bir OOP özelliğidir. Türetilmiş sınıflar, temel sınıfın özelliklerini devralır.
Diyelim ki, aynı işlev hem türetilmiş sınıfta hem de temel sınıfta tanımlanmıştır. Şimdi bu işlevi türetilmiş sınıfın nesnesini kullanarak çağırırsak, türetilmiş sınıfın işlevi çalıştırılır.
Bu, C ++ ' da işlevi geçersiz kılma olarak bilinir . Türetilmiş sınıftaki işlev, temel sınıftaki işlevi geçersiz kılar.
Örnek 1: C ++ İşlevini Geçersiz Kılma
// 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; )
Çıktı
Türetilmiş Fonksiyon
Burada, aynı işlevi print()
hem de tanımlandığı Base
ve Derived
sınıfları.
Yani, biz çağırdığınızda print()
gelen Derived
, nesne derived1 print()
gelen Derived
işlevi geçersiz kılarak yürütülür Base
.

C ++ 'da Geçersiz Kılınan Erişim Fonksiyonu
Temel sınıfın geçersiz kılınan işlevine erişmek için kapsam çözümleme operatörünü kullanıyoruz ::
.
Ayrıca, türetilmiş sınıfın bir nesnesini işaret etmek için temel sınıfın bir göstericisini kullanarak ve ardından bu işaretçiden işlevi çağırarak geçersiz kılınan işleve erişebiliriz.
Örnek 2: Temel Sınıfa C ++ Erişimi Geçersiz Kılınan İşlevi
// 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; )
Çıktı
Türetilmiş Fonksiyon Temel Fonksiyonu
İşte bu ifade
derived2.Base::print();
print()
Base sınıfının işlevine erişir .

Örnek 3: Türetilmiş Sınıftan C ++ Çağrısı Geçersiz Kılınan İşlevi
// 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; )
Çıktı
Türetilmiş Fonksiyon Temel Fonksiyonu
Bu programda, Derived
sınıfın kendi içinde geçersiz kılınan işlevi çağırdık .
class Derived : public Base ( public: void print() ( cout << "Derived Function" << endl; Base::print(); ) );
Sınıf Base::print();
içinde geçersiz kılınan işlevi çağıran koda dikkat edin Derived
.

Örnek 4: İşaretçi Kullanarak C ++ Çağrı Geçersiz Kılma İşlevi
// 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.