Background:
Beginners of programming will have such problems. When encountering problems, they intuitively use the logic that the computer can understand to describe the problems to be solved and the specific solution process. However, such programs are only to meet the current needs and are not easy to maintain and expand.
Problem solving ideas:
- Before designing a piece of code, we need to consider its maintainability and expansibility;
- The object-oriented features are fully integrated into the code design process;
calculator.h
1 #ifndef _CALCULATOR_H_ 2 #define _CALCULATOR_H_ 3 #include <string> 4 5 struct Calculator { 6 Calculator() = default; 7 Calculator(double lNum, double rNum) 8 :leftNumber(lNum), rightNumber(rNum), result(0){} 9 ~Calculator(){} 10 11 virtual double getResult() = 0; //Can or not be implemented in the base class 12 protected: 13 double leftNumber; 14 double rightNumber; 15 double result; 16 }; 17 18 /* 19 About inheritance method: 20 public -- Attribute invariance 21 protected -- public Become protected 22 private -- All become private 23 */ 24 class CalculatorAdd :public Calculator 25 { 26 public: 27 CalculatorAdd() = default; 28 CalculatorAdd(double lNum, double rNum) 29 :Calculator(lNum, rNum){} 30 ~CalculatorAdd(){}; 31 //plus 32 virtual double getResult(); 33 }; 34 class CalculatorSub :public Calculator 35 { 36 public: 37 CalculatorSub() = default; 38 CalculatorSub(double lNum, double rNum) 39 :Calculator(lNum, rNum){} 40 ~CalculatorSub(){}; 41 //reduce 42 virtual double getResult(); 43 }; 44 class CalculatorMul :public Calculator 45 { 46 public: 47 CalculatorMul() = default; 48 CalculatorMul(double lNum, double rNum) 49 :Calculator(lNum, rNum){} 50 ~CalculatorMul(){}; 51 //ride 52 virtual double getResult(); 53 }; 54 class CalculatorDiv :public Calculator 55 { 56 public: 57 CalculatorDiv() = default; 58 CalculatorDiv(double lNum, double rNum) 59 :Calculator(lNum, rNum){} 60 ~CalculatorDiv(){}; 61 //except 62 virtual double getResult(); 63 }; 64 65 struct FactoryCalculator { 66 FactoryCalculator() = default; 67 FactoryCalculator(std::string op, double num1, double num2); 68 double getResult(){ return p->getResult(); }//Short function default inline 69 private: 70 std::string operate; 71 double number1; 72 double number2; 73 Calculator *p; 74 }; 75 76 #endif
calculator.cpp
1 #include "calculator.h" 2 #include <iostream> 3 4 using namespace std; //Usually, using Appears only in the source file, not in the header file 5 6 double Calculator::getResult() 7 { 8 cout << "message print in Calculator::getResult" << endl; 9 return 0.0; 10 } 11 12 double CalculatorAdd::getResult() 13 { 14 Calculator::getResult();//I've been here 15 16 result = leftNumber + rightNumber; 17 return result; 18 } 19 double CalculatorSub::getResult() 20 { 21 Calculator::getResult(); 22 23 result = leftNumber - rightNumber; 24 return result; 25 } 26 double CalculatorMul::getResult() 27 { 28 Calculator::getResult(); 29 30 result = leftNumber * rightNumber; 31 return result; 32 } 33 double CalculatorDiv::getResult() 34 { 35 Calculator::getResult(); 36 37 if (rightNumber == 0){ 38 cout << "division cannot be zero" << endl; 39 return -1; 40 } 41 result = leftNumber / rightNumber; 42 return result; 43 } 44 45 //If the function body is too long, the initialization list is written in the source file (the initialization list must follow the function body) 46 FactoryCalculator::FactoryCalculator(string op, double num1, double num2) 47 :operate(op), number1(num1), number2(num2) 48 { 49 char flag = operate[0]; 50 switch (flag) 51 { 52 case '+': 53 p = new CalculatorAdd(number1, number2); 54 break; 55 case '-': 56 p = new CalculatorSub(number1, num2); 57 break; 58 case '*': 59 p = new CalculatorMul(number1, number2); 60 break; 61 case '/': 62 p = new CalculatorDiv(number1, number2); 63 break; 64 default: 65 break; 66 } 67 }
main.cpp
1 #include "calculator.h" 2 #include <iostream> 3 4 using namespace std; 5 6 int main() 7 { 8 FactoryCalculator *factoryCalculaotr = new FactoryCalculator("+", 1, 2); 9 FactoryCalculator *factoryCalculaotr2 = new FactoryCalculator("-", 1, 2); 10 FactoryCalculator *factoryCalculaotr3 = new FactoryCalculator("*", 1, 2); 11 FactoryCalculator *factoryCalculaotr4 = new FactoryCalculator("/", 1, 2); 12 cout << factoryCalculaotr->getResult() << endl; 13 cout << factoryCalculaotr2->getResult() << endl; 14 cout << factoryCalculaotr3->getResult() << endl; 15 cout << factoryCalculaotr4->getResult() << endl; 16 17 system("pause"); 18 }