I am learning about virtual functions and abstract classes and it seems that I have a difficulty in comprehending something. Let's say I have this code:
class animal {
public:
virtual void show()=0;
};
class dog : public animal {
int weight;
};
int main()
{
//animal a;
dog d;
return 0;
};
I understand how animal a is incorrect because the animal class is abstract and I am not allowed to declare objects of this class. However, why the dog d is also incorrect? (given that the class dog is not abstract and so I can declare dog objects)
Thanks in advance!!
The derived class Dog
is also an abstract class because the pure virtual function show
is not overridden. So the class Dog
has the same pure virtual function.
Take into account that if you need the polymorphism then you have to declare the destructor of the base class as virtual.
At least you can write
class animal {
public:
virtual ~animal() = default;
virtual void show()=0;
};