Thread: Class Help

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    Class Help

    Im trying to create a weapon class to determine the damage each weapon will inflict on an opponent. For this example, I only want to do 1 weapon an axe.

    Basically below I want to create a damage of 20 for the axe and decrease the cpu health points from 100 to 80(with the 20 damage taken)




    #include <iostream>

    using namespace std;

    class weapon
    {

    char Axe = 20;
    };

    int main ()
    {
    cpu;
    cpu = 100;

    cpu = cpu - weapon.Axe

    cout << cpu << endl;


    cin.get();
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You haven't asked a question.
    My best code is written with the delete key.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Well... Where should I start? Your code is wrong i'm afraid.

    A class defines a blueprint of an object. It is not exactly an Axe. It is instead the plans for a weapon. It describes the properties of that weapon (damage, speed factor, weight, ...) and possibly the actions of that weapon (slash, throw, defend,...). The properties are variables, the actions are functions.

    So... to actually create the a very simple weapon class you will do something like this:

    Code:
    class Weapon {
        public:
            weapon(int damage = 0): dam_(damage) {}
            
            int damage() { return dam_; }
        private:
            int dam_;
    }
    To better understand the elements above you need to read about classes. Check these links to understand how to work with them:

    http://www.cprogramming.com/tutorial/lesson12.html
    http://www.cplusplus.com/doc/tutorial/classes.html
    http://appsrv.cse.cuhk.edu.hk/~csc45...torial.2/1.htm
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    char Axe = 20;
    you define axe as a char and assign it 20, which is not giving it the value 20 but the ASCII character that 20 represents... axe should be declaired as an int.

    cpu;
    cpu = 100;
    cpu does not name a type, you should have
    Code:
    int cpu = 100;
    there are other errors too, check out the links that Mario posted, they will give you a good starting point for class usage.

    cpu = cpu - weapon.Axe
    you can also use the -= operator here, like this
    Code:
    cpu -= weapon.Axe;
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    9
    I've looked at one of your linked websites and I am having trouble wih understand the code. I have read the explanation like 10 times. But need point explanation, the code in bold is what I dont understand. Code in italic red Im not quite sure but have an idea of what it is. Please let me know if Im correct. If not, dont be afraid to tell me bluntly that Im wrong, which explaining why its different.


    class Example_class (name of class)
    { (begins coding)
    private: ( makes code limited access to class only)
    int x; (parameter of example class)
    int y; (parameter of the example class)
    public: (dont understand why there are a private and public in the same code)
    Example_Class() (constructor)
    { (start block of code)
    x = 0; (declare value of x)
    y = 0; (declare value of y)
    } (end block of code)
    ~Example_Class() (destructor)
    {} (why is there curly brackets with no code)
    int Add() ( no clue)
    { (begin block of code)
    return x+y; (return the total value of x+y)
    } ( end block of code)
    }; (ends coding with trailing, semi-colon)
    Last edited by HawkLord; 09-23-2006 at 02:42 PM.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    The ~Example_Class() is called a destructor. It's mainly used for memory management. This means that if you dynamically allocate memory (you may have heard of the new keyword), that you can delete that memory here. In that class example, there is no dynamically allocated memory, so there is no reason to delete it. The destructor is just put there for ... completion, and could be left out, but is there as an example. It's empty cause it has nothing to do.

    Class members are by definition private, which means that if you make an instance of the class, only somethings are accessible (things which are called under the public keyword). The public keyword says that that function/variable is accessible ... it's to do with encapsulation .. you'll come accross and become more familiar with it in time.

    What's your problem with int Add()?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    9
    Quote Originally Posted by twomers

    What's your problem with int Add()?
    I just dont understand what its for, please be patient with me

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> please be patient with me

    Heh, the worst I can do is ignore ya

    Anyways, classes are user-defined types. Think of an int or something. That's a type. So is a char or something, but a class is a type which is defined by the user. This means that it can have anything. One thing that classes possess are member functions, which are functions which belong only within that particular class, and more impartantly, they are relavent to that class -

    Here's a very simple example of a class, where there are two variables, and one function which adds the two integers.

    Code:
    class Add
    {
    private:
    	int x;
    	int y;
    
    public:
    	Add( void )
    	{
    		x = 2;
    		y = 33;
    	}
    
    	int Add_XY ( void )
    	{
    		return ( x+y );
    	}
    };
    
    int main( void )
    {
    	int result;
    
    	Add MyInstance;
    	result = MyInstance.Add_XY();
    
    	std::cout<< "The result of Add::Add_XY() is - " << result;
    
    	return 0;
    }
    Compile this, and see what happens.

    I underlined two parts of the code. The part within the class definition defines what happens when you call the function, just like other functions.
    Last edited by twomers; 09-23-2006 at 03:35 PM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    9
    I compiled it and it showed the result of add::add_xy() is -35. just to clarify does the code (void) mean to return?

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by HawkLord
    I compiled it and it showed the result of add::add_xy() is -35. just to clarify does the code (void) mean to return?
    Nothing. It's void of return.

    int funct() - returns an int
    char function() - returns a char
    etc
    void function() - returns nothing

    Also:

    int funct( void ) - returns an int
    char function( void ) - returns a char
    etc
    void function( void ) - returns nothing

    The (void)'s just mean that the functions don't take any parameters. It's strictly not necessary to have them there, but I'm somewhat pedantic about layouts etc.

    Do you understand why it returned 35 though?

    Here's another example -

    Code:
    class Add
    {
    private:
    	int x;
    	int y;
    
    public:
    	Add( int New_x, int New_y ) : x( New_x ), y( New_y )
    	// This is called an itilisation list. Just another way of intialising class members
    	{
    	}
    
    	int Add_XY ( void )
    	{
    		return ( x+y );
    	}
    };
    
    int main( void )
    {
    	int result, User_x, User_y;
    
    	std::cout<< "Enter int one: ";
    	std::cin >> User_x;
    
    	std::cout<< "Enter int two: ";
    	std::cin >> User_y;
    
    	Add MyInstance( User_x, User_y );
    	result = MyInstance.Add_XY();
    
    	std::cout<< "The result of Add::Add_XY() is: " << result;
    
    	return 0;
    }
    Last edited by twomers; 09-23-2006 at 03:34 PM.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Should you not therefore have void in the constructor, twomey?
    Last edited by bumfluff; 09-23-2006 at 03:30 PM.

  12. #12
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> Shouyld you not therefore have void in the constructor, twomey?

    Hush! It's not necessary, and I just forgot

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Ha, you spellt your quote wrong!

    Anyway, what twomers was intensionally showing you was bad programming practice by not sticking to the same syntax for similar coding.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    9
    i understood why it came up to 35. its because you declared x and y to 2 and 33. then you made a type add_xy which came up to 35.

    When I compiled your second code, something was wrong. When I enter the 2nd interger via cin the command window closes. I have cin.get(); to prevent from the window from closing but for some reason it wont stay open. I dont mean to judge your programming skills but maybe you made an error in your code?

  15. #15
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    There is none. Try a cin.ignore() too. or a system("pause"); if all else fails.

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, 09: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