Thread: how to receive a class object in a function?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    how to receive a class object in a function?

    Completely stumped on how to do this. It is a class project in which we must make witches and wizards battle at rock, paper, scissors. We are given the code for class "sorcerer", and we have to create two classes "witch" and "wizard" that inherit class "sorcerer." In both classes "witch" and "wizard" we have to implement a function that receives a sorcerer object that is passed by reference.

    class sorcerer is as follows

    Code:
    #ifndef _SORCERER_H_
    #define _SORCERER_H_
    
    #include <string>
    using namespace std;
    
    class sorcerer
    {
    public:
        sorcerer();
        sorcerer(string initName, double initStrength);
        string getName();
        double getStrength();
        void   takeStrength(sorcerer & s);    
    
    private:
        double strength; 
        string name;   
    };
    
    #endif

    Code:
    #include "sorcerer.h"
    
    sorcerer::sorcerer()
    {
    	name = "";
        strength = 0.0;
    }
    
    sorcerer::sorcerer(string initName, double initStrength)
    {
        name = initName;
    
        if (initStrength > 0)    
            strength = initStrength;
        else 
            strength = .1;
    }
        
    string sorcerer::getName()
    {
        return name;
    }
    
    double sorcerer::getStrength()
    {
        return strength;
    }
    
    void sorcerer::takeStrength(sorcerer & s)
    {
        s.strength /= 2;
        strength += s.strength;
    }
    class wizard is as follows

    Code:
    #ifndef _WIZARD_H_
    #define _WIZARD_H_
    
    #include "sorcerer.h"
    
    class wizard : public sorcerer
    {
       public:
         wizard();
         wizard(string initName, double initStrength, int initMull);
         int getMull();
    
       private:
         int mulligan;
    };
    #endif
    Code:
    #include "wizard.h"
    
    wizard:: wizard() : sorcerer()
    {
        mulligan = 0;
    }
    
    wizard:: wizard(string initName, doubleinitStrength, int initMull)
                 : sorcerer(initName, initStrength)
    {
        if (initMull > 0 && initMull < 5)
           mulligan = initMull;
        else
           mulligan = 0;
    }
    
    int wizard::getMull()
    {
         return mulligan;
    }
    Basically I need to know how to write a function called "fight" in class wizard that will receive a sorcerer object by reference and have them battle at rock, paper scissors. Any help would be greatly appreciated

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    does it have to be in the wizard class?

    im not very good at classes yet but i would make a fight command like this and make it a friend of both the classes, or add another base class "fighter" to allow it
    Code:
    void fight(wizard wizard_1; witch witch_1)
    {
          int wizards_choice = rand % 3; witches_choice % 3;
          switch (wizards_choice)
          { 
    // and so on

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How far have you gotten into polymorphism? I'd make fight a virtual function inside the base class, Wizard, and provide appropriate virtual functions in the derived classes to give the base class the required information to perform the fight.
    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
    Feb 2011
    Posts
    4
    Yes, it has to be in the wizard class. We aren't allowed to make any changes to sorcerer or add any other classes besides a witch and wizards class that both have the same fight function.

    This is my first project with classes, haven't gotten to virtual functions yet.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    So make a fight function:
    Code:
    class wizard : public sorcerer
    {
       public:
        ...
            void fight(sorcerer& enem);
    };
    
    bool wizard::fight(sorcerer& enemy)
    {
         ... //return true if you win, false if you lose
    }
    Code:
    class witch: public sorcerer
    {
       public:
        ...
            void fight(sorcerer& enem);
    };
    
    bool witch::fight(sorcerer& enemy)
    {
         ... //return true if you win, false if you lose
    }
    and use like
    Code:
    int main()
    {
       wizard wiz = new wizard("Good guy", 10, 10);
       witch wit = new witch("Bad gal", 10, 10);
       //The wizard attacks the witch
       wiz.fight(wit);
       //The witch attacks the wizard
       wit.fight(wiz);
    }
    You could make this function friend as mentioned which is the best.

    The only "problem" would be if you wanted to use a wizard-specific or witch-specific variable, like mulligan for the wizard, inside the fight function. Then you will have to know what the sorcerer object passed to fight is (either sorcerer, wizard or witch). You will need to use dynamic_cast<> to do this if you couldn't alter the classes.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by C_ntua View Post
    Code:
    int main()
    {
       wizard wiz = new wizard("Good guy", 10, 10);
       witch wit = new witch("Bad gal", 10, 10);
       //The wizard attacks the witch
       wiz.fight(wit);
       //The witch attacks the wizard
       wit.fight(wiz);
    }
    What's with these new? Seriously, at least stick to C++ syntax. This won't even compile! This is not some professional we're talking about. This is a newbie. Let's not confuse people.

    But if I understand correctly, you don't need friend functions. The winner of the battle should call winner.TakeStrength(loser) and should be over... right?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM