Code Monkey home page Code Monkey logo

designpattern's People

Contributors

bajdcc avatar josansun avatar

Stargazers

 avatar  avatar  avatar

designpattern's Issues

智能指针使用应该要改一下,详细见代码

`// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include
#include
#include
#include
#include
using namespace std;

class Oper
{
public:
double getNumberA()
{
return x;
}
void setNumberA(double xx)
{
x = xx;
}
double getNumberB()
{
return y;
}
void setNumberB(double yy)
{
y = yy;
}
virtual double getResult()//使oper可实例化
{
return 0.0;
}
private:
double x;
double y;
};

class OperatorAdd :public Oper
{
public:
double getResult()
{
return getNumberA() + getNumberB();
}
};

class OperatorSub :public Oper
{
public:
double getResult()
{
return getNumberA() - getNumberB();
}
};

class OperatorMul :public Oper
{
public:
double getResult()
{
return getNumberA() * getNumberB();
}
};

class OperatorDiv :public Oper
{
public:
double getResult()
{
try
{
if (fabs(getNumberB()) < 1e-9)
{
throw overflow_error("Divide by zero exception");
}
}
catch (overflow_error& e)
{
cout << e.what() << " -> " << getNumberB() << endl;
}
return getNumberA() / getNumberB();
}
};

class OperFactory
{
public:
static Oper* createOper(char operChar)
{
//这边会报错
Oper oper;
switch (operChar)
{//这里原先用法不对,应将实例的类名放在模版中,结果显示正确
case '+':
oper = new OperatorAdd();
break;
case '-':
oper = new OperatorSub();
break;
case '
':
oper = new OperatorMul();
break;
case '/':
oper = new OperatorDiv();
break;
//other operation
default:
oper = NULL;
}
return oper;
}
};

void testSimpleFactoryMode()
{
shared_ptr oper(OperFactory::createOper('+'));
oper->setNumberA(1);
oper->setNumberB(2);
double res = oper->getResult();
cout << res << endl;
}

int main()
{
testSimpleFactoryMode();
return 0;
}

`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.