Bloomberg LP Interview Question
Software Engineer / DevelopersAgreed.
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.
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.
#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
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 ()
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 ()
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