GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 450 of 453    Print  
Base Class Pointer with Derived Object
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?


  
Total Answers and Comments: 3 Last Update: September 22, 2009     Asked by: dilzmail 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: sarojkumar21.panda
 

Which version of the function to be called is based upon the type of object pointed by the pointer.This determination of call is made at run time.As your question says the pointer is pointed to the derived object.So the call will be based upon the object pointed and in ur case it is the derived class function.

Execute the following programm and you will be clear.

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

using namespace std;
class base
{
      public:
             virtual void display()
             {
                     cout<<"display function in base class"<<endl;
                    
                     }
};

class derived:public base
{
      public:
            
             void display()
             {
                  cout<<"dislay function in derived class"<<endl;
                  }
                  };
                 
                 
                 
                  int main()
                  {
                      base *bptr;
                      derived d1;
                      bptr=&d1;
                      bptr->display();
                      getch();
                      return 0;
                      }
                 


ans:;dislay function in derived class



Above answer was rated as good by the following members:
newlife, sandeep549
September 01, 2009 12:43:07   #1  
sarojkumar21.panda Member Since: September 2009   Contribution: 3    

RE: Base Class Pointer with Derived Object

Which version of the function to be called is based upon the type of object pointed by the pointer.This determination of call is made at run time.As your question says the pointer is pointed to the derived object.So the call will be based upon the object pointed and in ur case it is the derived class function.

Execute the following programm and you will be clear.

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

using namespace std;
class base
{
public:
virtual void display()
{
cout<<"display function in base class"<<endl;

}
};

class derived:public base
{
public:

void display()
{
cout<<"dislay function in derived class"<<endl;
}
};



int main()
{
base *bptr;
derived d1;
bptr &d1;
bptr->display();
getch();
return 0;
}



ans:;dislay function in derived class


 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
September 17, 2009 14:16:33   #2  
newlife Member Since: July 2009   Contribution: 4    

RE: Base Class Pointer with Derived Object
Whenever any class has virtual function then the binding is delayed till run time.
 
Is this answer useful? Yes | No
September 22, 2009 14:20:52   #3  
llifeinus Member Since: September 2009   Contribution: 1    

RE: Base Class Pointer with Derived Object
As explained you have assigned the base class pointer to the derived class object the derived class method will get called.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape