ben2ong2
Quality Poster
Paid
Hero Member
   
Reputation: 17
Offline
Gender: 
Posts: 2374
9976.80 RD$
View Inventory
Send Money to ben2ong2
|
 |
« on: October 07, 2006, 08:48:48 AM » |
|
[ This material adapted from "Effective C++" by Scott Meyers, page 111. -adc ]
"C++ doesn't do a very good job of separating interfaces from implementations. In particular, class declarations include not only the interface specification (typically in the public and protected sections), but also the implementation details. For example:
class Person { private: TString fName; TDate fBirthdate; TAddress fAddress; TCountry fCountry; public: Person (const TString& name, const TDate& date, const TAddress& a, const TCountry& c); [...] };
[...] the class Person can't be compiled unless the compiler also has access to declarations for the classes in terms of which Person is implemented, namely, String, Date, Address, and Country.
[...] Unfortunately this sets up a compilation dependency between the file containing the Person class declaration and these additional files.
[...] Here is how you employ the technique to decouple the Person's interface from it's implementation. [...]
class TString; class TDate; class TAddress; class TCountry;
class TPersonImpl;
class Person { private: TPersonImpl* fImpl; public: Person (const TString& name, const TDate& date, const TAddress& a, const TCountry& c); [...] };
Now clients of Person are completely divorced from the details of strings, dates, addresses, countries and persons. Those classes can be modified at will, but clients of Person may remain blissfully unaware. More to the point, they may remain blissfully un-recompiled. This is true separation of interface and implementation.
A different approach is to make Person an Abstract Base Class (ABC) [...]. It typically has no data members, no constructors, a virtual destructor, and a set of pure virtual functions that specify the interface. An ABC for Person might look like this:
class Person { public: virtual ~Person ();
virtual const char* name () const = 0; virtual const char* birthdate () const = 0; virtual const char* address () const = 0; virtual const char* nationality () const = 0; };
Clients of the Person class program in terms of Person pointers and references, since it is not possible to instantiate classes containing pure virtual functions.
[...] "What does all this hocus-pocus cost me?," you mutter. The answer is the usual one in computer science: it costs you some speed at runtime, plus some additional memory per object.
|