Design pattern C + + implementation one: Simple engineering mode

Source: Internet
Author: User

Refer to the big talk design pattern and some information on the net, write down some simple design patterns, let yourself understand the beauty of these design patterns!


#ifndef operation_h#define operation_h#include<iostream>using namespace Std;class operation{protected:double OpA, Opb;public:bool SetValue (double& N, double& m); virtual double getresult () const=0;}; Class Operationadd:p ublic operation{double getresult () const;}; Class Operationsub:p ublic operation{double getresult () const;}; Class Operationmul:p ublic operation{double getresult () const;}; Class Operationdiv:p ublic operation{double getresult () const;}; Class Operationfactory{public:operation * Creatoperation (char& operate);}; BOOL Operation::setvalue (double &n, double &m) {OpA = N;OPB = M;return true;} Double Operationadd::getresult () const{double Result;result = OpA + opb;return result;} Double Operationsub::getresult () const{double result;result = Opa-opb;return result;} Double Operationmul::getresult () const{double result;result = OpA * Opb;return result; Double Operationdiv::getresult () const{if (OpB = = 0) {cout << "OpB in Operationdiv can ' t is zero.\n"; return 0.00000001;} Double result;result = Opa/opb;return result;} Operation * Operationfactory::creatoperation (char& operate) {operation * Oper = Null;switch (operate) {case (' + '): O per = new Operationadd;break;case ('-'): Oper = new Operationsub;break;case (' * '): Oper = new Operationmul;break;case ('/'): Oper = new Operationdiv;break;default:break;} return Oper;} #endif

#include "operation.h" int main () {double NumA, numb;char op;operation * oper;operationfactory opfa;cout << "please Enter the numbers and operation (A op B) "; while (Cin>>numa&&cin>>op&&cin>>numb) {Oper = Opfa.creatoperation (OP); Oper->setvalue (NUMA, NumB) cout << "The result of" << NumA << op << NumB << "=" << oper->getresult () << endl;cout << "Please enter the numbers and operation (A op B) ";} return 0;}


Design pattern C + + implementation one: Simple engineering mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.