abstract class

P

puzzlecracker

Can we provide definition for not pure virtual functions (PVF) i.e.
methods in Abstract class - given that we have at least one PVF? IF
yes, where would it be used? can a suggestive example be provided?


thx
 
V

Victor Bazarov

puzzlecracker said:
Can we provide definition for not pure virtual functions (PVF) i.e.
methods in Abstract class - given that we have at least one PVF? IF
yes, where would it be used? can a suggestive example be provided?

Yes. Wherever the design needs it to be used. Do it yourself.
 
C

chris

puzzlecracker said:
Can we provide definition for not pure virtual functions (PVF) i.e.
methods in Abstract class - given that we have at least one PVF? IF
yes, where would it be used? can a suggestive example be provided?
I'm not sure what you want (assuming this isn't just a homework
question). You can definatly have non-pure virtual functions in a class
with pure virtual functions. You'd want them whenever you have an
abstract class where there exists some common overloadable functionality
all objects of that type (and derived types) want.
 
P

puzzlecracker

I was interested of whether it is needed, for you cannot create an
object of an abstract class, so having the definition appears to be an
exercise in futility.

Actually, I see one intuitive explanation, that are objects of
subclasses should have identical implementation of a method in the base
class... is it a valid case, are there more cases where it is needed?
thanks.
 
M

Mike Wahler

puzzlecracker said:
I was interested of whether it is needed, for you cannot create an
object of an abstract class, so having the definition appears to be an
exercise in futility.

No it's not. It can provide a 'default' behavior for
objects of derived type which don't implement it.
Actually, I see one intuitive explanation, that are objects of
subclasses should have identical implementation of a method in the base
class...

:)

is it a valid case,


Sure, why not? But of course 'validity' can only be
assessed for each particular heirarchy design.
are there more cases where it is needed?

I don't think so, but I could easily be overlooking something.
I wouldn't worry about it, just use it if it seems appropriate
to your design. Actually it could also be used as a 'place holder'
for code you haven't written yet, letting you have a 'complete'
entity for testing.


-Mike
 
L

le ténébreux

puzzlecracker said:
I was interested of whether it is needed, for you cannot create an
object of an abstract class, so having the definition appears to be an
exercise in futility.

Actually, I see one intuitive explanation, that are objects of
subclasses should have identical implementation of a method in the base
class... is it a valid case, are there more cases where it is needed?
thanks.

Even if only a large portion (not all) of your derived classes are
going to use the same implementation, giving that definition in the
base class will save you from having to define it for each of those.

For example, I'm working on a set of Windows GUI wrapper classes.
Each window class uses a completely different method for processing
messages from the OS, so I've declared a pure virtual function in
the base class for that. On the other hand, they almost all, with
a few exceptions, use the same method for repositioning and resizing
when their parent window changes, so I put that implementation into
the base class and just override it in the few cases where it needs
to be different.
 

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
474,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top