What is out put of C++ 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.          }


  9. };


  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.          }


  19. };


  21. int main(int argc, char *argv[])

  22. {

  23.      base *bptr = new derived;

  24.      bptr->display();


  26.       return 0;

  27. }

Copyright GeekInterview.com

Questions by ak.nextptr   answers by ak.nextptr

Showing Answers 1 - 12 of 12 Answers


  • May 1st, 2012

Derived class display with i = 10

Shobhit Chaturvedi

  • May 12th, 2012

dervie class function display

  Was this answer useful?  Yes

saum almas

  • May 30th, 2012

Derived class display with i = 10

  Was this answer useful?  Yes


  • 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

  Was this answer useful?  Yes


  • 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

  Was this answer useful?  Yes


  • 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.


  • Aug 9th, 2012

it will be 20

  Was this answer useful?  Yes


  • 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.

  Was this answer useful?  Yes

rohit suri

  • Sep 11th, 2014

Base class display method will called

  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