//------------.h---------------------- #include <cmath> #include <iostream> #include <string> using namespace std; class Operate { public: virtual ~Operate(); float get_a(); float get_b(); void set_a(float a); void set_b(float b); virtual float get_result(); private: float m_i_A; float m_i_B; }; class Add : public Operate { public: virtual float get_result(); }; class Sub : public Operate { public: virtual float get_result(); }; class Mul : public Operate { public: virtual float get_result(); }; class Div : public Operate { public: virtual float get_result(); }; class OperateFactory { public: Operate * make_operate(string op); }; //--------------------.cpp------------------------ #include "Operate.h" Operate::~Operate() {} float Operate::get_result() { return 0.0; } float Operate::get_a() { return m_i_A; } float Operate::get_b() { return m_i_B; } void Operate::set_a(float a) { m_i_A = a; } void Operate::set_b(float b) { m_i_B = b; } float Add::get_result() { float result = 0.0; result = get_a() + get_b(); return result; } float Sub::get_result() { float result = 0.0; result = get_a() - get_b(); return result; } float Mul::get_result() { float result = 0.0; result = get_a() * get_b(); return result; } float Div::get_result() { float result = 0.0; if( fabs(get_b() - 0.0001) > 0.001 ) { result = get_a() / get_b(); return result; } cout << "Div number is ZERO-ERROR!" << "/2n"; return 0.000001; } Operate * OperateFactory::make_operate(string op) { Operate * cOp = NULL; if( op == "+" ) { cOp = new Add(); } else if( op == "-" ) { cOp = new Sub(); } else if( op == "*" ) { cOp = new Mul(); } else if( op == "/" ) { cOp = new Div(); } else { cout << "You maybe enter the wrong OPERATOR!" << "/n"; } return cOp; } int main(int argc, char **argv) { float a, b; string op; cout << "Please enter the first Number: "; cin >> a; cout << "Please enter the operator: "; cin >> op; cout << "Please enter the second Number: "; cin >> b; Operate * Opr; OperateFactory operate_factory; Opr = operate_factory.make_operate(op); Opr->set_a(a); Opr->set_b(b); cout << "the result is: " << Opr->get_result(); if( Opr != NULL ) { delete Opr; } return 0; }