You can have derived classes that contain member functions and data that do not show up in the base class --- alot of this kind of thing is very common - it is the way to extend the functionality of a base class. Windows controls base classes are often extended in this fashion, for example.
Take this esample:
class BaseClass{
public:
BaseClass();
virtual ~BaseClass();
void SomeFunction1();
void SomeFunction2();
} // end BaseClass
class DerivedClass : public BaseClass
{
public:
DerivedClass();
virtual ~DerivedClass();
void SomeFunction3();
void SomeFunction4();
} // end DerivedClass
DerivedClass ingerits all the functionality of BaseClass (SoneFunction1() and SomeFunction2()) , plus adds a couple of functions to boot, effectively extending the capability of BaseClass......
In other words, you don't need to "back insert" functions/data that belong in the derived class(es) into your base class at all.
Main Topics
Browse All Topics





by: imladrisPosted on 2000-10-04 at 07:52:49ID: 4576954
There are two kinds of possibilities in inheritance. One is that the base contains code common to various derived classes. The point of inheritance here is to factor out common code, thus (hopefully) making it easier to maintain the program. The second kind of desire is to have objects that present a common kind of "interface", but behave somewhat differently based on what they are representing.
It seems from your description that the second case is, at least partly, your aim. However, given the need to specialize the derived classes, some extra work is needed. I would probably (not knowing the details of the problem) approach it by using casting. Put all the actual common code in the base class and the specialized code in the derived classes. Now, whenever you access a "common" method (hopefully the majority of the time), you simply use the MyClass pointer to do that. In the odd case where you need the specialized function of the derived class, you are presumably in code that knows, or is in a position to find out, whether MyClass really represents Derived1 or Derived2. With that information you can access the specialized method by casting MyClass to Derived1 (or 2) and accessing the method.
Having said all that, it would still be worth considering whether it is possible to do things in such a way that it can all go "through" BaseClass. Is there some way of creating a common abstraction of the needed specializations? That would, obviously, be better.