大话设计模式 工厂模式 C++计算器
2017-03-24 21:54
183 查看
#include<iostream> using namespace std; class Operation { public: virtual double getAnswer(double _num1 ,double _num2) = 0; }; class OperationAdd : public Operation { public: double getAnswer(double _num1,double _num2) { return _num1 + _num2; } }; class OperationSub : public Operation { public: double getAnswer(double _num1,double _num2) { return _num1 - _num2; } }; class OperationDiv : public Operation { public: double getAnswer(double _num1,double _num2) { return _num1 / _num2; } }; class OperationMul : public Operation { public: double getAnswer(double _num1,double _num2) { return _num1 * _num2; } }; class CFactory { public: Operation & creatFactory(char type) { Operation *op; switch(type) { case '+': op = new OperationAdd(); break; case '-': op = new OperationSub(); break; case '*': op = new OperationMul(); break; case '/': op = new OperationDiv(); break; } return * op; } }; int main() { CFactory factory; Operation *op; double num1,num2; char type; cin >> num1 >> type >> num2; op = &factory.creatFactory(type); cout << op->getAnswer(num1,num2); }
相关文章推荐
- 【c++】用工厂模式实现计算器功能(附工厂模式代码)
- 大话设计模式——工厂方法模式(C++计算器修改)
- 大话设计模式中简单工厂模式的计算器实例的C++代码
- 大话设计模式笔记——工厂模式(C++计算器实现)
- 用C++实现的命令行计算器
- 我读《大话设计模式》第一章把里面的代码用C++实现
- [Gof设计模式]简单工厂模式和工厂模式的C++实现
- C++设计模式之一 工厂模式(简单工厂、工厂和抽象工厂)
- 大话设计模式之C++实现---简单工厂模式
- c++ 计算器代码
- C++设计模式之一 工厂模式(简单工厂、工厂和抽象工厂)
- [读书笔记]大话设计模式 计算器设计
- [转]C++设计模式之一 工厂模式(简单工厂、工厂和抽象工厂)
- C++工厂模式实现与改进小结
- 设计模式----工厂模式实现C++
- C++ 设计模式 (一) 工厂模式
- 计算器之简单工厂模式学习笔记[C++版]
- c++实现计算器
- c++编写计算器
- QT C++ 计算器