[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