C ++派生类构造函数问题

时间:2014-08-17 02:22:33

标签: c++ constructor

我的构造函数存在一些问题,因为目前只使用了基本构造函数,因为我的多个参数构造函数没有被使用。生物是我抽象的祖父母类,而战士来自生物,地精来自战士

combatant fighter_1;
combatant fighter_2;
do
{
    cout << "Please pick the first combatant, enter 1 for a Goblin, \n";
    cout << "enter 2 for a Barbarian, enter 3 for a Reptile person, \n" ;
    cout << "enter 4 for a Blue Men, enter 5 for a Beserker, \n";
    cout << "enter 6 for a hobbit. \n";
    cin >> pick;
    checker=pick;
        if (cin.fail()||checker!=pick||pick<1||pick>6)
        {
            cout << "You have entered an invalid input.\n";
            cin.clear();
            cin.ignore(100, '\n');
            pick=-1;
        }
}
while (checker!=pick||pick<1||pick>6); //makes sure number entered is valid
if (pick==1)
{
goblin fighter_1("Goblin",6,6,0,6,0,0,3,8);
}

我为每个生物类都有一个构造函数,但每个生成器的行为与goblin相同

creature::creature(): chartype("no name yet"), adice1(0), 
adice2(0), adice3(0),ddice1(0), ddice2(0), ddice3(0), defense(0),health(0)
{

}

combatant :: combatant(): creature()
{

}



creature::creature( string name, int attack1, int attack2, int attack3, int def1, int    def2,
int def3, int defense, int health): chartype(name), adice1(attack1), adice2(attack2), 
adice3(attack3),ddice1(def1), ddice2(def2), ddice3(def3),  defense(defense),health(health)
{

}

combatant::combatant(string name, int attack1, int attack2, int attack3, int def1, int  def2,
int def3, int defense, int health) :creature(name, attack1, attack2, attack3, def1, def2,
def3, defense, health)
{

}


goblin::goblin(string name, int attack1, int attack2, int attack3, int def1, int def2,
int def3, int defense, int health) :combatant(name, attack1, attack2, attack3, def1, def2,
def3, defense, health)
{

}

2 个答案:

答案 0 :(得分:0)

通常,当if正文中的单个陈述未执行时(因为您在此处报告的情况),由于if条件不是{{1}而导致}。

答案 1 :(得分:0)

我从几个月前拿到了这段代码,并意识到我没有关闭我应该拥有的最新版本

fighter_1 = goblin("Goblin",6,6,0,6,0,0,3,8);