TITLE: Another example on separation of interface and implementation PROBLEM: waddy@vsfl.demon.co.uk [...] Comming from a C and Ada background I pretty used to writing code modules where the implementation can be hidden entirely from the users. In C++ if I'm using a class then this doesn't seem to work very well. Say I'm writing a GUI object class and I'm trying to get some semblance of portability. I obviously need to record implementation dependent stuff in the object (eg. widget IDs) but if I declare these in the class then the .h file needs to include all the implementation dependent header files eg. // Gui.h #include class Gui { public: blah, blah private: Widget shell_w; Widget button_w; ... etc, } RESPONSE: jimad@microsoft.com (Jim Adcock), Microsoft Corporation Set up one header file with a description of your abstract base class and the protocols it supports. Provide another header file with functions that create and return pointers to objects of specific types derived from your abstract base class. The user then can do whatever is necessary using just those two header files. The fact that some concrete class needs to use Widgets remains totally hidden, which in turn means that the user never needs to include an header files associated with Widgets. In the below example, note that the user only sees Cats and Dogs, without ever worrying about the fact that the implementation of Cats and Dogs requires the use of iostreams. // Animal.h ---------- class Animal { public: void virtual Speak() = 0; void virtual Sleep() = 0; }; // makepet.h ---------- class Animal* NewDog(); class Animal* NewCat(); // user program: ---------- main() { Animal* Spot = NewDog(); Animal* Scratches = NewCat(); Spot->Speak(); Spot->Sleep(); Scratches->Speak(); Scratches->Sleep(); return 0; } // dog.cpp ---------- #include class Dog: public Animal { public: void Speak() { cout << "Woof\n"; } void Sleep() { cout << "ZZZzzzzz.....\n"; } }; Animal* NewDog() { return new Dog; } // cat.cpp ---------- #include class Cat: public Animal { public: void Speak() { cout << "Meow\n"; } void Sleep() { cout << "SSSSssss.....\n"; } }; Animal* NewCat() { return new Cat; }