Re: Retrofitting base class with pure virtual function

From: Sz Borbely (szabolcs_borbely_at_hotmail.com)
Date: 04/08/04


Date: 7 Apr 2004 19:36:23 -0700


"Ken->" <handsom@comcast.netspicedham> wrote in message news:<NYmdncnRq5B75endRVn-hA@comcast.com>...
> I have a base class that is inherited by several classes and they all have
> there own ReadFile and WriteFile functions. The base class does not have a
> ReadFile or WriteFile, but can I now add a pure virtual ReadFile and
> WriteFile function to the base class without screwing up the classes that
> already inherit from it?

In a design where derived classes have commonalities, they all should
be implemented in the base class. To take advantage of polymorphism,
make the base class abstract by making ReadFile and WriteFile members
pure virtual. Put all common instructions to reading/writing into
ReadFile and WriteFile of the base class. Only implement specifics in
derived classes.
So to answer the question, yes, you should be able to make the base
abstract.
Good luck!

class B {
virtual void ReadFile () = 0;
};

class D: public B{
virtual void ReadFile ();
};

D *ptrDerived = new D;
B *ptrBase = ptrBase; // implicit conversion

ptrBase->ReadFile (); // will call D::ReadFile ()

and so on.