It is about simple binary mathematical operations: addition, subtraction, multiplication, division, and so on.
The book is described in the non-C ++ language, but I still like C ++ a little more. The C ++ version is changed below.
Operation Base class:
Class operation:
{
Public:
Operation (): m_numa (0), m_numb (0 ){}
Virtual double getresult () {return 0 ;}
Void setoperationnum (double NUMA, double numb)
{
M_numa = NUMA;
M_numb = numb;
}
PRIVATE:
Double m_numa;
Double m_numb;
};
Addition class:
Class operationadd: Public operation
{
Public:
Virtual double getresult ()
{
Return m_numa + m_numb;
}
}
Similar to operationsub multiplication operationmul
Then implement the computing factory class:
Class operationfactory:
{
Public:
Operation * createoperate (const string & operate)
{
Operation * operator;
Switch (operate)
{
Case "+": condition = new operationadd (); break;
Case "-": condition = new operationsub (); break;
......
}
}
}
Practical application:
Operation * operator;
Operationfactory factory;
Operator = factory. createoperate ("+ ");
Handler-> setoperationnum (2, 3 );
Double result = response-> getresult ();
Delete objects;