Interview Question


Country: India




Comment hidden because of low score. Click to expand.
5
of 5 vote

Output is:
der 10 func
der 1 func

The explanation goes as follows:

The default argument is a compile-time feature i.e. the substitution of default arguments is done at compile-time.
For this reason, obviously, there's no way the default argument mechanism for member functions can depend on the dynamic (i.e. run-time) type of the object. It always depends on static (i.e. compile-time) type of the object.

The call you wrote in your code sample[bb->func(10)] is immediately interpreted by the compiler as bb->func(10,10) regardless of anything else.

- Aashish July 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

shondik, can you elaborate on that? Since I am not sure I understand the slightly modified version below.

class base{
public:
virtual void func(int i,int j=10)
{
cout<<"base func"<<endl;
}
};


class der:public base
{
public:
void func(int i,int j=5)
{
cout<<"der "<<j<<" func"<<endl;
}
};


int main()
{
der dd;
base *bb = new der();
bb->func(10);
dd.func(10,1);
dd.func(10,5);
}

- Oliver March 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

der 10 func
der 1 func

- Harsha July 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

first out put : base func

second output : der 1 func

- sam July 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please check the output before writing anything.

- Aashish July 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is there any way one can call the "base func" to get printed?

- jaigupta July 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sure - make the base func non-virtual

- Tom July 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

der 10 func
der 1 func

- deveshucer August 06, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More