Class example

This is a discussion on Class example within the C++ Programming forums, part of the General Programming Boards category; Iam pretty new to OOP, but for a text game, would this be ok for the player class? any suggestions ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    Class example

    Iam pretty new to OOP, but for a text game, would this be ok for
    the player class? any suggestions appricaiated:

    Code:
    class Player
    {
    public:
    Player();  // constructor
    int health;
    string name;
    char weapon[15];
    
    private:
    bool palaive;
    bool pdead;
    
    };
    
    Player::Player()
    {
    health = 100;
    name;
    weapon = "sword";
    }
    
    // in main
    
    Player play
    
    // call member
    
    cout << Weapon is: " << play.weapon;
    cout << "health is: " << play.health;
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    
    class Player
    {
    public:
    Player();  // constructor
    int health;
    string name;
    string weapon;
    
    private:
    bool palaive;
    bool pdead;
    
    };
    
    Player::Player()
    {
    health = 100;
    name;
    weapon = "sword";
    }
    
    int main()
    {
    Player play;
    cout << "Weapon is: " << play.weapon;
    cout << "health is: " << play.health;
    
    return 0;
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    1. You should be trying to make all your data private to the class, and provide access functions to manipulate that data.

    2. char weapon[15];
    Use std::string unless you have a good reason not to.

    3. bool palaive;
    bool pdead;
    So if alive and dead are both true, what does that mean (zombie?)
    They seem mutually exclusive states to me.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    yes i understand, i think il put health and any integers in a private acsessor then use functions to acsess the data after initalizing them using a constructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21