Thread: Need Help With "Class" For My Monster And Player

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    67

    Need Help With "Class" For My Monster And Player

    Hey guys! I know some of you might comment saying this should be in the game programming section, but my question isn't specifically related to a game function, but the C++ function of classes in general.

    Here is my program so far, just to let you guys know, this program was purely made fast and simple to see if my first two classes would work:


    Code:
    #include <iostream>
    #include <windows.h>
    #include <string>
    
    using namespace std;
    class CPlayer {
        int Hp;
        int Att;
        int Str;
    
      public:
        void set_values ( int,int,int);
    };
    void CPlayer::set_values (int Hp, int Att, int Str) {
      Hp = 100;
      Str = 10;
      Att = 10 + Str;
    }
        class CMonster {
        public:
        int Hp;
        int Att;
        int Str;
        public:
        void set_values (int, int, int);
        };
        void CMonster::set_values(int HP, int Att, int Str) {
        Hp = 50;
        Str = 5;
        Att = 10 + Str;
        }
    
    int main ()
    {
       string Input;
       int PHealth;
       int MHealth;
      cout << "Watch out warrior, a monster is behind you!";
      cin.get();
      cout << "(A monster jumps out and attacks you!";
      getline (cin, Input);
      cout << "Would you like to attack?";
      cout << "(y) for Yes, (n) for No: ";
      cin >> Input;
      if ( Input == "y") {
    
     cout << "You attack the monster!";
     Sleep(3000);
     (CMonster.Hp - CPlayer.Att = PHealth);
    }
    
    else {
    cout << "Oh no! Since you didn't attack him, he attacked you!";
    (CPlayer.Hp - CMonster.Att = MHealth);
    }
    }


    Now, first off I did not use pointers in this. 'PHealth' is the variable for players health that will be changed and out putted.. same thing goes for MHealth, except its for monster. I am extremely new to classes, and haven't really concepted how every part of it works, such as private, public, and protected. I do know what they are for, but somethings about 'em I don;t understand. Anyway thats not my problem here... my problem is at the very end where I want to subtract my monsters health variable from my players attack variable.. The program will not compile.. I think it has to do with the fact that "CMonster.Hp" isn't the way you tell the program you want to use the variable "Hp" from the class 'CMonster".. can any one point me in the right direction on what to do for that?? Maybe the right way is "CMonster::Hp" but that still wouldn't compile the program....

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    37
    First of all I will recommend to read about classes in c++.
    A few problems I see from quick look:
    1. All the fields in CPlayer class are private - you can only approach to them from within the class (read about private, public).
    2. in set_values(intHP, intAtt, intStr) you gave the variables the same name as the fields in the class - will not work how you wished it will work.
    As you said, I believe you don't know some key features in classes, will recommend reading a little bit before.
    Good luck!

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You also do not seem to understand what classes are or how to use them properly, so a little reading on that subject would definitely clear out some question marks!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    Quote Originally Posted by walla View Post
    First of all I will recommend to read about classes in c++.
    A few problems I see from quick look:
    1. All the fields in CPlayer class are private - you can only approach to them from within the class (read about private, public).
    2. in set_values(intHP, intAtt, intStr) you gave the variables the same name as the fields in the class - will not work how you wished it will work.
    As you said, I believe you don't know some key features in classes, will recommend reading a little bit before.
    Good luck!

    Thank you for you clarification and help! I tremendously appreciate it! =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-19-2011, 03:12 AM
  2. the operator "="error in class "String"
    By boyhailong in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2011, 08:39 PM
  3. Class Name "monster" not allowed??
    By napkin111 in forum C++ Programming
    Replies: 9
    Last Post: 01-23-2003, 09:39 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM