Bloomberg LP Interview Question
Financial Software DevelopersFails at
A(int ii , int jj) :i(ii),j(ii){}
. Pure virtual function can not be defined. To make it work, a class should be derived from Base class and then it can be over ridden.
As far my understanding goes, it wont compile.
Once the method is defined to be pure virtual in base class, it IS pure virtual function. all derived classes should implement it.
(Wierd enough, compiler wont complain abt definition of the function even after its declaration to be pure virtual.
Pure virtual function can be defined(but not inside the class). It can be accessed using the scope resolution operator from derived object as well.
If the derived class is not over riding the pure virtual function then derived class will become abstract (like the base).
The code will compile
The inplementation of pure virtual function will be ignored by the compiler, unless it is pure virtual destructor.
class Basez
{
public :
virtual void method () = 0;
private :
int n;
};
void Basez::method()
{ n = 1;} // will be ignored in compilation time
class D1z : Basez {};
class D2z : public D1z
{
int i;
void method() {i = 2;}
};
"main":
//Basez bsz; // cannot do this
Derivez* dsz = new D2z();
dsz->method();
//dsz->Basez::method(); // cannot do this
Brooks is right. As written, the code segment will compile. However, the function definition:
- Wandering programmer February 28, 2010void Base::method() { n = 1;}
is ignored because the function method() in the base class is a pure virtual function. If one attempts to instantiate an object of either of the types Base or D1, the program will fail. The error will state that an abstract class cannot be instantiated.