#include<iostream>
using namespace std
;
class Calculate
{
public:
int getResult(string oper
)
{
if (oper
== "+")
{
return m_Num1
+ m_Num2
;
}
else if (oper
== "-")
{
return m_Num1
- m_Num2
;
}
else if (oper
== "*")
{
return m_Num1
* m_Num2
;
}
else if(oper
== "/")
{
return m_Num1
/ m_Num2
;
}
}
public:
int m_Num1
;
int m_Num2
;
};
void test01()
{
Calculate c
;
c
.m_Num1
= 10;
c
.m_Num2
= 10;
cout
<< c
.m_Num1
<< "+" << c
.m_Num2
<< "=" << c
.getResult("+") << endl
;
cout
<< c
.m_Num1
<< "-" << c
.m_Num2
<< "=" << c
.getResult("-") << endl
;
cout
<< c
.m_Num1
<< "*" << c
.m_Num2
<< "=" << c
.getResult("*") << endl
;
cout
<< c
.m_Num1
<< "/" << c
.m_Num2
<< "=" << c
.getResult("/") << endl
;
}
class AbstractCalculate
{
public:
virtual int getResult()
{
return 0;
}
public:
int m_Num1
;
int m_Num2
;
};
class AddCalculate :public AbstractCalculate
{
public:
int getResult()
{
return m_Num1
+ m_Num2
;
}
};
class SubCalculate :public AbstractCalculate
{
public:
int getResult()
{
return m_Num1
- m_Num2
;
}
};
class MulCalculate :public AbstractCalculate
{
public:
int getResult()
{
return m_Num1
* m_Num2
;
}
};
class DivCalculate :public AbstractCalculate
{
public:
int getResult()
{
return m_Num1
/ m_Num2
;
}
};
void test02()
{
AbstractCalculate
* abc
= new AddCalculate
;
abc
->m_Num1
= 10;
abc
->m_Num2
= 10;
cout
<< abc
->m_Num1
<< "+" << abc
->m_Num2
<< "=" << abc
->getResult() << endl
;
delete abc
;
abc
= new SubCalculate
;
abc
->m_Num1
= 10;
abc
->m_Num2
= 10;
cout
<< abc
->m_Num1
<< "-" << abc
->m_Num2
<< "=" << abc
->getResult() << endl
;
delete abc
;
abc
= new MulCalculate
;
abc
->m_Num1
= 10;
abc
->m_Num2
= 10;
cout
<< abc
->m_Num1
<< "*" << abc
->m_Num2
<< "=" << abc
->getResult() << endl
;
delete abc
;
abc
= new DivCalculate
;
abc
->m_Num1
= 10;
abc
->m_Num2
= 10;
cout
<< abc
->m_Num1
<< "/" << abc
->m_Num2
<< "=" << abc
->getResult() << endl
;
delete abc
;
}
int main()
{
test02();
system("pause");
return 0;
}
转载请注明原文地址:https://tech.qufami.com/read-2471.html