C + + interface definition and implementation examples

Source: Internet
Author: User

I. Definition OF the interface

Sometimes, we have to provide some interface for others to use. The function of an interface is to provide a way to interact with other systems. Other systems do not need to know your internal details and do not understand the internal details, but communicate with you only through the interfaces you provide to the outside. According to the characteristics of C + +, we can use pure virtual function to achieve the way. The benefit of this is the ability to achieve encapsulation and polymorphism. Now give an example for your reference. (Do not want to do too much to explain, you should be able to understand at a glance)

1 Class IPerson2 3 {4 5  Public:6 7 IPerson () {};8 9    Virtual~iperson () =0{};//Note that it is a good idea to define this virtual destructor to prevent subclasses from calling destructors properly, and if defined as pure virtual destructors, you must have a definition body because the subclass implicitly calls the destructor. Ten  One    //the interface that is provided to the outside uses pure virtual function generally A  -    Virtual  voidSetName (Const string&strname) =0; -  the    Virtual  Const stringGetName () =0; -  -    Virtual  voidWork () =0; -  +}

Second, the interface implementation

The implementation interface is implemented by inheriting the subclass of the interface, and different subclasses can achieve different effects, even the so-called polymorphic.

1Class Cteacher: PublicIPerson2 3 {4 5  Public:6 7 Cteacher () {};8 9    Virtual~Cteacher ();Ten  One    stringM_strname; A  -    voidSetName (Const string&strName); -  the    Const stringGetName (); -  -    voidWork (); -  + } -  + voidCteacher::setname (Const string&strName) A  at { -  -M_strname =name; -  - }  -  in Const stringCteacher::getname () -  to { +  -     returnM_strname; the  * } $ Panax Notoginseng voidcteacher::work () -  the { +  Acout <<"I am teaching!"<<endl;//The teacher's job is teaching, and the work of other professions is not the same.  the  +}

Third, interface export

1 BOOLGetipersonobject (void**_rtobject)2 {3 4iperson* Pman =NULL;5 6Pman =NewCteacher ();7 8*_rtobject = (void*) Pman;9 Ten    return true; One  A }  -  -__declspec (dllexport)BOOLGetipersonobject (void* * _rtobject);

Iv. use of the interface

1#include"IPerson.h"2 3 #pragmaComment (lib, "IPerson.lib")4 5 BOOL__declspec (dllimport) getipersonobject (void**_rtobject);6 7  8 9 /*Test Examples*/Ten  One voidMain () A  - { -  theIPerson * _ipersonobj =NULL; -  -    void* pobj=NULL; -  +        if(Getipersonobject (&POBJ))  -  +         { A  at             //Get Object -  -_ipersonobj = (IPerson *) pObj; -  -             //invoking an interface, performing an operation -  in_ipersonobj->setname ("Tom"); -  to              stringStrName = _ipersonobj->GetName; +  -_ipersonobj->Work (); the  *        } $ Panax Notoginseng        if(_ipersonobj! =NULL) -  the        { +  A Delete _ipersonobj; the  +_ipersonobj =NULL; -  $        } $  -}

So far, the basic complete demonstration of the definition and implementation of the interface, there are shortcomings, please advise. ^-^...

C + + interface definition and implementation examples

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.