Stack
class¤:
class Stack : private List
{
public:
void Push(int i)
{ First(); Insert(i); }
int Pop(void)
{
First();
int top = GetCurrent();
DeleteCurrent();
return top;
}
};
Coefficient
class¤:
class Coefficient
{
public:
Coefficient(double initValue)
{ myValue = initValue; }
double GetValue(void)
{ return myValue; }
bool SetValue(double v)
{
if (v<0 || v>1) { return false; }
myValue = v;
return true;
}
private:
double myValue;
};
'\0'
characters:
class String;
class CharProxy
{
public:
CharProxy(String* owner, int index)
: myOwner(owner), myIndex(index)
{}
void operator=(char c)
{
if (c=='\0') { return; }
myOwner->myStr[myIndex] = c;
}
operator char (void) const
{ return myOwner->myStr[myIndex]; }
private:
String myOwner;
int myIndex;
};
class String
{
friend class CharProxy;
public:
String (char*);
~String(void);
CharProxy operator[](int index)
{
CharProxy proxy (this, index);
return proxy;
}
//...ETC...
private:
char* myStr;
int mySize;
};
// AND LATER...
int main(void)
{
String s ("a string");
s[3] = 'p';
s[5] = '\0';
cout << s[3] << s[5] << endl;
}
class Singleton
{
friend Singleton& getSingleton(void);
public:
~Singleton(void);
void Print(void)
{ cout << "There can be only one!" << endl; }
// OTHER MEMBER FUNCTIONS HERE
private:
Singleton(void);
};
Singleton& getSingleton(void)
{
static Singleton* theOneAndOnly = 0;
if (theOneAndOnly==0) { theOneAndOnly = new Singleton; }
return *theOneAndOnly;
}
// AND LATER
int main(void)
{
getSingleton().Print();
}
Controller
, Process
,
Validator
, Interface
, FileLock
,
EncryptionKey
, etc.
class Command
{
public:
Command(int bet, int bank)
: myBet(bet), myBank(bank)
{}
virtual int Do(void);
protected:
int myBet;
int myBank;
};
class Win : public Command
{
public:
Win(int bet, int bank) : Command(bet,bank) {}
virtual int Do(void)
{
cout << "You're a winner! You made "
<< 2*myBet << endl;
return myBank + 2*myBet;
}
}
class Lose : public Command
{
public:
Lose(int bet, int bank) : Command(bet,bank) {}
virtual int Do(void)
{
cout << "Better luck next time. You lost "
<< myBet << endl;
return myBank - myBet;
}
}
Command* selectfunc(int bet, int bank)
{
int randVal = randIn(1,1000000);
if (randVal==42) { return new Win(bet,bank); }
else { return new Lose(bet,bank); }
}
int main(void)
{
int credit = 100;
while (credit>0)
{
int bet;
cout << "Place your bet: ";
cin >> bet;
Command* action = selectFunc(bet,bank);
action->Do();
delete action;
}
cout << "Thank-you for contributing to"
<< " Victoria's $700 million surplus!"
<< endl;
}