Virtual Method

Base class has some virtual method and derived class has a method with the same name. if we initialize the base class pointer with derived object, calling of that virtual method will result in which method being called?

Questions by chhavi1440

Showing Answers 1 - 6 of 6 Answers

hydsarema

  • Nov 25th, 2008
 

derived function is called
to demonstrate  this is the foll program


#include<iostream.h>
#include<conio.h>

class base
  {
     public: virtual void vfun()
      {
        cout<<"this is base virtual function calling";
      }
  };

  class derived1:public base
     {
       public:void vfun()
       {
  cout<<"this is derived function calling";
       }
     };


     void main()
     {
       clrscr();

       base *p,b;//base class pointer is created

       derived1 ob1;

       p=&ob1;//initialise base class pointer with derived class

       p->vfun();//derived function is called

       getch();
     }
    /* output
     this is derived function calling*/

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions