Bloomberg LP Interview Question for Software Engineer / Developers






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

Yes, pure virtual methods can have implementations. One reason is so that subclassed methods within the same hierarchy can be consistent. (Terrible question.)

- Anonymous July 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

Agreed.

pure virtual function and function definition are in a way independent concept.

We make pure virtual function just to ensure that no object of the class can be created. Such a class is called "ABSTRACT CLASS". one can make all the functions pure virtual (by putting =0) and still give definition to all. OR one can make just single function as pure virtual to ensure that object of this class is not created.


Since object can not be created , we can be confident that this member function will not be called ever. Hence definition is optional. However, derived class objects can be created and hence definition is MUST unless one is making derived class also an ABSTRACT CLASS.


Now the question is why and in what situation we still might want to give the function a definition??
Answer is simple. To have "Code Reuse". If there are 100 classes derived from this class and all those classes same definition of this function, then why to define it individually in all classes. Why not make this function , pure virtual and still give it the definition that will be reused by derived classes.

- vipin September 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

if all derived classes need same implementation, why make it pure at all?

- s October 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

let me re phrase the Question, can you think of any situation where this is mandotary.

- indian July 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pure virtual destructors must always have default implementation.

- fiddler.g July 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

not necessary !!

- vipin September 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

First of all, this will not work:

void A::foo(void )={};

It will be considered as function redeclaration.

Second, if the base class destructor is pure virtual, you cannot make a derived class object without explicit definition of the base class destructor.

- sergey.a.kabanov January 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes , fiddler.g is correct .
More info at gotw.ca/gotw/031.htm

- Anonymous July 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Used when:
void main() 
{ 

Base * ptr;
Derived e1;
ptr=e1;
ptr->virtualFunction();

}

Hence in above case virtualFunction needs to be present in both the classes and the one in base should be virtual so that one from derived is called... assuming thats the need.

Because Compiler would first go to base class since the pointer is of types base but if it doesn't find the function present
it will give error.
Only when it sees the function is present but virtual it later looks up for the derived class to have the necessary function.
Hence removing the function will give error.

- unicorn July 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Derived classes that implement this pure virtual function may call this implementation smwhere in their code.

- Brahma September 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pure Virtual destructors need an implementation.

- Richa Aggarwal November 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string.h>
using namespace std;
class A{
public:
	virtual int foo(void)=0;
	virtual ~A()=0;
};
int A::foo(void){
	int i=1;
	cout<<"Pure virtual function:: foo"<<endl;
}
A::~A(){
	cout<<"Pure virtual destructor. Base"<<endl;
}
class B:public A{
public:
	virtual int foo(void){
		cout<<"Pure virtual implementation in base"<<endl;
	}
	virtual ~B(){
		cout<<"Derived destructor"<<endl;
	}
};
int main(){
	A* ptr;
	ptr = new B();
	B b1;
	delete ptr;
}

Output:
Derived destructor
Pure virtual destructor. Base
Derived destructor
Pure virtual destructor. Base

- blueskin.neo November 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Body of pure virtual function is a must when this function is destructor.
It is also reasonable: when destroying an instance of a derived class, you also want the abstract base object to be destroyed as well.

class Base
{
  virtual ~Base () = 0;
};

class Derived: public Base
{
  virtual ~Derived () {}
};

int main ()
{
  Derived d;
}

Link error: unresolved Base::~Base ()

- Leo March 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Body of pure virtual function is a must when this function is destructor.
It is also reasonable: when destroying an instance of a derived class, you also want the abstract base object to be destroyed as well.

class Base
{
  virtual ~Base () = 0;
};

class Derived: public Base
{
  virtual ~Derived () {}
};

int main ()
{
  Derived d;
}

Link error: unresolved Base::~Base ()

- Leo March 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This program will throw error.

class A
{
public:
void foo(void) = 0;

};

A::foo(void )={};

'A::foo' : pure specifier or abstract override specifier only allowed on virtual function
'void A::foo(void)' : member function redeclaration not allowed

- Keyur May 12, 2013 | 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