Simple Factory mode with C + + learning design pattern

Source: Internet
Author: User

</pre> got the book "Big Talk Design Mode", which, judging from the style of the book, feels that the book is well suited for college education. But for beginners, C # is not easy to accept. In order to solve this problem, and in order to improve their C + + level and deepen the understanding of object-oriented, I decided to use C + + to learn this book. <p></p><p><span style= "WHITE-SPACE:PRE;" > Operation class header file </span></p><p><span style= "WHITE-SPACE:PRE;" ></span></p><pre name= "code" class= "CPP" > #ifndef operation_h#define operation_hclass OPERATION {Public    :        operation ();        Virtual ~operation ();        void Set_numbera (double _numbera);        void Set_numberb (double _numberb);        Double Get_numbera ();        Double Get_numberb ();        Double virtual  get_result ();    Protected:        double Numbera;        Double Numberb;    Private:}; #endif//Operation_h

Operation class. cpp file

#include "Operation.h" operation::operation () {    //ctor    numbera = 0;    Numberb = 0;} Operation::~operation () {    //dtor}void Operation::set_numbera (double _numbera) {    numbera = _numbera;} void Operation::set_numberb (double _numberb) {    numberb = _numberb;} Double Operation::get_numbera () {    return this->numbera;} Double Operation::get_numberb () {    return this->numberb;} Double   Operation::get_result () {    return 0.0;}

Then the + 、-、 *,/Four classes inherit the class of operations respectively

#ifndef operationadd_h#define operationadd_h#include "Operation.h" class Operationadd:public operation{public    :        double Get_result ();    Protected:    private:}; #endif//Operationadd_h
#include "OperationAdd.h" double Operationadd::get_result () {    double result = 0;    result = Numbera + Numberb;    return result;}


#ifndef operationsub_h#define operationsub_h#include "Operation.h" class Operationsub:public operation{public    :        double Get_result ();    Protected:    private:}; #endif//Operationsub_h

#include "OperationSub.h" double Operationsub::get_result () {    double result = 0;    result = Numbera-numberb;    return result;}



#ifndef operationmul_h#define operationmul_h#include "Operation.h" class Operationmul:public operation{public    :        double Get_result ();    Protected:    private:}; #endif//Operationmul_h

#include "OperationMul.h" double Operationmul::get_result () {    double result = 0;    result = Numbera*numberb;    return result;}


#ifndef operationdiv_h#define operationdiv_h#include "Operation.h" class Operationdiv:public operation{public    :        double Get_result ();    Protected:    private:}; #endif//Operationdiv_h

#include "OperationDiv.h" double Operationdiv::get_result () {    double result = 0;    result = Numbera/numberb;    return result;}



Then there was a factory class

#ifndef operationfactory_h#define operationfactory_h#include "Operation.h" #include "OperationAdd.h" #include " OperationSub.h "#include" OperationMul.h "#include" OperationDiv.h "class operationfactory{public    :        static Operation *createoperation (char ch);        Operationfactory ();        Virtual ~operationfactory ();    Protected:    private:}; #endif//Operationfactory_h

#include "OperationFactory.h" Operationfactory::operationfactory () {    //ctor}operationfactory::~ Operationfactory () {    //dtor}operation *operationfactory::createoperation (char ch) {    operation *oper;    Switch (CH) {case    ' + ':        oper = new Operationadd ();        break;    Case '-':        oper = new Operationsub ();        break;    Case ' * ':        oper = new Operationmul ();        break;    Case '/':        oper = new Operationdiv ();        break;    }    return oper;}


And finally the main class

#include <iostream> #include "Operation.h" #include "OperationAdd.h" #include "OperationSub.h" #include " OperationMul.h "#include" OperationDiv.h "#include" OperationFactory.h "using namespace Std;int Main () {    operation* Oper;    Oper = operationfactory::createoperation (' * ');    Oper->set_numbera (1);    Oper->set_numberb (2);    Cout<<oper->get_result ();    return 0;}



Simple Factory mode with C + + learning design pattern

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.