Case study of adapter Mode

Source: Internet
Author: User

[Case]

My friend brought me a microwave oven from the United States, but because the domestic electricity voltage in the United States is 110 V, and the voltage in China is 220 V, I cannot use it. Fortunately, my friend has a foresight, bring me back a transformer and convert the 220v voltage to the 110v voltage.

[Intention]

Convert the excuse of a class into another interface that the client expects, so that the two classes that the original interface does not match and cannot work together can work together. This program enables the c220 class to be used in the c110 class through adaptation.

 

Program code:

# Include <iostream. h>

 

Class c220v

{

Public:

   Void dianya220v ()

    {

        Cout <"220v voltage! "<Endl;

    }

};

Class c110v

{

Public:

    Virtual void dianya110v () = 0;

};

Class adaper: Public c110v

{

PRIVATE:

    C220v * c220obj;

Public:

    Adaper (c220v * OBJ)

    {

        C220obj = OBJ;

    }

    Void dianya110v ()

    {

        C220obj-> dianya220v ();

        Cout <"transformer to" <Endl;

        Cout <"110v voltage" <Endl;

    }

};

Int main ()

{

    C220v * c220vobj = new c220v;

    Adaper adobj (c220vobj );

    Adobj. dianya110v ();

    Return 1;

}

Running result:

220v voltage!

Transformer Conversion

110v Voltage

Press any key to continue

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.