Forum

Virtual functions

This thread contains 1 reply, has 2 voices, and was last updated by Nitesh User Do not delete 5 years, 3 months ago.

  • Author Replies
    • #52555

      What are virtual functions?


    • #52558

      A virtual function is a member function which is declared within the base class and is re-defined (Override) by the derived class. When you allude to a derived class object utilizing a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.

      Following things are important to compose a C++ program with runtime polymorphism (utilization of virtual capacities)

      1) A base class and a derived class.
      2) A function with the same name in base class and derived class.
      3) A pointer or reference of base class type pointing or referring to an object of derived class.

      EXAMPLE: –

      #include<iostream>
      using namespace std;

      class Base {
      public:
      virtual void show() { cout<<” In Base \n”; }
      };

      class Derived: public Base {
      public:
      void show() { cout<<“In Derived \n”; }
      };

      int main(void) {
      Base *xy = new Derived;
      xy->show(); // RUN-TIME POLYMORPHISM
      return 0;
      }

      Output: In Derived


      Regards,
      Nitesh Bavishiya

Viewing 1 reply thread

You must be to reply to this thread.Please or . Registration is 100% free.

GET FREE CONSULTATION

Call Us Now For Free Consultation97370 05566

Our experts listen to you patiently and suggest you the right course after conducting a personality profile test. Register your interest below to schedule personality profile test for you.

SUBSCRIBE TO OUR NEWSLETTER

Copyright 2016-2024 Smart Mentors. All Rights Reserved.

Login

FORGOT PASSWORD

Sign up now to Become An Instructor

    Register your Interest

      cf7captchaRegenerate Captcha