Big talk design pattern Note one

Source: Internet
Author: User

Convert its Java code into CPP code.

#include <iostream> #include <string>using namespace std;void func1 () {cout << "please input number A:" &  lt;< endl;long numbera;cin >> numbera;cout << "Please input operate (+_*/):" << Endl;char operate;cin >> operate;cout << "Please input number B:" << endl;long numberb;cin >> Numberb;switch (operate) { Case ' + ': cout << "result was:" << Numbera + numberb << endl;break;case '-': cout << "result is:" & lt;< numbera-numberb << endl;break;case ' * ': cout << "result is:" << Numbera * numberb << End L;break;case '/': cout << ' result is: ' << (double) numbera/numberb << Endl;break;}}

  

#include <iostream> #include <string>using namespace Std;class operation{protected:double numbera;double Numberb;public:double Getnumbera () {return numbera;} Double Getnumberb () {return numberb;} void Setnumbers (double value1, double value2) {Numbera = value1; numberb = value2;} Virtual Double GetResult () {return 0.0;}}; Class Operationadd:public operation{public:double GetResult () {return numbera + Numberb;}}; Class Operationsub:public operation{public:double GetResult () {return numbera-numberb;}}; Class Operationmul:public operation{public:double GetResult () {return numbera * NUMBERB;}}; Class Operationdiv:public operation{public:double GetResult () {return numbera/numberb;}}; Class Operationfactory{public:static operation* createoperate (char operate) {Operation *p = Null;switch (operate) {case '  + ':p = new Operationadd (); Break;case '-':p = new Operationsub (); Break;case ' * ':p = new Operationmul (); Break;case '/':p = new Operationdiv (); break;} return p;}}; void Func2 () {Double A =7.0;double b = 8.0;char op = ' * '; operation* oper = operationfactory::createoperate (OP); Oper->setnumbers (A, b); cout << a << op << b << "equal" <<oper->getresult () << Endl;}

  

Big talk design pattern Note one

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.