#include <iostream>
using namespace std;
class base
{
public:
virtual void taunt() = 0;
virtual void hide() = 0;
};
template <int nx, int ny>
class derivedA : public base
{
void hide();
public:
void taunt() { cout << "derivedA" << endl; }
char c[nx][ny];
};
template <int nx, int ny, int nz>
class derivedB : public base
{
void taunt();
public:
void hide() { cout << "hide B" << end; }
char c[nx][ny][nz];
};
int main()
{
derived * dp = new derivedA();
dp->taunt();
delete dp;
DerivedB b;
dp = &b;
dp->hide();
return 0;
}
A and B are both derived from the pure virtual class base, and both have one method unimplemented. Is it legal to leave one method unimplemented? Is it acceptable or good practice to leave one method unimplemented? Are there better ways?
Edit:
I just noticed that the classes are not derived from base. I’ve changed the code so that it does.
If you derive from a class that has a pure virtual method and you don’t override that method in the derived class, that derived class will be an abstract class as well. If this is OK for you (e.g. you will derive it further), than you can do that. If you do want to instantiate that derived class, than you will have to override all pure abstract methods
Note that in your example you do not derive from the abstract
baseclass: bothderivedAandderivedBare standalone classes. You need: