Detailed procedures for the definition and implementation of C + + interfaces

Source: Internet
Author: User

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
  1. #ifndef interface_define_and_realize
  2. #define Interface_define_and_realize
  3. #include <string>
  4. Using Std::string;
  5. Define Interface
  6. Class Person
  7. {
  8. Public
  9. Person (): M_strname ("# # #") //Member list initialization parameters
  10. {};
  11. Virtual ~person () {};
  12. virtual void Eat () =0; People need to eat.
  13. virtual void Sleep () =0; People need to sleep
  14. virtual void SetName (const string strName) =0; Everyone has a name.
  15. virtual String GetName () = 0; //Get name
  16. virtual void work () =0; People may have to have a job
  17. Private
  18. String M_strname;
  19. };
  20. Implementing interfaces
  21. Implementing interfaces is implemented through derived classes, each derived class can obtain different implementations of the same interface according to its own characteristics
  22. The so-called polymorphic
  23. Class Student: Public person
  24. {
  25. Public
  26. Student (): M_strname ("* * *")
  27. {};
  28. ~student ()
  29. {};
  30. void Eat ();
  31. void Sleep ();
  32. void SetName (const string strName);
  33. String GetName ();
  34. void work ();
  35. Private
  36. String M_strname;
  37. };
  38. #endif

The implementation of the 1.2 interface is usually done in the source file to complete the interface/*interfacedefineandrealize.cpp*/

[CPP]View PlainCopy
  1. #include "InterfaceDefineAndRealize.h"
  2. #include <iostream>
  3. #include <string>
  4. Using Std::string;
  5. Using Std::cout;
  6. Using Std::endl;
  7. External implementation of the interface
  8. void Student::sleep ()
  9. {
  10. cout<<"Student sleep."  <<endl;
  11. }
  12. void Student::eat ()
  13. {
  14. cout<<"Student eat."  <<endl;
  15. }
  16. void Student::setname (const string strName)
  17. {
  18. M_strname=strname;
  19. }
  20. void Student::work ()
  21. {
  22. cout<<"student work."  <<endl;
  23. }
  24. String Student::getname ()
  25. {
  26. return m_strname;
  27. }
  28. Functions that need to be exported, that is, interfaces that the user can invoke externally
  29. _declspec (dllexport)bool Getpersonobject (void** _rtobject)
  30. {
  31. person* Pman=null;
  32. pman=new Student ();
  33. *_rtobject= (void*) Pman;
  34. return true;
  35. }


The export of the 1.3 interface is usually done in the module definition file/*interfacedefineandrealize.def*/

[CPP]View PlainCopy
    1. LIBRARY interfacedefineandrealize
    2. Exports
    3. 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
  1. #include <iostream>
  2. #include "InterfaceDefineAndRealize.h"
  3. BOOL _declspec (dllimport) getpersonobject (void** _rtobject);
  4. int main ()
  5. {
  6. person* Person=null;
  7. void* Pobj=null;
  8. if (Getpersonobject (&pobj))//Call interface
  9. {
  10. person= (person*) pObj;
  11. Person->eat ();
  12. Person->sleep ();
  13. Person->setname ("Zhang Hui");
  14. Std::cout<<person->getname () <<std::endl;
  15. Person->work ();
  16. if (person!=null)
  17. {
  18. Delete person;
  19. Person=null;
  20. }
  21. }
  22. System ("pause");
  23. return 0;
  24. }


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.