inheritance of abstract class
I hav a base class which is abstract. I am inheritting a new class from the abstract base which i could not instantiate an object. The reason the compiler tells is that
cannot allocate an object of abstract type
Is there any way to overcome开发者_C百科 this.
cannot allocate an object of abstract type
This indicates you've not implemented all the pure virtual
functions in the derived class. So first, implement all the pure virtual functions, then create instance of this class.
You cannot create instances of class which has even a single pure virtual
function!
Remember, pure virtual functions are those which are assigned with zero (C++ Virtual/Pure Virtual Explained), as
class sample
{
public:
virtual void f(); //virtual function
virtual void g()=0; //pure virtual function
};
Here only g()
is pure virtual function! This makes sample
an abstract class, and if the derived class doesn't define g()
, it would also become an abstract class. You cannot create instance of any of these class, as both of them are abstract!
精彩评论