• 1.简单工厂模式


    UML类图

    在这里插入图片描述

    代码

    main.cpp

    #include 
    #include "OperationFactory.h"
    using namespace std;
    
    int main(void) {
    
    	float num1;
    	float num2;
    	char operate;
    	cin >> num1 >> num2 >> operate;
    	Operation* oper = OperationFactory::createOperate(operate);
    	oper->setnumA(num1);
    	oper->setnumB(num2);
    	double result = oper->getResult();
    	cout << result << endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    OperationFactory.h

    #include"Operation.h"
    #include
    using namespace std;
    class OperationFactory {
    public:
    	static Operation* createOperate(char operate) {
    		Operation *oper = NULL;
    		switch (operate) {
    		case '+':
    			oper = new OperationAdd();
    			break;
    		case '-':
    			oper = new OperationSub();
    			break;
    		case '*':
    			oper = new OperationMul();
    			break;
    		case '/':
    			oper = new OperationDiv();
    			break;
    		}
    		return oper;
    	}
    };
    
    
    • 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

    Operation.h

     class Operation {//运算类基类
    
     protected:
    	 float numA = 0;
    	 float numB = 0;
    
     public:
    	 void setnumA(float paramA) {
    		 numA = paramA;
    	 }
    	 void setnumB(float paramB) {
    		 numB = paramB;
    	 }
    	 virtual float getResult() {
    		 float result = 0;
    		 return result;
    	 }
    
    };
    
    
     class OperationAdd :public Operation {//加法类派生类
    
     public:
    	 float getResult() {
    		 float result = 0;
    		 result = numA + numB;
    		 return result;
    	 }
    
     };
    
     class OperationSub :public Operation {//派生减法类
    
     public:
    	 float getResult() {
    		 float result = 0;
    		 result = numA - numB;
    		 return result;
    	 }
     };
    
     class OperationMul :public Operation {//派生乘法类
     public:
    	 float getResult() {
    		 float result = 0;
    		 result = numA * numB;
    		 return result;
    	 }
     };
    
     class OperationDiv :public Operation {//派生除法类
     public:
    	 float getResult() {
    		 float result = 0;
    		 result = numA / numB;
    		 return result;
    	 }
     };
    
    
    • 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
    • 60
  • 相关阅读:
    哈佛国际评论比赛秋季赛抓紧时间准备起来
    【剑指Offer】19.正则表达式匹配
    4核8G服务器价格选择轻量还是CVM合适?
    el-table表格进行排序 & 清除排序和清除排序箭头的高亮图标
    01-简历设计(模板1)
    802.11基础——术语及缩略语
    LeetCode 859. Buddy Strings
    Java InputStreamReader类的简介说明
    P2258 [NOIP2014 普及组] 子矩阵 day16
    玻色量子“天工量子大脑”亮相中关村论坛,大放异彩
  • 原文地址:https://blog.csdn.net/qq_40178082/article/details/132939372