86年的菜鸟

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  6 随笔 :: 0 文章 :: 6 评论 :: 0 Trackbacks
今天将大话设计模式第一章在C++里面实现。最终还是参考了某位仁兄的代码才豁然开朗。补了一节课。
1、了解了C++抛出异常的机制,以后编程要多注意。
2、封装的思想,封装啊封装。
3、编程规范,要学会用宏。
4、Operation 有子类OperationAdd。以下用法是不行的:
Operation oper;
oper 
= OperationAdd();
必须用new,此时oper对象必须使用指针。
Operation *oper;
oper 
= new OperationAdd(); 
用第一种方法的话,oper就不能调用子类重载的方法了。
5、原来调用类的成员用“::”,调用对象的成员用“.”,指针调用间接成员用“->”。

每天都能发现一点不足并改进少少的话,世界真美好!
最后贴个代码吧……犯忌的话,是我不小心,大家轻点砸……
  1/******Operation.h******/
  2
  3using namespace std;
  4
  5const int DIVED_ZERO_ERROR = 0;
  6const int OPERATE_TYPE_ERROR = 1;
  7
  8class Operation
  9{
 10protected:
 11    double numberA;
 12    double numberB;
 13
 14public
 15    Operation()
 16    {
 17        numberA = 0;
 18        numberB = 0;
 19    }

 20
 21    double GetNumberA()
 22    {
 23        return numberA;
 24    }

 25
 26    void SetNumberA ( double number )
 27    {
 28        numberA = number;
 29    }

 30
 31    double GetNumberB ()
 32    {
 33        return numberB;
 34    }

 35
 36    void SetNumberB ( double number )
 37    {
 38        numberB = number;
 39    }

 40
 41    virtual double GetResult()
 42    {
 43        double result = 0;
 44        return result;
 45    }

 46}
;
 47
 48class OperationAdd : public Operation
 49{
 50publicdouble GetResult()
 51    {
 52        double result = 0;
 53        result = numberA + numberB;
 54
 55        return result;
 56    }

 57}
;
 58
 59class OperationSub : public Operation
 60{
 61public
 62    double GetResult()
 63    {
 64        double result = 0;
 65        result = numberA - numberB;
 66
 67        return result;
 68    }

 69}
;
 70
 71class OperationMul : public Operation
 72{
 73public
 74    double GetResult()
 75    {
 76        double result = 0;
 77        result = numberA * numberB;
 78
 79        return result;
 80    }

 81}
;
 82
 83class OperationDiv : public Operation
 84{
 85public
 86    double GetResult()
 87    {
 88        if( numberB == 0 )
 89        {
 90            throw (DIVED_ZERO_ERROR);        
 91        }

 92
 93        double result = 0;
 94        result = numberA / numberB;
 95
 96        return result;
 97    }

 98
 99}
;
100
101//Factory Class
102 
103class OperationFactory
104{
105public :
106    static int getInt(){return 1;}
107    static Operation* createOperation(char operate)
108    {
109        Operation *oper;
110
111        switch (operate)
112        {
113        case '+' :
114            oper = new OperationAdd();
115            break;
116
117        case '-':
118            oper = new OperationSub();
119            break;
120
121        case '*':
122            oper = new OperationMul();
123            break;
124
125        case '/':
126            oper = new OperationDiv();
127            break;
128
129        default:
130            throw (OPERATE_TYPE_ERROR);
131
132        }

133        return oper;
134    }

135    
136}
;
137
138/******main.cpp*****/
139#include "iostream"
140#include "Operation.h"
141 
142using namespace std;
143
144const int OK    = 0;
145const int ERROR = 1;
146
147double ReadDouble ()
148{
149    double input;
150
151    cin >> input;
152    while (cin.fail())
153    {
154        cout << "Please Input a double number:" << endl;
155
156        cin.clear();
157        cin.sync();
158
159        cin >> input;
160    }

161    return input;
162}

163
164int main(void)
165{
166    double numberA,numberB;
167    char   operate;
168
169    try
170    {
171        cout << "Input numberA:" << endl;
172        numberA = ReadDouble();
173
174        cout << "Input numberB:"  <<endl;
175        numberB = ReadDouble();
176
177        cout << "Input operate:"  <<endl;
178        cin  >> operate;
179    
180        Operation *oper;
181        oper = OperationFactory::createOperation(operate);
182
183        oper->SetNumberA(numberA);
184        oper->SetNumberB(numberB);
185
186        cout << "The result is: " << oper->GetResult();
187
188        //return OK;
189
190    }
catch (int errorCode)
191    {
192        switch (errorCode)
193        {
194        case DIVED_ZERO_ERROR:
195            cout << "Dived number cannot be zero!" << endl;
196            break;
197
198        case OPERATE_TYPE_ERROR:
199            cout << "Operate Not Found!" << endl;
200            break;
201
202        default:
203            cout << "Unknown Error!" << endl;
204            
205        }

206        //return ERROR;
207    }

208
209    cin >> numberA;
210    
211}

212
posted on 2010-07-15 20:02 vc-zs 阅读(636) 评论(0)  编辑 收藏 引用 所属分类: C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理