Hi, these classes for making a MuD are driving me crazy!!!

heres my starting code, haven't had more than 15 minutes to work out some basic stuff, just trying to start small, get it to compile, then add stuff...

#include<iostream>
#include<cstdio>
#include<windows.h>
#include<fstream>
#include<cstdlib>
#include<ctime>
using namespace std;
int a;
int main()
{
    //Just an include for the naming, to save some space.
    #include <naming.h>
    
    
//Starts player class.
class Player {
      public:
  int health;
  int strength;
  int attack;
  int defence;
  void move();
  void attackit();
  void getit(int x);
  void lookit();
  /*Player Constructor, USE ~       Player noob(15, 10, 20, 20);  
  first integer is health, second is strength, 
  third is attack, and fourth is defence. Use it to create a player object.
  */
  
  Player(int h, int s, int a, int d) {
  health = h;
  strength = s;
  attack = a;
  defence = d;
}


class Armor {
      int defbonus;
      string description;
      string name;
      //Armor constructor, use to create a armor object.
      Armor(int a, string b, string c) {
                defbonus=a;
                description=b;
                name=c;
                }
                };
                
                //Weapon class
                class Weapon {
                      int attbonus;
                      string description;
                      string name;
                      //Weapon object construction.
                      Weapon(int a, string b, string c) {
                                 attbonus=a;
                                 description=b;
                                 name=c;
                                 
                                 }
                                 };
                
                
                
};


Player noob(71, 72, 71, 59); 



 










cout<<"attack\n";
cout<<noob.attack;
cout<<"\n";
cout<<"str\n";
cout<<noob.strength;
cout<<"\n";
cout<<"def\n";
cout<<noob.defence;
cout<<"\n";
cout<<"hp\n";
cout<<noob.health;
cout<<"\n";



cin>>a;




    

}

Here is what I am confused about!!!!
How do I make a weapon object within the player object?
I would think It would be like this, but it wont compile..
Player noob.Weapon noobsword;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Any help??? I have heard something about pointers while searching google but i'm pretty sure there is a way to do this without them, I just don't know the correct syntax. I possible, could anyone give me the corect syntax?????
And if I have to use "Pointers", then could someone show me how to use them, or point me to a good, well explained place that can?

Recommended Answers

All 4 Replies

In the player class you could put a weapon class variable like in the code below (look at the player class).

But since the Weapon class is defined below the player class, the player class will not know what the Weapon type(class) is. So it is nessesary to include the class header so it will know that a weapon type does exist (look at the update towards the top). (Updates have capital comments).

Hope that helps

#include<iostream>
#include<cstdio>
#include<windows.h>
#include<fstream>
#include<cstdlib>
#include<ctime>
using namespace std;
class Weapon;                    //NEED CLASS HEADER
int a;
int main()
{
    //Just an include for the naming, to save some space.
    #include <naming.h>
    
    
//Starts player class.
class Player {
      public:
  int health;
  int strength;
  int attack;
  int defence;
  Weapon mWeapon;         //COULD PUT WEAPON CLASS TYPE INSIDE 
  void move();
  void attackit();
  void getit(int x);
  void lookit();
  /*Player Constructor, USE ~       Player noob(15, 10, 20, 20);  
  first integer is health, second is strength, 
  third is attack, and fourth is defence. Use it to create a player object.
  */
  
  Player(int h, int s, int a, int d) {
  health = h;
  strength = s;
  attack = a;
  defence = d;
}


class Armor {
      int defbonus;
      string description;
      string name;
      //Armor constructor, use to create a armor object.
      Armor(int a, string b, string c) {
                defbonus=a;
                description=b;
                name=c;
                }
                };
                
                //Weapon class
                class Weapon {
                      int attbonus;
                      string description;
                      string name;
                      //Weapon object construction.
                      Weapon(int a, string b, string c) {
                                 attbonus=a;
                                 description=b;
                                 name=c;
                                 
                                 }
                                 };
                
                
                
};


Player noob(71, 72, 71, 59); 



 










cout<<"attack\n";
cout<<noob.attack;
cout<<"\n";
cout<<"str\n";
cout<<noob.strength;
cout<<"\n";
cout<<"def\n";
cout<<noob.defence;
cout<<"\n";
cout<<"hp\n";
cout<<noob.health;
cout<<"\n";



cin>>a;




    

}

Thanks! The only thing I'm still confused about is...
Could I still use the weapon class within the player class?? How exactly would i do this? Plus, could someone show me the syntax through which I would access it?

any help lol?????

You don't need to create the entire weapon class inside the player class, (at the moment it seems to be inside the armour class for some reason. This seems to imply that a weapon is an implementation detail of armour. If you don't want this, you can make it stand-alone).

I believe what seanw is referring to, is something like this

class Weapon
{
    // Weapon stuff
public:
    void do_damage(int amount, string to_what);
};

class Player
{
    Weapon players_weapon;

    //Other player stuff
public:
    void attackit();
};

The idea being that any interaction with players_weapon will take place within methods of the Player class, for example, your attackit method might look something like this

//The attackit method is a member of the Player class, so it has direct access to players_weapon
void Player::attackit()
{
    players_weapon.do_damage(20, "Big_Monster");
    //More attackit stuff
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.