// Fig. 11.16: Implementation.h
// Implementation class definition.
class Implementation
{
private:
int value;
public:
// constructor
Implementation( int v )
: value( v ) // initialize value with v
{
// empty body
} // end constructor Implementation
// set value to v
void setValue( int v )
{
value = v; // should validate v
} // end function setValue
// return value
int getValue() const
{
return value;
} // end function getValue
// data that we would like to hide from the client
}; // end class Implementation
// Fig. 11.18: Interface.cpp
// Implementation of class Interface--client receives this file only
// as precompiled object code, keeping the implementation hidden.
#include "Interface.h" // Interface class definition
#include "Implementation.h" // Implementation class definition
// constructor
Interface::Interface( int v )
: ptr ( new Implementation( v ) ) // initialize ptr to point to
{ // a new Implementation object
// empty body
} // end Interface constructor
// call Implementation's setValue function
void Interface::setValue( int v )
{
ptr->setValue( v );
} // end function setValue
// call Implementation's getValue function
int Interface::getValue() const
{
return ptr->getValue();
} // end function getValue
// destructor
Interface::~Interface()
{
delete ptr;
} // end ~Interface destructor
// Fig. 11.17: Interface.h
// Proxy class Interface definition.
// Client sees this source code, but the source code does not reveal
// the data layout of class Implementation.
class Implementation; // forward class declaration required by line 17
class Interface
{
public:
Interface( int ); // constructor
void setValue( int ); // same public interface as
int getValue() const; // class Implementation has
~Interface(); // destructor
private:
// requires previous forward declaration (line 6)
Implementation *ptr;
}; // end class Interf