如何使用不同的文件初始化具有重载构造函数的对象

时间:2013-06-22 17:27:41

标签: c++ class oop object constructor

我尝试过几乎所有尝试初始化构造函数,以便在创建对象并传递参数时,它不会给我一个疯狂的负数,但它仍然不起作用!

为了测试它的工作原理,我使用了getHp和攻击函数。例如,如果我说Hero Hi(100,200,300,400),那么我说Hi.getHP(),它应该是100(第一个参数)......

//main.cpp
    int main()
    {
        Hero Me(100,20,30,40);//Created using overloaded constructor
        Monsters m(100,16,18,20);//creates a monster object and uses overloaded constructor
     to initialize
        cout << "\ntest1\n";
        Me.getHp();//expecting 100
        Me.getAttack();//expecting w.e is in parameters

        m.getHp();//expecting 100
        m.getAttack();//same as hero


        cin.sync();
        cin.get();
        return 0;
    }

以下是我的其余代码,以备您需要时使用。我还省略了标题等简单的代码。

    //Characters.h
    class Characters
    {
    private:
        int level;
        int hp;
        int attack;
        int defense;
    protected:
        Characters(); // zero everything by default
        Characters(int, int, int, int); // populate explicitly
        ~Characters();
    public:
        int getAttack() const { return attack; }
        int getDefense() const { return defense; }
        int getHp() const { return hp; }
        int getlevel() const { return level; }

        void setAttack(int);
        void setDefense(int);
        void setStrength(int);
        void setHp(int);
        void setlevel(int);
        void damageTaken(int);
    };

    //Characters.cpp
    Characters::Characters() : level(0), hp(0), attack(0), defense(0) {}
    //
    //Characters::Characters(int seed)
    //{
    //    // NB. your code still doesn't initialize hp, strength etc.
    //    // it also logs level before initializing it, so that will be garbage
    //}

    //Characters::Characters(int hit, int lvl, int att, int def)
    // : level(lvl), hp(hit), attack(att), defense(def){}

    Hero::Hero(int newHp, int newLevel, int newAttack, int newDef)
        : Characters(newHp, newLevel, newAttack, newDef)
    {
        cout << "Hero created using Overloaded function!\n";
        HeroHp = newHp;
        cout << "Hp is: "<< HeroHp << endl;
        Herolevel = newLevel;
        cout << "level is: " << Herolevel << endl;
        HeroAttack = newAttack;
        cout << "Attack is: " << HeroAttack << endl;
        HeroDefense = newDef;
        cout << "Defense is: " << HeroDefense << endl;
        // logging goes here
        // note that you don't need HeroLevel etc. at all any more, just use level
    }

    Monsters::Monsters(int newHp, int newLevel, int newAttack, int newDef)
        : MonsterHp(newHp), Monsterlevel(newLevel), MonsterAttack(newAttack)
    , MonsterDefense(newDef)//initialize list
    {
        cout << "Monster created using Overloaded function!\n";
        MonsterHp = newHp;
        cout << "Hp is: "<< MonsterHp << endl;
        Monsterlevel = newLevel;
        cout << "level is: " << Monsterlevel << endl;
        MonsterAttack = newAttack;
        cout << "Attack is: " << MonsterAttack << endl;
        MonsterDefense = newDef;
        cout << "Defense is: " << MonsterDefense << endl;
    }


    Characters::~Characters()
    {
        cout << "Character has been destroyed!\n";
    }

    void Characters::setAttack(int att)
        {
            attack = att;
        }

    void Characters::setDefense(int def)
        {
            defense = def;
        }

    void Characters::setHp(int health)
        {
            hp = health;
        }

    void Characters::damageTaken(int damage)
        {
            hp -= damage;
        }

    void Characters::setlevel(int lvl)
        {
            level = lvl;
        }

    //Monsters.h
    class Monsters:
        public Characters //Hero
    {
    private:
        int Monsterlevel;
        int MonsterHp;
        int MonsterStrength;
        int MonsterAttack;
        int MonsterDefense;
    public:
        Monsters(int, int, int, int); //explicit
        ~Monsters();
    };

    //Monsters.cpp
    Monsters::~Monsters()
    {
        cout << "\nMonster Destroyed";
    }

    //Hero.h
    class Hero:
        public Characters
    {
    private:
        int Herolevel;
        int HeroHp;
        int HeroStrength;
        int HeroAttack;
        int HeroDefense;

    public:
        //Hero();
        Hero(int, int, int, int);
        ~Hero();

    };

    //Hero.cpp
    Hero::~Hero()
    {
        cout << "Hero destroyed!\n";
    }

2 个答案:

答案 0 :(得分:0)

HeroHp类中的

Herohp类中的Characters是单独的变量。 Hero的构造函数从不初始化hp,这是getHp返回的变量。

您可能希望消除HeroXXX类中的Hero变量,并使用Characters基类中的相应变量。

答案 1 :(得分:0)

如果我取消注释Characters构造函数,它可以正常工作:http://coliru.stacked-crooked.com/view?id=261944b9b1304c3ed9d76e69b2318ee2-ad7854d9cfd7979d567ca413f0830b65

你确定你正在编译成功吗?你留下了一些可能欺骗了你的警告...一个很好的理由去除总是你的所有警告。