标签:
#include <iostream> using namespace std; class Operation { public: void setOperands(const int operA = 0, const int operB = 0) {m_operandA = operA; m_operandB = operB;} virtual int getRlt() = 0; protected: int m_operandA; int m_operandB; }; class OperationAdd : public Operation { public: int getRlt() {return m_operandA + m_operandB;} }; class OperationSub : public Operation { public: int getRlt() {return m_operandA - m_operandB;} }; class SimpleFactory { public: static Operation* createOperation(char oper); }; Operation* SimpleFactory::createOperation(char oper) { Operation *operation = NULL; switch (oper) { case ‘+‘: operation = new OperationAdd(); break; case ‘-‘: operation = new OperationSub(); break; } return operation; } void main() { Operation *oper = NULL; oper = SimpleFactory::createOperation(‘+‘); oper->setOperands(32, 23); cout<<"Result: "<<oper->getRlt()<<endl; delete oper; oper = SimpleFactory::createOperation(‘-‘); oper->setOperands(32, 23); cout<<"Result: "<<oper->getRlt()<<endl; delete oper; system("pause"); }
标签:
原文地址:http://www.cnblogs.com/hushpa/p/4424507.html