0

I'm trying to build a Monte Carlo object class and am wondering about the best way to design the relevant classes.

The structure of the object (called Entity) contains name, description AND a Distribution type which can be of a few different types eg. Normal, Fixed Integer, Triangle etc. The Distribution class is (or might be) a superclass of the specific distributions eg. Normal.

So, I have

class Entity {
public:
  string name;
  Distribution* dsn; // superclass. Can this be done?
}

class Distribution {
   public:
   string Type;
   double Sample(void)=0; // To be implemented in subclasses, same function sig.
}

Then an example of the actual distribution might be

class NormalDistribution : public Distribution {
    public:
    setMean(double mean);
    setStdDeb(double stddev);
    double Sample(void);
}

FixedDistribtion would also be a subclass of Distribution and implement different methods eg. setWeights, setValues as well as Sample of course.

I was hoping to write code like this

Entity* salesVolume = new Entity();
salesVolume->setDistribution(NORM);
salesVolume->dsn->setMean(3000.0:
salesVolume->dsn->setStdDev(500.0);

Entity* unitCost = new Entity();
unitCost->dsn->setWeights( .. vector of weights);
unitCost->dsn->setValues( .. vector of values) ;

then

loop
   sv = salesVolume->sample();
   uc = unitCost->sample();
endloop

What I'd like to do is define a Distribution (superclass) in Entity, then use its subclass methods which vary depending on distribution type eg. setMean. The distribution object in Entity would be determined at run-time eg. if (distnType == NORM) dsn = new NormalDistribution; if (distnType == FIXED) dsn = new FixedDistribution;

Is this possible in C++, or have completely lost the concept of Polymorphism? I'm having trouble defining the dsn variable as the superclass, then narrowing down to an actual distribution at run-time. A (simpler) way would be to define Entity without a Distribution, and use Distribution's on their own, without attachment to Entity.

Thanks guys

Pete855217
  • 1,570
  • 5
  • 23
  • 35
  • Couldn't this be solved with templates? Write a class that's templated on the type of distribution? What you're describing in the second-to-last paragraph is called 'run time type information', and is very very very bad style, and almost never necessary. – pg1989 May 23 '12 at 02:24
  • If you need `Entity`s to be of the same type regardless of their distribution, I'd use the [visitor pattern](http://en.wikipedia.org/wiki/Visitor_pattern). – Robert Cooper May 23 '12 at 02:44
  • Thanks pg1989 and Robert Cooper. That was exactly the sort of info I was looking for. I will get rid of that run-time type info and look into the visitor pattern and templating the thing. Pete – Pete855217 May 23 '12 at 02:48

2 Answers2

0

I think you want to do it like this:

Entity* salesVolume = new Entity;
{
  NormalDistribution *dsn = new NormalDistribution;
  dsn->setMean(3000.0);
  dsn->setStdDev(500.0);
  salesVolume->setDistribution(dsn);
}

Entity* unitCost = new Entity;
{
  FixedDistribution *dsn = new FixedDistribution;
  dsn->setWeights( .. vector of weights);
  dsn->setValues( .. vector of values) ;
  unitCost->setDistribution(dsn);
}

There are other possibilities, but I think this most closely represents your original design.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • Yep, that's what I was trying to do. The problem is in the definition of Entity where the result of setDistribution could be either a NormalDistribution or a FixedDistribution. How would Entity handle this as they're pointers to two different types of objects? – Pete855217 May 23 '12 at 05:50
  • @Pete855217: Since they are both derived from Distribution, the setDistribution method just needs to take a Distribution pointer. – Vaughn Cato May 23 '12 at 12:32
  • Thanks Vaughn, that's what I was trying to do in my original code, having a few problems but I think they're just itty-bitty coding things. – Pete855217 May 25 '12 at 02:02
0

Posting own solution based on answers by pg1989 and Robert Cooper.

To define different types of Distribution, templates will be used. The visitor pattern can be used to solve this problem of wanting different implementations of a master class.

Pete855217
  • 1,570
  • 5
  • 23
  • 35