I. Interface Definition
Sometimes, we have to provide some interfaces for others to use. The interface is used to provide a method for interacting with other systems. Other systems do not need to know your internal details or internal details. They can only communicate with you through external interfaces. Based on the features of C ++, we can use pure virtual functions. The advantage of doing so is encapsulation and polymorphism. Here is an example for your reference. (If you don't want to explain it too much, you should be able to understand it at a Glance)
Class iperson
{
Public:
Iperson (){};
Virtual ~ Iperson () = 0 {}; // Note: it is best to define this virtual destructor to prevent the subclass from calling the Destructor normally. If it is defined as a pure virtual destructor, it must contain a definition body, because the subclass implicitly calls the destructor.
// Interfaces provided for external use generally use pure virtual functions
Virtual void setname (const string & strname) = 0;
Virtual const string getname () = 0;
Virtual void work () = 0;
}
Ii. Interface implementation
The implementation interface is implemented by inheriting the sub-classes of the interface. Different sub-classes can achieve different effects, even if the so-called polymorphism.
Class cteacher: Public iperson
{
Public:
Cteacher (){};
Virtual ~ Cteacher ();
String m_strname;
Void setname (const string & strname );
Const string getname ();
Void work ();
}
Void cteacher: setname (const string & strname)
{
M_strname = Name;
}
Const string cteacher: getname ()
{
Return m_strname;
}
Void cteacher: Work ()
{
Cout <"I am teaching! "<Endl; // the instructor's job is to teach, and other professionals do different jobs.
}
Iii. Interface Export
Bool getipersonobject (void ** _ rtobject)
{
Iperson * pman = NULL;
Pman = new cteacher ();
* _ Rtobject = (void *) pman;
Return true;
}
_ Declspec (dllexport) bool getipersonobject (void ** _ rtobject );
Iv. Interface Usage
# Include "iperson. H"
# Pragma comment (Lib, "iperson. lib ")
Bool _ declspec (dllimport) getipersonobject (void ** _ rtobject );
/* Test example */
Void main ()
{
Iperson * _ ipersonobj = NULL;
Void * pobj = NULL;
If (getipersonobject (& pobj ))
{
// Obtain the object
_ Ipersonobj = (iperson *) pobj;
// Call the interface to perform the operation
_ Ipersonobj-> setname ("Tom ");
String strname = _ ipersonobj-> getname;
_ Ipersonobj-> Work ();
}
If (_ ipersonobj! = NULL)
{
Delete _ ipersonobj;
_ Ipersonobj = NULL;
}
}
So far, the definition and implementation of interfaces have been fully demonstrated. If you have any shortcomings, please advise. ^-^...