#include <iostream>
using namespace std;
class Operation
{
public:
double getFirstNum() const {
return firstNum;
}
void setFirstNum(double firstNum) {
Operation::firstNum = firstNum;
}
double getSecondNum() const {
return SecondNum;
}
void setSecondNum(double SecondNum) {
Operation::SecondNum = SecondNum;
}
virtual double getResult() = 0;
private:
double firstNum;
double SecondNum;
};
class OperationAdd : public Operation
{
public:
double getResult()
{
return getFirstNum()+getSecondNum();
}
};
class OperationDiv : public Operation
{
public:
double getResult()
{
if(getSecondNum() != 0)
return getFirstNum()/getSecondNum();
}
};
class OperationFactory
{
private:
Operation *operation;
public:
Operation *createOperate(char oper)
{
switch (oper)
{
case '+':
operation = new OperationAdd();
break;
case '/':
operation = new OperationDiv();
break;
default:
cout <<"default branch" << endl;
}
return operation;
}
};
int main()
{
Operation *oper;
OperationFactory operationFactory;
oper = operationFactory.createOperate('/');
oper->setFirstNum(1);
oper->setSecondNum(2);
cout << oper->getResult() << endl;
return 0;
}