Abstract class derived from concrete class?

W

Wat

Is it doable to have a abstract class derived from a concrete class?

Is it a good practice? If so in what situation is this necessary?

Thanks in advance!
 
G

Gianni Mariani

Wat said:
Is it doable to have a abstract class derived from a concrete class?

yes.

struct Fountation
{
int instance_number;

Fountation()
instance_number( next_number ++ )
{
}

static int next_number;
};

struct Interface
: virtual Fountation
{
virtual void ExplodeStuff() = 0;
};

....

Is it a good practice?

It depends on what you need it to do.

If so in what situation is this necessary?

Probably never although it might be the best way to implement somthing.
 
C

Cy Edmunds

Wat said:
Is it doable to have a abstract class derived from a concrete class?
Yes.


Is it a good practice?

IMHO, no.
If so in what situation is this necessary?

Thanks in advance!

If you want a mix of concrete and abstract behavior you can start with the
abstract and then derive a base class with a partial implementation:

class Thing // abstract base class
{
public:
virtual const char *name() const = 0;
virtual do_something() = 0;
};

class Base : public Thing // partial implementation
{
private:
std::string m_name;
public:
Base(const char *i_name) : m_name(i_name) {}
virtual const char *name() const {return m_name.c_str();}
};

Now the client can derive from Base if he wants this implementation of
name() but can also derive from Thing if he wants to do it some other way.
If you derived Thing from a concrete type the concrete part would not be
negotiable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top