We'll look into function overriding in the C++ programming language in this post. Polymorphism at runtime is supported in C++. ...
We'll look into function overriding in the C++ programming language in this post. Polymorphism at runtime is supported in C++.
When a child class redefines a function that is already defined in its parent class, this is known as function overriding.
It allows the programmer to add functionality to derived classes (even though it is defined in the base class).
Now we'll look at a few examples to help us comprehend the notion of function overriding in C++.
Example-1
In the example code below, I've defined two classes: a base class and a derived class. The base class includes a member function, disp (). The "Derived Class" is a subclass of the "Base Class." The "disp()" function appears in "Base Class" and is then redefined in "Derived Class."
An object of Derived Class, i.e., "d," is created in the main() function. Then we call the disp() function on the derived class object, which invokes the function in the derived class. The base class version is ignored in this case. The output is shown below as – "disp() function in the derived class."
[#includeusing namespace std; //base class class Base_Class { public: void disp() { cout << "disp() function in base class." << endl; } }; //derived class class Derived_Class:public Base_Class { public: void disp() { cout << "disp() function in derived class" << endl; } }; int main() { Derived_Class d; d.disp(); //disp() function on derived class object return 0; }]
Example-2
In this example, we'll see how to use the derived class object to call the base class version of a function. We use the scope resolution operator [::] in the derived class function definition to call the base class version of the function.
[#includeusing namespace std; class Base_Class { public: void disp() { cout << "disp() function in base class." << endl; } }; class Derived_Class:public Base_Class { public: void disp() { Base_Class::disp(); //Call the base class version of disp() } }; int main() { Derived_Class d; d.disp(); return 0; }]
Example-3
This is yet another instance of function overriding. In this example, we've created a parent class called Animal, as well as two derived classes called Duck and Dog. The base class, Animal, has a member function called sound ().
We redefined the same function, sound(), in the derived class/child class, Dog, to override its base class definition. Similarly, we have redefined the same function, i.e., sound, in the other derived class, Duck ().
We created the "dog" object of "Dog" and the "duck" object of "Duck" in the main() function. The derived class version of the sound() function will now be called when we call the sound() function for dog and duck. The program's output can be seen in the image below. As a result, we may use function overriding to call the derived class-specific function.
[#includeusing namespace std; class Animal { public: void sound() { cout << "Animal sound!" << endl; } }; //Derived class – Dog class class Dog: public Animal { public: void sound() { cout << "Dog sound - bark." << endl; } }; //Derived class – Duck class class Duck: public Animal { public: void sound() { cout << "Duck sound - quack." << endl; } }; int main() { Dog dog; Duck duck; dog.sound(); //sound() of dog duck.sound(); //sound() of duck return 0; }]
Conclusion
I've covered function overriding in C++ in this article. Runtime polymorphism is supported by the C++ programming language. In C++, function overriding aids in achieving run-time polymorphism. This article discussed the notion of function overriding and how to use it to achieve runtime polymorphism.
COMMENTS