• 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
  • 相关阅读:
    云上攻防-云原生篇&Docker安全&权限环境检测&容器逃逸&特权模式&危险挂载
    tailwindcss引入项目的正确‘姿势’
    input控件的maxlength属性
    JAVA并发编程——CAS与AQS源码详解
    公司如何激发员工的创新能力?
    Ubuntu 20.04 上安装和配置 neo4j
    CleanMyMacX 永久版下载激活码破解版
    Selenium--多表单frame切换
    Prometheus 性能调优-水平分片
    架构核心技术之分布式消息队列
  • 原文地址:https://blog.csdn.net/qq_40178082/article/details/132939372