Use the C ++ Interface

Source: Internet
Author: User
# Include "stdafx. H"

Using namespace STD;



// Abstract Interfaces

Interface IX
...{
Virtual void _ stdcall fx1 () = 0;
Virtual void _ stdcall fx2 () = 0;
};


Interface Iy
...{
Virtual void _ stdcall fy1 () = 0;
Virtual void _ stdcall FY2 () = 0;
};

Void trace (const char * PMSG)... {cout <PMSG <Endl ;};
// Interface implementation
Class CA: Public IX, public Iy
...{
Public:
// Implement interface IX
Virtual void _ stdcall fx1 ()... {cout <"CA: fx1" <Endl ;}
Virtual void _ stdcall fx2 ()... {cout <"CA: fx2" <Endl ;}

// Implement interface Iy
Virtual void _ stdcall fy1 ()... {cout <"CA: fy1" <Endl ;}
Virtual void _ stdcall FY2 ()... {cout <"CA: FY2" <Endl ;}
};


Int main ()
...{
Trace ("client create an instance of the component ");
Ca * pA = new CA;
// Get an iX pointer
IX * pix = PA;
Trace ("client: Use the IX interface ");
PIX-> fx1 ();
PIX-> fx2 ();

// Get an Iy pointer
Trace ("client use the Iy interface ");
Iy * piy = PA;
Piy-> fy1 ();
Piy-> FY2 ();
System ("pause ");

}

Stdafx. h

# Pragma once

# Include <iostream>
# Include <tchar. h>
# Include <objbase. h>

 

Non-interface communication
I wonder if you still remember the previous article that we mentioned that communication between customers and components is only through interfaces. However, the customers in the program list do not follow this rule. There, the communication between the customer and the component is completed by a pointer pointing to the class ca instead of a pointer pointing to the interface. Use a pointer to Ca to ask the customer to know the definition of the class (usually a header file ). There are many implementation details in the ca-like declaration. Modifications to these Implementation Details make it necessary for the customer to be re-compiled. As mentioned earlier, adding or reducing component interfaces should not interrupt existing customers. This is why we have repeatedly insisted that the customer and components should communicate only through interfaces. Remember, an interface is implemented by a virtual pure base class without implementation details.

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.