-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory mode operation.cpp
59 lines (55 loc) · 1.16 KB
/
factory mode operation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <string>
#include <iostream>
using namespace std;
//运算基类
class Operation
{
private:
double Number_A ;
double Number_B ;
public:
double Get_A(){return Number_A;}
double Set_A(int avalue){Number_A = avalue;}
double Get_B(){return Number_B;}
double Set_B(int bvalue){Number_B = bvalue;}
virtual double GetResult()=0; //纯虚函数,实现类无实现则报错
};
//具体实现运算类 +
class AddOperation: public Operation
{
public:
virtual double GetResult()
{
return (Get_A()+Get_B());
}
};
//工场基类Creator-接口,赖运算基类
class OperationFactory
{
public:
virtual Operation* CreateOperation()=0;
};
//工场实现类 Concrete
class AddOperationFactory : public OperationFactory
{
public :
virtual AddOperation* CreateOperation()
{
return new AddOperation();
}
};
//客户端
int main()
{ //创建+对象,如需其它,扩展加入即可,不需修改原来任何类
int a,b;
OperationFactory *ofP=new AddOperationFactory ();
Operation *s=ofP->CreateOperation();
cin>>a>>b;
s->Set_A(a);
s->Set_B(b);
cout << s->GetResult()<< endl;
delete s;
delete ofP;
system("pause");
return 0;
}