// lesson1
#include <stdio.h>
#include <iostream>
using namespace std;
class Operation {
protected:
double _numberA;
double _numberB;
public:
virtual ~Operation(){};
double getNumberA(){ return _numberA;};
void setNumberA(double A){_numberA = A;};
double getNumberB(){ return _numberB;};
void setNumberB(double B){_numberB = B;};
virtual double getResult(){return 0.0;};
};
class OperationAdd:public Operation {
public:
double getResult(){
double result = 0;
result = _numberA + _numberB;
return result;
}
};
class OperationSub:public Operation {
public:
double getResult(){
double result = 0;
result = _numberA - _numberB;
return result;
}
};
class OperationMulti:public Operation {
public:
double getResult(){
double result = 0;
result = _numberA * _numberB;
return result;
}
};
class OperationDiv:public Operation {
public:
double getResult()
{
if (_numberB == 0) {
throw "_numberB can't be 0";
}
return _numberA / _numberB;
}
};
class OperationFactory{
public:
static Operation * createOperate(string operate){
Operation * oper ;
if (operate == "+") {
oper = new OperationAdd();
} else if (operate == "-") {
oper = new OperationSub();
} else if (operate == "*") {
oper = new OperationMulti();
} else if (operate == "/") {
oper = new OperationDiv();
}
return oper;
}
};
void testLesson1() {
float f1 = 5;
float f2 = 4;
string op = "*";
Operation * operation = OperationFactory::createOperate(op);
operation->setNumberA(f1);
operation->setNumberB(f2);
float result = operation->getResult();
cout<< result << endl;
}
比较重要概念:
聚合表示一种弱的“拥有关系”,就像大雁和雁群,
合成就是一种强的“拥有关系”,就像大雁和翅膀,