Read big talk design patterns-simplefactory (II)

Source: Internet
Author: User

Because C # is not very familiar with, the C # program is converted into C ++.

 

// A supermarket discount instance

# Include "stdafx. H"
# Include "stdio. H"

// Abstract class
Class cashsuper
{
Public:
Virtual double acceptcash (double money) = 0;
};

// Normal billing subclass
Class cashnormal: Public cashsuper
{
Public:
Virtual double acceptcash (double money)
{
Return money;
}
};

// Discount fee subclass
Class cashrebate: Public cashsuper
{
PRIVATE:
Double moneyrebate;

Public:
Cashrebate (double rebate = 0.0): moneyrebate (rebate)
{

}
Virtual double acceptcash (double money)
{
Return money * moneyrebate;
}
};

// Rebate billing subclass
Class cashreturn: Public cashsuper
{
PRIVATE:
Double moneycondition;
Double moneyreturn;
Public:
Cashreturn (double condition = 0.0, double return = 0.0): moneycondition (condition ),
Moneyreturn (return)
{

}
Virtual double acceptcash (double money)
{
Double result = money;
If (money> = moneycondition)
Result = money-(money/moneycondition) * moneyreturn;
Return result;
}
};

Class cashfactory
{
Public:
Static cashsuper * createcashaccept (INT type) // cash receiving Factory
{
Cashsuper * cs = 0;
Switch (type)
{
// Normal charges
Case 0:
Cs = new cashnormal ();
Break;
Case 1:
Cs = new case hreturn (300,100 );
Break;
Case 2:
Cs = new case hrebate (0.8 );
Break;
}
Return Cs;
}
};

Int main (INT argc, char * argv [])
{
Double totle;
Int type = 1;
Cashsuper * csuper = cashfactory: createcashaccept (type );
Printf ("% 2.2f/N", csuper-& gt; acceptcash (1000 ));

Return 0;
}

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.