C ++ interface definition and implementation example

Source: Internet
Author: User

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. ^-^...

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.