Quantum booleans (was: Re: [LC++]Is there a place for public data members?)

Carlo Wood carlo at alinoe.com
Tue Nov 6 15:05:05 UTC 2001


On Tue, Nov 06, 2001 at 11:16:15AM +1030, Mark Phillips wrote:
> So your means of setting is better, but I still like the idea of 
> using "myInt()" rather than "getMyInt()" as I don't see what's 

Thats ok, it's just a matter of preference.

> wrong with saying "this is the attribute" rather than just "I am 
> going to get the attribute for you".  Is this issue just a matter 
> of taste, or am I missing something more significant?

Your 'attribute' function is a 'const' function, and the 'modifier'
function is non-const function.

Important for the design however is how you look at your object.
When thinking about an object and its public interface, you shouldn't
take anything of it's internals into account - as if you don't even
KNOW anything about it.

It is not possible to give any example without using something
specific: it depends very on what the object represents on how
you should look at it.  The "MyClass" object is therefore a difficult
example to use, it is too abstract.  Explicitly revealing
an internal M_myInt, is also bad for the example: it spoils the
attempt not to know anything about the internals.

I sometimes teach people C++, that I do by teaching them to
think Object Oriented.  A good start is to let them write a
class Boolean with all possible operators defined:

class Boolean;

and then define operators for ||, &&, !=, !, =, operator bool, etc.

After you wrote that (and tested it), I say: now we will turn
this class into a statistical boolean: it's (internal) representation
must be the *chance* that it's value is true (physians love that,
its closely related to quantum mechanics ;).

So then we have a boolean with 'quantum' state 0.3 that we
OR with one with a 'quantum' state of 0.6, this gives:

int main(void)
{
  QuantumBoolean a(0.3);		// uses constructor QuantumBoolean(double);
  QuantumBoolean b(0.6);
  QuantumBoolean c = a || b;		// OR operator.
  if (c)
    std::cout << "true\n";		// Chance that c is true is 0.72.
  else
    std::cout << "false\n";
  return 0;
}

Playing with this gives you a pretty good idea of what an
object is ;).

-- 
Carlo Wood <carlo at alinoe.com>

PS  0.72 == 1 - (1 - 0.3) * (1 - 0.6)
PS2 The Real Problem is the correlation problem of
    QuantumBoolean(QuantumBoolean const& q) that should store
    a reference to the assigned QuantumBoolean rather then
    a chance.  All `this' pointers could for instance be
    stored in a static set<> in order to keep track of
    correlations between chances ;).  That is step three:

    QuantumBoolean a(0.3);
    QuantumBoolean b = !a;
    QuantumBoolean c = a && b;
    std::cout << "c = " << c.chance() << '\n';	// Must print 0

PS3 If someone wants to play with this, use

#include <cstdlib>
#include <limits>

class QuantumBoolean {
  private:
    double M_chance;
  public:
    QuantumBoolean(void) : M_chance(0.5) { }
...
    operator bool (void)
    {
      double value = random();
      value /= std::numeric_limits<long>::max();
      return M_chance > value;
    }
    double chance(void) const { return M_chance; }
};

This operator allows you to use the Boolean as a classic boolean ;).
Ie: Boolean c;  if (c) ...





More information about the tuxCPProgramming mailing list