///////////////////////////////////////////////////////////////////// // client.cpp - demonstrate protocol class // // ver 1.2 // // // // This file represents client code, which knows only about the // // protocol class, e.g., the protocol language and the names, // // but not the details, of the derived classes. // // // // The protocol class declares static make functions for its // // derived classes, and the derived classes implement the make // // (factory) function for their own class objects. Since the // // client does not directly create any derived class objects it // // does not need to include their header files, AND: // // // // HAS NO COMPILE DEPENDENCIES ON THEM! // // // ///////////////////////////////////////////////////////////////////// // build process // ///////////////////////////////////////////////////////////////////// // Required files: // // client.cpp, protocol.h, dynLL.lib, dynLL.dll // // // // Command line build: // // cl /GX /GD /LDd /Federived.dll derived1.cpp derived2.cpp // // cl /GX client.cpp derived.lib // // // // To build in integrated environment: // // - create Win32 Dynamic Link Library project // // - add derived1.cpp, derived2.cpp, and protocol.h to project // // - build derived.dll, copy derived.dll and derived.lib to // // client directory // // - create Win32 Console Application project for client // // - add client.cpp and protocol.h // // - select Project\Settings\Link // // - add dynLL.lib to Object/library modules: edit box // // - copy dynLL.dll and dynLL.lib to client's project // // directory from its debug directory // // - build and run // ///////////////////////////////////////////////////////////////////// #include #include "protocol.h" // note that the client only needs // the base class header file, not // headers from the derived classes using namespace std; // void main() { cout << "\n" << " testing protocol \n" << "==================" << endl; // note that clients can not make a protocol object, but fact- // ory functions make anonymous derived class objects and give // client access to them through a base class protocol pointer. /////////////////////////////////////////////////////////////// // client uses the first derived class, knowing only the // // protocol language and how to call its object factory // /////////////////////////////////////////////////////////////// protocol *bptr; bptr = protocol::make_d1(); // factory function creates // derived object bptr->putInt(2); // use base class interface bptr->getInt(); // derived class functions bptr->kill(); cout << endl; /////////////////////////////////////////////////////////////// // client uses the second derived class, knowing only the // // protocol language and how to call its object factory // /////////////////////////////////////////////////////////////// bptr = protocol::make_d2(); // factory function creates // another derived object bptr->putInt(-3); bptr->getInt(); bptr->kill(); cout << '\n' << endl; }