TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JASON REYNOLDS
from: JERRY COFFIN
date: 1997-03-17 17:22:00
subject: Help with Classes

On (16 Mar 97) Jason Reynolds wrote to All...
 JR> I use the following format to store and retrieve int's from a class. 
 JR> I would like to know how to store and retrieve char's.  I've been
 JR> unable to use char's in a class.  Could someone please help me out?
Hmm...you _should_ be able to just declare the variable as char instead
of an int.  What problems do you have when you do this?
 JR> Class PlayerInfo
 JR> {
 JR>   Public:
 JR>     int  GetHitPoint() const { return itsHitPoint;}
 JR>     void SetHitPoint(int hitPoint) { itsHitpoint = hitPoint; }
 JR>   Private:
 JR>     int itsHitPoint;                    // characters Hit Points
 JR> };
 JR> void main()
 JR> {
 JR>   PlayerInfo Player;
 JR>   Player.SetHitPoint(40);
 JR>   cout << "Hit Points: " << GetHitPoint() << endl;
 JR> }
Personally, I'd advise changing this.  Most code that uses Get/Set type
functions is relatively poorly designed, and this is an excellent
example.  (Don't feel bad though: you have LOTS of company, some of it
extremely distinguished.)
Now, for how I'd do this instead:
class PlayerInfo   	// Note that `class' canNOT be capitalized.
{
public:             // And neither can `public' or `private'
    virtual void show_status(ostream &stream) {
        cout << "Hit Points: " << itsHitPoint << endl;
    }
    bool take_hit(int inflicted_damage) {
        itsHitPoint -= inflicted_damage / armor_factor;
        return itsHitPoint >= 0;
    }
    PlayerInfo(int HitPoints = 40) : itsHitPoint(HitPoints) {}
    ~PlayerInfo() {}
private:
    PlayerInfo &operator=(PlayerInfo &other) {}
    int itsHitPoint;
    int armor_factor;
};
int main() {    // note that main should always return an int.
    PlayerInfo Player;
    Player.show_status();
    return 0;
}
Though it doesn't really show up in a program this size, this really
does help things out.  First and foremost, with this code, the outside
world doesn't need to know nearly as much about the player as it did
before.  The outside world just gets to try to inflict damage on the
player, and figure out whether it's dead or not.
Now, when does this make a difference: well, consider a player than can
use magic.  If he puts on armor, he takes less hits, but also loses some
ability to use magic.  Now, if we use Get/Set style code, we have to
have outside code figuring out what sort of armor (if any) the player is
wearing, and adjusting hit points accordingly.  However, with code like
that above, can simply add the code in ONE place to take_hit.  The
player knows what sort of armor he's wearing, and adjusts the hit
points.  Other code then doesn't have to worry about it.
IMO, it's also a mistake to make the attack just a number of hit points.
It's far better to make this a class as well.  This makes FAR more
realistic simulations much simpler to write.  For instance, magical
attacks should generally be FAR less effective against another magic
user than against a non-magic-user.  Likewise, plate armor might be only
twice as effective as chain mail against a sword, but 10 or 12 times as
effective against arrows.
Anyway, quick summary: Get/Set style functions aren't exactly evil in
themselves, but they USUALLY indicate places you could improve your
design.
    Later,
    Jerry.
... The Universe is a figment of its own imagination.
--- PPoint 1.90
---------------
* Origin: Point Pointedly Pointless (1:128/166.5)

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™.