Thread: Inheritance trouble

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) Every object is created with a constructor.

    2) Every base class portion of a derived class object is created with a base class constructor. When a derived class constructor is called, the first thing that happens is that a base class constructor is called to construct the base class portion of the derived object.

    The way you have your code now, when a Defiler object is constructed, since you do not explicitly call a base class constructor, the default base class constructor is called automatically. Your default base class constructor creates the base class portion of the derived object with these values:

    Enemy(): hp(20), sp(0), flee(50), hit(50), atkPower(8) {};

    but then you immediately change those values to:

    Defiler(){hp = 30, sp = 10, flee = 60, hit = 60, atkPower = 8;};

    That's inefficient. You should define another constructor in the base class that accepts parameters:

    Code:
    Enemy(int num1, int num2, int num3, int num4, int num5):
             hp(num1), sp(num2), flee(num3), hit(num4), atkPower(num5)
    {
    }
    Then call that from your derived class constructor:
    Code:
    class Defiler : public Enemy
    {
    public:
    	Defiler() : Enemy(30, 10, 60, 60, 8)
    	{
    	}
    However, I think you may be getting confused with the difference between default constructors and constructors that take parameters. To keep it simple, try defining your default constructor to just set all the variables equal to zero:
    Code:
    #pragma warning ( disable : 4786 )
    #include <iostream>
    
    using namespace std;
    
    class Enemy
    {
    public:
    	Enemy():hp(0), sp(0), flee(0), hit(0), atkPower(0) 
    	{
    	}
    
    	Enemy(int num1, int num2, int num3, int num4, int num5): 
    			hp(num1), sp(num2), flee(num3), hit(num4), atkPower(num5)
    	{
    	}
           
    	void atk();
    	void takeDmg();
    
    	int get_hp()
    	{
    		return hp;
    	}
           
    protected: 
    	int hp;
    	int sp;
    	int flee;
    	int hit;
    	int atkPower;
    
    };
    
    class Defiler : public Enemy
    {
    public:
    	Defiler(): Enemy(), defileMultiply(0)
    
    	{
    	}
    	
    	Defiler(int dM, int num1, int num2, int num3, int num4, int num5):
    			Enemy(num1, num2, num3, num4, num5), 
    			defileMultiply(dM)
    	{
    	}
         
    	void dfileAtk();
    
    	int get_defileMultiply()
    	{
    		return defileMultiply;
    	}
    
    private:
    	int defileMultiply;      
    };
    
    int main()
    {
    	Defiler defOne(9999,30,10,60,60,8);
    	cout << defOne.get_hp()<<endl;
    	cout<<defOne.get_defileMultiply();
    
    	Enemy noOne;
    	cout << endl << noOne.get_hp();
    
    	cin.get();
    	
    	return 0;    
    }
    All the intializer lists can make things confusing too, so you may want to make things even simpler:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Enemy
    {
    public:
    	Enemy()
    	{
    		hp = sp = flee = hit = atkPower = 0;
    	}
    
    	Enemy(int num1, int num2, int num3, int num4, int num5) 
    	{
    		hp = num1;
    		sp = num2;
    		flee = num3;
    		hit = num4;
    		atkPower = num5;
    	}
           
    	void atk();
    	void takeDmg();
    
    	int get_hp()
    	{
    		return hp;
    	}
           
    protected: 
    	int hp;
    	int sp;
    	int flee;
    	int hit;
    	int atkPower;
    
    };
    
    class Defiler : public Enemy
    {
    public:
    	Defiler(): Enemy()
    
    	{
    		defileMultiply = 0;
    	}
    	
    	Defiler(int dM, int num1, int num2, int num3, int num4, int num5):
    			Enemy(num1, num2, num3, num4, num5) 
    			
    	{
    		defileMultiply = dM;
    	}
         
    	void dfileAtk();
    
    	int get_defileMultiply()
    	{
    		return defileMultiply;
    	}
    
    private:
    	int defileMultiply;      
    };
    
    int main()
    {
    	Defiler defOne(9999,30,10,60,60,8);
    	cout << defOne.get_hp()<<endl;
    	cout<<defOne.get_defileMultiply();
    
    	Enemy noOne;
    	cout << endl << noOne.get_hp();
    
    	cin.get();
    	
    	return 0;    
    }
    Last edited by 7stud; 04-01-2006 at 04:19 PM.

  2. #2
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Thanks guys. I got my getHp function to work and decided to not set any values in the base class and just set values in the derived classes. Don't know if that's effecient?

    This is what my code looks like now.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Enemy
    {
    public:
           
           void atk();
           void takeDmg();
           
           int getHp(){return hp;}
           int getSp(){return sp;}
           int getFlee(){return flee;}
           int getHit(){return hit;}
           int getAtkPower(){return atkPower;}
           
           void setHp(int HP){hp = HP;}
           void setSp(int SP){sp = SP;}
           void setFlee(int FLEE){flee = FLEE;}
           void setHit(int HIT){hit = HIT;}
           void setAtkPower(int ATKPOWER){atkPower = ATKPOWER;}
                  
    protected:
    
              int hp;
              int sp;
              int flee;
              int hit;
              int atkPower;
          
    };
    
    class Defiler : public Enemy
    {
    public:
    
          Defiler()
          {hp = 30, sp = 10, flee = 60, hit = 60, atkPower = 8, dMultiply = 2;};
          
          void defileAtk();
    
    private:
    
            int dMultiply;      
    };
    
    class Blood : public Enemy
    {
    public:
          Blood()
          {hp = 35, sp = 10, flee = 50, hit = 70, atkPower = 8, bMultiply = 2;};
          
          void bloodAtk();
          
    private:
            int bMultiply;      
    };
    
    class Violence : public Enemy
    {
    public:
          Violence()
          {hp = 50, sp = 20, flee = 70, hit = 80, atkPower = 8, vMultiply = 3;};
          
          void ViolenceAtk1();
          
    private:
            int vMultiply;      
    };
    
    int main()
    {
        Defiler defOne;
        cout << defOne.getHp();
        Blood noOne;
        cout << endl << noOne.getHp();
        noOne.setHp(10);
        cout << endl << noOne.getHp();
        cin.get();
    return 0;    
    }
    Now for the functions

  3. #3
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    I need more help. I'm having trouble with the atk() function.

    Code:
    class Hero
    {
    public:
           //Hero(){};
           
           void atk(&Enemy);
           void takeDmg();
           
           int getHp(){return hp;}
           int getSp(){return sp;}
           int getFlee(){return flee;}
           int getHit(){return hit;}
           int getAtkPower(){return atkPower;}
           
           void setHp(int HP){hp = HP;}
           void setSp(int SP){sp = SP;}
           void setFlee(int FLEE){flee = FLEE;}
           void setHit(int HIT){hit = HIT;}
           void setAtkPower(int ATKPOWER){atkPower = ATKPOWER;}
                  
    protected:
              
              int hp;
              int sp;
              int flee;
              int hit;
              int atkPower;
          
    };
    
    void Hero::atk(&Enemy)
    {
               int fleeRoll, flee, hit;
               flee = *Enemy.getFlee();
               hit = *Hero.getHit();
               fleeRoll = flee - hit;   
    }
    Still really confused about pointers and references

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Inheritance and program structure planning please help a newbie
    By ninjacookies in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2005, 12:18 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM