1. Definition and implementation of the interface
The so-called interface, which encapsulates the internal implementation details, allows external users to use the interface functionality without needing to know the specifics of the interior. C + +, through the class implementation of object-oriented programming, and in the base class only the declaration of pure virtual function, and then in the derived class implementation of the pure virtual function of the way to implement the interface, different derived classes implement the interface is not the same way to achieve polymorphism. A simple example is now available to illustrate the implementation step (⊙o⊙).
The definition of a class 1.1 interface usually completes the definition of the class interface in the header file/*interfacedefineandrealize.h*/
[CPP]View PlainCopy
- #ifndef interface_define_and_realize
- #define Interface_define_and_realize
- #include <string>
- Using Std::string;
- Define Interface
- Class Person
- {
- Public
- Person (): M_strname ("# # #") //Member list initialization parameters
- {};
- Virtual ~person () {};
- virtual void Eat () =0; People need to eat.
- virtual void Sleep () =0; People need to sleep
- virtual void SetName (const string strName) =0; Everyone has a name.
- virtual String GetName () = 0; //Get name
- virtual void work () =0; People may have to have a job
- Private
- String M_strname;
- };
- Implementing interfaces
- Implementing interfaces is implemented through derived classes, each derived class can obtain different implementations of the same interface according to its own characteristics
- The so-called polymorphic
- Class Student: Public person
- {
- Public
- Student (): M_strname ("* * *")
- {};
- ~student ()
- {};
- void Eat ();
- void Sleep ();
- void SetName (const string strName);
- String GetName ();
- void work ();
- Private
- String M_strname;
- };
- #endif
The implementation of the 1.2 interface is usually done in the source file to complete the interface/*interfacedefineandrealize.cpp*/
[CPP]View PlainCopy
- #include "InterfaceDefineAndRealize.h"
- #include <iostream>
- #include <string>
- Using Std::string;
- Using Std::cout;
- Using Std::endl;
- External implementation of the interface
- void Student::sleep ()
- {
- cout<<"Student sleep." <<endl;
- }
- void Student::eat ()
- {
- cout<<"Student eat." <<endl;
- }
- void Student::setname (const string strName)
- {
- M_strname=strname;
- }
- void Student::work ()
- {
- cout<<"student work." <<endl;
- }
- String Student::getname ()
- {
- return m_strname;
- }
- Functions that need to be exported, that is, interfaces that the user can invoke externally
- _declspec (dllexport)bool Getpersonobject (void** _rtobject)
- {
- person* Pman=null;
- pman=new Student ();
- *_rtobject= (void*) Pman;
- return true;
- }
The export of the 1.3 interface is usually done in the module definition file/*interfacedefineandrealize.def*/
[CPP]View PlainCopy
- LIBRARY interfacedefineandrealize
- Exports
- Getpersonobject
Create a new project, load the above three files, set the project properties, configure properties--general---Configuration type, select "Dynamic Library. Dlll", generate the available dynamic libraries, If the project name is interfacedefineandrealize (note: The project name must be the same as the name defined in the module definition file after the library, otherwise it will cause an error that the dynamic library cannot be found.) ), the dynamic library and its import library are generated under the current working directory of the project.
2. Invocation of the interface
To be consistent with the usual way to invoke a dynamic library, here are some extra work. Create a new "include" folder and place InterfaceDefineAndRealize.h under this folder, create a new "Lib" folder and place the InterfaceDefineAndRealize.lib file under this folder. New Project Usinginterface, add the source file to implement the function of calling interface.
2.1 Adding an additional include directory for a project
Method 1: Project Properties--Configuration Properties-->c/c++--> general--The add-in directory adds the full path to the Include folder.
Method 2: Project Properties--Configuration Properties-->vc++ Directory--Include the full path of the Include folder in the included directory.
2.2 Adding additional libraries for a project
Method 1: Project Properties--Configuration Properties--linker--general--Additional library directories add the full path to the Lib folder.
Method 2: Project Properties--Configuration Properties-->vc++ Directory--The library directory adds the full path to the Lib folder.
Note: Method 1 in 2.1 corresponds to Method 1 in 2.2, method 2.1 in 2 corresponds to method 2.2 in 2, and cannot be used.
2.3 Adding an import library for a project
Item Properties--Configuration Properties--linker--add InterfaceDefineAndRealize.lib in an additional dependency
2.4 Providing dynamic libraries for projects
Put the generated. dll dynamic library into the debug directory under the project's current directory to prevent errors that are missing from the dynamic library.
2.5 Writing code to implement an interface invocation
[CPP]View PlainCopy
- #include <iostream>
- #include "InterfaceDefineAndRealize.h"
- BOOL _declspec (dllimport) getpersonobject (void** _rtobject);
- int main ()
- {
- person* Person=null;
- void* Pobj=null;
- if (Getpersonobject (&pobj))//Call interface
- {
- person= (person*) pObj;
- Person->eat ();
- Person->sleep ();
- Person->setname ("Zhang Hui");
- Std::cout<<person->getname () <<std::endl;
- Person->work ();
- if (person!=null)
- {
- Delete person;
- Person=null;
- }
- }
- System ("pause");
- return 0;
- }
Note: The above code is only for personal understanding, there may be errors or incorrect description of the place, I hope to give correct, thank you.
Detailed procedures for the definition and implementation of C + + interfaces