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;
}