Classes\Good Usage...

This is a discussion on Classes\Good Usage... within the C++ Programming forums, part of the General Programming Boards category; I'm not 100% on classes and I was wondering if someone could give me a example and explain it. Something ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    22

    Question Classes\Good Usage...

    I'm not 100% on classes and I was wondering if someone could give me a example and explain it.

    Something like using a class to make monsters in a rpg...

    Thanks ~GameGenie

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    Here you go.
    Code:
    class monster
    {
    public:
        void fight();
        void die();
    private:
        int power;
    };
    Woop?

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    22
    Quote Originally Posted by prog-bman
    Here you go.
    Code:
    class monster
    {
    public:
        void fight();
        void die();
    private:
        int power;
    };
    Thanks for the reply but it doesn't help really, i know how to write one down. Can you tell me what that would do?


    Edit: Also what would be a good way to use it?

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    #include <iostream>
    
    using namespace std;
    
    class monster //class called monster
    {
    public: //this allows everyhting to see and interate with each other in the class
        void fight()
        {
              cout << "you called the fight function" << endl;
        }
        void die()
        {
              cout << "you called the die function" << endl;
        }
    private: //this keeps the what below hidden from other classes
        int power; 
    }monster1; // access the class thru this from main
    
    
    int main()
    {
         monster1.fight(); // runs the fight function from class monster.
         monster1.die(); //calls the die function from class monster.
         cin.get();
         return 0;
    }
    Last edited by ILoveVectors; 08-19-2005 at 11:02 PM.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    I was kidding. But for a monster class it would depend on what you wanted to do.
    You could have alot of variables eg. power, health, weapons, isaboss, etc. Have functions that attack players like void attack(player &to_attack); etc it is all about what you want to do.
    Woop?

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    22

    Question

    Quote Originally Posted by ILoveVectors
    Code:
    #include <iostream>
    
    using namespace std;
    
    class monster //class called monster
    {
    public: //this allows everyhting to see and interate with each other in the class
        void fight()
        {
              cout << "you called the fight function" << endl;
        }
        void die()
        {
              cout << "you called the die function" << endl;
        }
    private: //this keeps the what below hidden from other classes
        int power; 
    }monster1; // access the class thru this from main
    
    
    int main()
    {
         monster1.fight(); // runs the fight function from class monster.
         monster1.die(); //calls the die function from class monster.
         cin.get();
         return 0;
    }
    Thanks that helped alot. I have another question though, why do you use "monster1.fight();" and not just monster.fight();?

    Edit: Never mind that last question i figured it out
    Last edited by GameGenie; 08-19-2005 at 11:15 PM.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    22
    Quote Originally Posted by prog-bman
    I was kidding. But for a monster class it would depend on what you wanted to do.
    You could have alot of variables eg. power, health, weapons, isaboss, etc. Have functions that attack players like void attack(player &to_attack); etc it is all about what you want to do.

    I was thinking more like multiple monsters...say rat, dog, cat, bear, pig..........................but any would work just so i get the point of classes

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    22

    Question

    What about making a class for alot of different types of monsters, each have there own stats.

    Code:
    #include<iostream>
    
    class monster
    {
    public:
        void cat()
        {
            health = 10;
            std::cout<<"Cat Health: " << health;
        }
        void dog()
        {
            health = 15;
            std::cout<<"Dog Health: " << health;
        } 
    private:
        int health;
    }mon;
    
    int main()
    {
        mon.cat();
        mon.dog();
        std::cin.get();
    }
    Is this a good way to use a class?...right....

    If i did this is there a way to change the health outside of the class or would i have to make a gethit(); function in the class to - the life away? Like when you monster gets hit, you would need to - some of the health.

    Edit: Ok i couldn't get it to work with it outside the class so i made this...
    Code:
    #include<iostream>
    
    class monster
    {
    public:
        void GetHit(int indam)
        {
            health -= indam;
        }
        void Cat()
        {
            health = 10;
            std::cout<<"Cat Health: " << health << "\n";
        }
        void Dog()
        {
            health = 15;
            std::cout<<"Dog Health: " << health << "\n";
        } 
    private:
        int health;
    }mon;
    
    int main()
    {
        mon.Cat();
        mon.Dog();
        std::cin.get();
        GetHit(5);
        std::cin.get();
    }
    It all worked before i added the GetHit() function, now i'm getting a error.

    30 C:\Dev-Cpp\classetup.cpp `GetHit' undeclared (first use this function)
    Last edited by GameGenie; 08-20-2005 at 02:49 AM.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Classes (and structs) are a mechanism to describe some type of object (in your case a class to describe monsters). You use member variables to describe attributes (in your case health) and member methods to describe activities. In the end analysis, all attributes will boild down to plain old data types like int, double, char, arrays, pointers, etc., though they can be combined into other classes contained within the current class etc.

    Once you have declared a class and defined any class methods, you can declare an actual instance of the the class (frequently called an object), (in your case mon). You can declare the objects immediately after the declaration of the class, as in your example, in which case the object has global scope. Or you can declare an object within a function, in which case it has limited scope. You can declare multiple objects of the same type but with different names.

    Sometimes you use one class to build other classes. In your case, you could have a Cat and Dog class that has all the attributes and methods of the monster class, plus some other attributes or methods unique to them. This process is called inheritance.

    When you use objects in your code you need to indicate which object you are using by indicating the name before the attribute/method (static methods are one exception to this rule, but we don't need to go there yet). In your case GetHit() is a member method of the monster class. Therefore you need to use it with an object, like mon.

    mon.GetHit(5);

    not alone as you did. When GetHit() is called without an object being bound to it, the compiler thinks you are calling a GetHit() function not associated with it, but there isn't one, so it spits out the error message that it is undeclared.
    You're only born perfect.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    22

    Cool

    Quote Originally Posted by elad
    Classes (and structs) are a mechanism to describe some type of object (in your case a class to describe monsters). You use member variables to describe attributes (in your case health) and member methods to describe activities. In the end analysis, all attributes will boild down to plain old data types like int, double, char, arrays, pointers, etc., though they can be combined into other classes contained within the current class etc.

    Once you have declared a class and defined any class methods, you can declare an actual instance of the the class (frequently called an object), (in your case mon). You can declare the objects immediately after the declaration of the class, as in your example, in which case the object has global scope. Or you can declare an object within a function, in which case it has limited scope. You can declare multiple objects of the same type but with different names.

    Sometimes you use one class to build other classes. In your case, you could have a Cat and Dog class that has all the attributes and methods of the monster class, plus some other attributes or methods unique to them. This process is called inheritance.

    When you use objects in your code you need to indicate which object you are using by indicating the name before the attribute/method (static methods are one exception to this rule, but we don't need to go there yet). In your case GetHit() is a member method of the monster class. Therefore you need to use it with an object, like mon.

    mon.GetHit(5);

    not alone as you did. When GetHit() is called without an object being bound to it, the compiler thinks you are calling a GetHit() function not associated with it, but there isn't one, so it spits out the error message that it is undeclared.

    Oops Thanks alot =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 02:13 PM
  2. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM
  3. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  4. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  5. Win2K Limiting CPU Usage?
    By drdroid in forum Tech Board
    Replies: 4
    Last Post: 03-31-2004, 01:08 PM

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