日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++设计模式-继承与多态影响耦合性(最基础的简单工厂模式小实例)

發布時間:2025/3/15 c/c++ 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++设计模式-继承与多态影响耦合性(最基础的简单工厂模式小实例) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

繼承與多態影響耦合性(最基礎的簡單工廠模式小實例)

原理:
通過繼承和虛函數的方式修改某個子類對應函數的功能;
通過簡單工廠模式到底實例化誰;

如果要增加復雜的運算只有增加響應的子類,以及工廠的分支即可;

?

程序運行截圖如下:

?

目錄結構如下:

?

源碼如下:

OperationFactory.h

#pragma once#include "Operation.h" #include "OtherOperation.h" #include <iostream> #include <string> using namespace std;class OperationFactory{ public:static Operation *createOperate(string operateStr){Operation *operation;if(operateStr == "+")operation = new OperationAdd;else if(operateStr == "-")operation = new OperationSub;else if(operateStr == "*")operation = new OperationMul;else if(operateStr == "/")operation = new OperationDiv;else throw "The operation is wrong";return operation;} };

Oeration.h

#pragma once class Operation { public:double getNumberA();double getNumberB();void setNumberA(const double numberA);void setNumberB(const double numberB);virtual double getResult();virtual ~Operation();//protected:double m_numberA;double m_numberB; };

OtherOperation.h

#pragma once#include "Operation.h" #include <iostream> using namespace std;class OperationAdd:public Operation{ public:double getResult() override{return m_numberA + m_numberB;}~OperationAdd(){cout << "~OperationAdd called!" << endl;} };class OperationSub:public Operation{ public:double getResult() override{return m_numberA - m_numberB;}~OperationSub(){cout << "~OperationSub called!" << endl;} };class OperationMul:public Operation{ public:double getResult() override{return m_numberA * m_numberB;}~OperationMul(){cout << "~OperationMul called!" << endl;} };class OperationDiv:public Operation{ public:double getResult() override{if(m_numberB == 0)throw "The divisor cannot be 0";return m_numberA / m_numberB;}~OperationDiv(){cout << "~OperationDiv called!" << endl;} };

main.cpp

#include "OperatinFactory.h"int main(){double num1, num2;string operStr;cout << "Please in put NumA:";cin >> num1;cout << "Please in put operation:";cin >> operStr;cout << "Please in put NumB:";cin >> num2;Operation *oper;try{oper = OperationFactory::createOperate(operStr);oper->setNumberA(num1);oper->setNumberB(num2);cout << "The result is " << oper->getResult() << endl;delete oper;}catch(const char *msg){cout << "The error : " << msg << endl;}system("pause");return 0; }

Operation.cpp

#include "Operation.h" #include <iostream> using namespace std;double Operation::getNumberA() {return m_numberA; }double Operation::getNumberB() {return m_numberB; }void Operation::setNumberA(const double numberA) {m_numberA = numberA; }void Operation::setNumberB(const double numberB) {m_numberB = numberB; }double Operation::getResult() {return 0.0; }Operation::~Operation() {cout << "~Operation called" << endl; }

?

UML類圖如下:

總結

以上是生活随笔為你收集整理的C++设计模式-继承与多态影响耦合性(最基础的简单工厂模式小实例)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。