Gice

Technology and General Blog

In this write-up, we will deal with purpose overriding in the C++ programming language. C++ supports runtime polymorphism.

In functionality overriding, the youngster course redefines the perform which is currently described in its mum or dad course.

It enables the programmer to present derived course-particular operation (even even though it is defined in the base course).

Now, we will see a handful of illustrations and comprehend the perform overriding thought in C++.

Illustration-1

I have described two classes in the under instance code – a base class and a derived course. The base course has a member purpose, i.e., disp(). The “Derived_Class” is inherited from the “Base_Course.” The “disp()” perform is current in “Base_Class” and then redefined in the “Derived_Course.”

In the main() purpose, an object of Derived_Class, i.e., “d,” is made. Then, we phone the disp() purpose on the derived course item, so the purpose in the derived class will be invoked. The base class variation is dismissed in this article. You can see below the output as – “disp() function in the derived course.”

#consist of

applying namespace std

//foundation class
class Foundation_Course

general 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 will see how to deliberately call the base class version of the function through the derived class object. In the derived class function definition, we use the scope resolution operator [::] to call the base class version of the function.

#include

making use of namespace std

class Base_Course

general 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 another example of function overriding. In this example, we have created a parent class – Animal and two derived classes – Duck and Dog. The base class, i.e., the Animal class, has a member function sound().

In the derived class/child class, i.e., Dog, we have redefined the same function, i.e., sound(), to override its base class definition. Similarly, in the other derived class, i.e., Duck, we have redefined the same function, i.e., sound().

In the main() function, we have created the “dog” object of “Dog” and “duck” of “Duck.” So, now when we call the sound() function for dog and duck, the derived class version of the sound() function will call. You can see the output of the program as shown below. So, with the help of the function overriding, we can invoke the derived class-specific function.

#include

employing namespace std

class Animal

general public:
    void audio()
   
        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

In this article, I have explained function overriding in C++. The C++ language supports runtime polymorphism. Function overriding helps to achieve the run time polymorphism in C++. This article looked at the concept of function overriding and how to achieve runtime polymorphism using function overriding.

Leave a Reply

Your email address will not be published. Required fields are marked *