GeekInterview.com
Series: Subject: Topic:
Question: 13 of 267

What is out put of C++ code?

Code
  1. class base
  2. {
  3.   public:
  4.          virtual void display(int i = 10)
  5.          {
  6.            cout<<"Base class display with i = "<<i<<endl;
  7.          }
  8.  
  9. };
  10.  
  11. class derived : public base
  12. {
  13.   public:
  14.           void display(int i = 20)
  15.          {
  16.            cout<<"Derived class display with i = "<< i <<endl;
  17.          }
  18.  
  19. };
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23.      base *bptr = new derived;
  24.      bptr->display();
  25.        
  26.       return 0;
  27. }
  28.  
Copyright GeekInterview.com

Asked by: ak.nextptr | Member Since Jan-2012 | Asked on: Mar 21st, 2012

View all questions by ak.nextptr   View all answers by ak.nextptr

Showing Answers 1 - 11 of 11 Answers
Vip

Answered On : May 1st, 2012

Derived class display with i = 10

Yes  3 Users have rated as useful.
  
Login to rate this answer.
Shobhit Chaturvedi

Answered On : May 12th, 2012

dervie class function display

  
Login to rate this answer.
bluesky28

Answered On : May 15th, 2012

View all answers by bluesky28

Derived class display with i = 20

Yes  1 User has rated as useful.
  
Login to rate this answer.
windowcleaner

Answered On : May 30th, 2012

View all answers by windowcleaner

Derived class display with i = 10

  
Login to rate this answer.
saum almas

Answered On : May 30th, 2012

Derived class display with i = 10

  
Login to rate this answer.
Deepak

Answered On : Jun 13th, 2012

Derived class display with i=10 because in virtual functions do pass the default arguments if you are passing then made both versions of function with same default argument

  
Login to rate this answer.
Mahesh

Answered On : Jun 20th, 2012

Derived class display method with i=20, because assigning derived object to base class reference. so derived class display method will call

  
Login to rate this answer.
Teodor

Answered On : Aug 6th, 2012

It indeed displays "Derived class i=10". What happens is that through polymorphism, the base * obj = new derived() object can have access to the body of the overridden method, but its signature containing the default value for the "i" parameter is taken from the base class. That's because of the run-time vtable mechanism: the function is called for a base object, but the vtable fetches the implementation for the overridden one, from the derived class. Hence the odd output.

  
Login to rate this answer.
abhimanyu

Answered On : Aug 9th, 2012

it will be 20

  
Login to rate this answer.
panreth

Answered On : Aug 23rd, 2012

View all answers by panreth

Answer is : "derived class display with i=10"

  
Login to rate this answer.
reddy-

Answered On : Apr 10th, 2013

Overriding function default argument takes more precedence ,Hence based class default argument 10 will be initialized to i.So i value holds 10 will be printed for this call with derived count statement.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.