TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: BENJAMIN L MCGEE
from: THOMAS MAEDER
date: 1997-05-25 14:41:00
subject: First C++ prog

BM> I'm still a beginner tho and I'd appreciate it if you guys
BM> and gals would check my work.  So here it is my very first
BM> C++ program.  The Slot Machine.....
The overall inpression is good.
But if somebody shows me his/her (and sometimes even my :-)) C++ code,
I always have some remarks.
BM> #define WHEEL_MAX 13
BM> #define STARTING_CASH 300
Don't  use  #define  for  anything  else  than  conditional  compiling
(including avoiding including a header file more than once).
Better write
const unsigned int WHEEL_MAX = 13;
const unsigned int STARTING_CASH = 300;
The compiler will treat values defined like this exactly  as  if  they
had been #defined, but without the bad implications #define can have.
BM> class Wheel {
BM>     int top;
BM>     int mid;
BM>     int bot;
Two remarks here:
1)  Instead  of  exposing  member  variables  in the public interface,
better use accessor methods; this will prevent a client of  the  class
from overwriting the member variables with invalid values:
class Wheel 
{ 
    public: 
        unsigned int top() const  // accessor methods
        {
            return myTop;
        }
        unsigned int mid() const
        {
            return myMid;
        }
        unsigned int bot() const
        {
            return myBot;
        }
 
        void Spin(); 
    private:
        unsigned int myTop;  // member variables for representation
        unsigned int myMid;
        unsigned int myBot;
};
2)  Try  to avoid redundant member variables. Instances of class Wheel
each describe the state of a wheel.  For  this  purpose,  knowing  the
(say) top symbol and the order of the symbols is more appropriate than
your three variables:
class Wheel 
{ 
    public: 
        Wheel();    // constructor for initializing const members
        unsigned int top() const  // accessor methods
        {
            return myCurrentTop;
        }
        unsigned int mid() const
        {
            return (myCurrentTop+1)%WHEEL_MAX;
        }
        unsigned int bot() const
        {
            return (myCurrentTop+2)%WHEEL_MAX;
        }
 
        void Spin(); 
        // sets myCurrentTop to a new value in [0..WHEEL_MAX)
    private:
ú  [ Continued In Next Message... ]
--- PCBoard (R) v15.22/M 25
---------------
* Origin: McMeier & Son BBS (2:301/138)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.