Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1265
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-26 22:07
文章分类
文章存档

2015年(1)

我的朋友
最近访客

分类: C/C++

2015-05-26 22:18:31

头文件中对类的声明:

点击(此处)折叠或打开

  1. #ifndef _MY_HEAD_
  2. #define _MY_HEAD_
  3. class operation//opreation class
  4. {
  5.     private:
  6.         double num1;
  7.         double num2;
  8.     public:
  9.         double getNum1();
  10.         double getNum2();
  11.         void setNum1(double num);
  12.         void setNum2(double num);
  13.         virtual double getResult()=0;    //纯虚函数,有子类去实现具体的运算过程
  14. };
  15. class operation_add : public operation//+
  16. {
  17.     public:
  18.         operation_add();
  19.         virtual double getResult();
  20. };
  21. class operation_sub: public operation//-
  22. {
  23.     public:
  24.         operation_sub();
  25.         virtual double getResult();
  26. };
  27. class operation_mult: public operation//*
  28. {
  29.     public:
  30.         operation_mult();
  31.         virtual double getResult();
  32. };
  33. class operation_div: public operation// /
  34. {
  35.     public:
  36.         operation_div();
  37.         virtual double getResult();
  38. };
  39. class FactoryOperation
  40. {
  41.     public:
  42.         static operation* creatOperation(char s);
  43. };
  44. #endif
类的定义:

点击(此处)折叠或打开

  1. #include"operation.h"

  2. double operation::getNum1()
  3. {
  4.     return num1;
  5. }
  6. double operation::getNum2()
  7. {
  8.     return num2;
  9. }
  10. void operation::setNum1(double num)
  11. {
  12.     this->num1 = num;
  13. }
  14. void operation::setNum2(double num)
  15. {
  16.     this->num2 = num;
  17. }
  18. double operation_add::getResult()
  19. {
  20.     return this->getNum1()+this->getNum2();    
  21. }
  22. operation_add::operation_add(){
  23. }

  24. double operation_sub::getResult()
  25. {
  26.     return this->getNum1()- this->getNum2();
  27. }
  28. operation_sub::operation_sub(){
  29. }
  30. double operation_mult::getResult()
  31. {
  32.     return this->getNum1()*this->getNum2();
  33. }
  34. operation_mult::operation_mult(){
  35. }

  36. double operation_div::getResult()
  37. {
  38.     return this->getNum1()/this->getNum2();
  39. }
  40. operation_div::operation_div(){
  41. }

  42. operation *FactoryOperation::creatOperation(char s)
  43. {
  44.     operation *oper = nullptr;
  45.     switch(s)
  46.     {
  47.         case '+':
  48.             oper = new operation_add();
  49.             break;
  50.         case '-':
  51.             oper = new operation_sub();
  52.             break;
  53.         case '*':
  54.             oper = new operation_mult();
  55.             break;
  56.         case '/':
  57.             oper = new operation_div();
  58.             break;
  59.     }
  60.     return oper;
  61. }

阅读(176) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~