Use the object-oriented programming language to implement a console program. You must enter two numbers and operator numbers to obtain the result"
Introduce [Object-Oriented Programming] and point out three important features
- Encapsulation
- Inheritance
- Polymorphism
Pointed out the programming requirements:
- Maintenance
- Reusable
- Scalable
Here we will summarize the design ideas: according to the requirements of the questions, we can divide them into the following steps,
- Enter two numbers
- Input operator number
- Calculation Result
- Output result
Here we will refer to the [logical part] and [display part]. In the [logical part], we want to establish a class for computing. Why should this class be the [Operational class] operation? -- If you want to add an operation called "fight", that is, "x fight y", consider the object-oriented scalability, we should simply add a class related to fight to fulfill this expansion requirement, while fight is an operation. Therefore, it is obviously necessary to add a fight operation class. As a result, in addition to the fight operation class, there are still many operation classes. Therefore, you can design an operation base class. These specific operation classes are designed as subclasses of the operation base class. To obtain the specific base Class and Class: [base class]
class Operator{public:Operator(void);virtual ~Operator(void);virtual double GetResult() const = 0; public: double _numberFirst = 0; double _numberSecond = 0;};Operator::Operator(void){}Operator::~Operator(void){}
[Addition operation class]
/* Addition operation class */class operatoradd: public operator {public: operatoradd (void); Virtual ~ Operatoradd (void); Virtual double getresult () const ;}; operatoradd: operatoradd (void) {STD: cout <"addition operation class constructor" <STD :: endl;} operatoradd ::~ Operatoradd (void) {} double operatoradd: getresult () const {STD: cout <"Call the getresult method of the addition class" <STD: Endl; double result = _ numberfirst + _ numbersecond; return result ;}
Subtraction operation class]
/* Subtraction operation class */class operatorsub: public operator {public: operatorsub (void); Virtual ~ Operatorsub (void); Virtual double getresult () const ;}; operatorsub: operatorsub (void) {STD: cout <"subtraction operation class constructor" <STD :: endl;} operatorsub ::~ Operatorsub (void) {} double operatorsub: getresult () const {STD: cout <"Call the getresult method of the subtraction class" <STD: Endl; double result = _ numberfirst-_ numbersecond; return result ;}
Simple factory class: returns the corresponding operation class for the input.
/* Factory class */# include <cassert> class operatorfactory {public: operatorfactory (void );~ Operatorfactory (void); Public:/* Static factory Method */static operator * createoperator (const STD: string & Model) ;}; operatorfactory: operatorfactory (void) {} operatorfactory ::~ Operatorfactory (void) {} operator * operatorfactory: createoperator (const STD: string & Model) {If (model = "+") {return New operatoradd ();} else if (model = "-") {return New operatorsub () ;}else {assert (false) ;}return null ;}
[Main]
int main(int argc, const char * argv[]){Operator * ope = NULL;std::string modeName = "-";ope = OperatorFactory::CreateOperator(modeName); ope->_numberFirst = 4; ope->_numberSecond = 5;cout << ope->GetResult(); delete ope;ope = NULL;return 0;}
[Output result]
Subtraction Constructor
Call the subtraction classGetresultMethod
-1