Skip to content

Instantly share code, notes, and snippets.

@grodtron
Created November 14, 2012 00:10
Show Gist options
  • Save grodtron/4069296 to your computer and use it in GitHub Desktop.
Save grodtron/4069296 to your computer and use it in GitHub Desktop.
Simple calculator program to demonstrate the factory pattern in cpp
*.swp
*.swo
*.o
test
#include "Addition.h"
Addition::Addition(){
setFirstOperand(0);
setSecondOperand(0);
}
Addition::~Addition(){
}
double Addition::getResult(){
return firstOperand + secondOperand;
}
#ifndef ADDITION_H_
#define ADDITION_H_
#include "MathOperation.h"
class Addition : public MathOperation {
public:
Addition();
virtual ~Addition();
// definition of pure virtual method from superclass
virtual double getResult();
};
#endif
#include "Division.h"
Division::Division(){
setFirstOperand(0);
setSecondOperand(1); // so we don't divide by zero
}
Division::~Division(){
}
void Division::setSecondOperand(double x){
// we do some validation to make sure that we don't divide by zero,
// and then call the sueprclass function
if (x != 0) MathOperation::setSecondOperand(x);
}
double Division::getResult(){
return firstOperand / secondOperand;
}
#ifndef DIVISION_H_
#define DIVISION_H_
#include "MathOperation.h"
class Division : public MathOperation {
public:
Division();
virtual ~Division();
virtual void setSecondOperand(double);
// definition of pure virtual method from superclass
virtual double getResult();
};
#endif
#include <iostream>
using namespace std;
#include "MathOperation.h"
#include "MathOperationFactory.h"
int main(int argc, const char *argv[])
{
char op;
double first, second;
// Get parameters to pass to the factory and the resulting object
cout << "Enter a simple math expression (e.g. '1 + 2' or '3.14159 * 25'): ";
cin >> first >> op >> second;
// create some kind of operation object based on the user input
MathOperation * operation = MathOperationFactory::createMathOperation(op);
// configure it
operation->setFirstOperand(first);
operation->setSecondOperand(second);
// get the result
cout << "Result is: " << operation->getResult() << endl;
return 0;
}
.PHONY: clean all
all: test
%.o: %.cpp
g++ -c $< -o $@
test: $(shell ls -1 *.cpp | sed 's/\.cpp/\.o/')
g++ $^ -o $@
clean:
rm -f *.o
rm -f test
#ifndef MATH_OPERATION_H_
#define MATH_OPERATION_H_
// An abstract class representing the idea of a mathematical operation on
// two operands (for example: addition, subtraction, multiplication or division)
class MathOperation {
protected:
// data members
double firstOperand;
double secondOperand;
public:
// some simple setters to allow configuring the operation.
virtual void setFirstOperand(double x){ firstOperand = x; }
virtual void setSecondOperand(double x){ secondOperand = x; }
// pure virtual method that should be implemented in subclasses
virtual double getResult() = 0;
// virtual destructor, since we intend to subclass this class
virtual ~MathOperation(){}
};
#endif
#include "MathOperationFactory.h"
#include "Addition.h"
#include "Subtraction.h"
#include "Multiplication.h"
#include "Division.h"
MathOperation * MathOperationFactory::createMathOperation(char operation){
switch(operation){
case '+':
return new Addition();
case '-':
return new Subtraction();
case '*':
return new Multiplication();
case '/':
return new Division();
default:
return new Addition(); // <-- we'll arbitrarily make this the default
}
}
#ifndef MATH_OPERATION_FACTORY_H_
#define MATH_OPERATION_FACTORY_H_
#include "MathOperation.h"
class MathOperationFactory {
public:
// the static factory method
static MathOperation * createMathOperation(char);
private:
MathOperationFactory(){} // no publicly accessible ctor
};
#endif
#include "Multiplication.h"
Multiplication::Multiplication(){
setFirstOperand(0);
setSecondOperand(0);
}
Multiplication::~Multiplication(){
}
double Multiplication::getResult(){
return firstOperand * secondOperand;
}
#ifndef MULTIPLICATION_H_
#define MULTIPLICATION_H_
#include "MathOperation.h"
class Multiplication : public MathOperation {
public:
Multiplication();
virtual ~Multiplication();
// definition of pure virtual method from superclass
virtual double getResult();
};
#endif
#include "Subtraction.h"
Subtraction::Subtraction(){
setFirstOperand(0);
setSecondOperand(0);
}
Subtraction::~Subtraction(){
}
double Subtraction::getResult(){
return firstOperand - secondOperand;
}
#ifndef SUBTRACTION_H_
#define SUBTRACTION_H_
#include "MathOperation.h"
class Subtraction : public MathOperation {
public:
Subtraction();
virtual ~Subtraction();
// definition of pure virtual method from superclass
virtual double getResult();
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment