Interview Question for Developer Program Engineers


Country: United States
Interview Type: Written Test




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

Two times....
if we add virtual in front of base in d1..
then only once !!

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

yes. this is the answer

- shanelinnz August 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agreed

- Anonymous August 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please explain.

- AlphaBeta August 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you see code more closely...,,
The class d1 & d2 both inherit base...
but only one is inheriting with virtual keyword >>Thats why the concept of virtual base class is not implemented...
hence...both d1 & d2 will have one-one copy of base,,,so two times...

- gnxtstar007 August 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks gnxtstar007. Now understood it.

- AlphaBeta August 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

One Time Bcoz Virtual Is Overide The Base Class...

- Shaikh Aqueel September 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Answer is 2 . but a slight changed reason here:
when a class D2 inherits base class B virtually; derived class of D2 i.e. DD will be responsible to call the B's constructor. So B's constructor will be called twice: one by D1 and other by DD.
Please comment if i am wrong........

- praveen gupta September 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Why two times and not three times? Are we not considering class D2?
Please explain.

- Anonymous August 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

It's 2 times. If virtual is placed in front of D1 while deriving from Base, then it is 1. Because there will be no duplicates.

- suhaskulkarni100 September 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can anybody explain the reason? the behavior change when we change the order. Means inherit base for D1 virtual and inherit base for D2 non virtual. Or change the order when inheriting D1 and D2 for DD!

- Anonymous August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tried to add the link to msdn's article, but the site is not allowing me to do that.
search for "virtual base classes msdn" in google and click the first link. That might help you. GOOD LUCK

- lsutigers August 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<iostream>
using namespace std;
class A{
  public:
  A(){
    cout << "In A\n";
  }
};

class vB:virtual public A{
  public:
  vB(){
    cout << "In vB\n";
  }
};

class nvB: public A{
  public:
  nvB(){
    cout << "In nvB\n";
  }
};

class D1: public vB, public nvB{// A vB A nvB D1 , first base virtual
  public:
  D1(){
    cout << "In D1\n";
  }
};

class D2: public nvB, public vB{// A A nvB vB D2 , first base not virtual
  public:
  D2(){
    cout << "In D2\n";
  }
};

main(){
  D1 d1;
  D2 d2;
}

class D1: public vB, public nvB
Here output :
A (result of call to A() from D1)
vB (Does not call A() because vB is virtual)
A (result of call to A() from nvB, which is not virtual)
nvB
D1

class D2: public nvB, public vB
Output:
A (result of call to A() from D2)
A (result of call to A() from nvB, which is not virtual)
nvB
vB (Does not call A() because vB is virtual)
D2

Note:
Case 1: intermediate base classes are virtual and non-virtual
The most derived class(here D1 / D2) call the top-most Base class(here A), when any one of the intermediate base classes(here vB / nvB) is virtual.
Then non-virtual Base class also call the top-most Base
Case 2:
When all the intermediate base classes(here vB / nvB) are virtual, the most derived class(D1 / D2) will only call top-most base class(A)
Case 3:
When all the intermediate base classes(here vB / nvB) are non-virtual, then intermediate base classes call the top-most base class. In this case the most derived class(D1 / D2) does not call the top-most base class.

- Abhijit Saha March 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

One Time Bcoz Virtual Overide The Base Class,,

- Shaikh Aqueel September 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

2 times.

- Nitin Gupta August 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, @nitingupta180 is right . if we create the object of DD class then Base constructor will called twice(2).

- Aalok August 14, 2012 | Flag


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