CHID
CHID

Reputation: 1643

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 this.

Upvotes: 2

Views: 8314

Answers (1)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361252

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!

Upvotes: 10

Related Questions