C ++正确的getter和setter

时间:2014-02-16 20:37:24

标签: c++ c++11 getter-setter

我正在编写一个程序,需要“模拟”两个口袋妖怪之间的战斗。我有一个userBuild类来创建口袋妖怪。每次用户创建一个宠物小精灵时都会调用此类。我遇到错误的地方是我添加getter和setter:

#include "Pokemon.h"
#include <string>
#include "Dice.h"

Pokemon::Pokemon() {
    healthPoints = attackL = defL =0;
        std::string Pname= "";
        d20=Dice(20);
        d6=Dice(6);
    }   

bool Pokemon::attack(Pokemon opponent){
int attackBonus = d20.roll();
int defenseBonus = d20.roll();


std::cout<<Pname<<" rolls an attack bonus of "<<attackBonus<<std::endl;
std::cout<<this -> Pname<<" rolls an defense bonus of "<<defenseBonus<<std::endl;

//if the attackLevel+attackBonus of the attacker is greater than the the defenseLevel+defenseBonus of the defender, then roll for damage. Otherwise the attack misses

if (attackL+attackBonus >= opponent.defL+defenseBonus){//Pokemon 1 attack
    int roll1, roll2, roll3;    //bonus 3 d6 rolls
    roll1 = d6.roll();
    roll2 = d6.roll();
    roll3 = d6.roll();

    int totalDamage = roll1 + roll2 + roll3;

    std::cout<<"The attack hits dealing 3-D6 damage!"<<std::endl;
    std::cout<<"The rolls are "<<roll1<<", "<<roll2<<", and "<<roll3<<" totalling: "<<totalDamage<<" damage!"<<std::endl;
    std::cout<<opponent.Pname<<" has"<<(opponent.healthPoints)- totalDamage<<"hit points left"<<std::endl;

    if (opponent.healthPoints <= 0){
        return true;
    }

}
else if (attackL+attackBonus <= opponent.defL+defenseBonus){
    std::cout<<"The attack missed"<<std::endl;
}
return false;
}

void Pokemon::userBuild(){


    std::cout<< "Please name your Pokemon: ";
    std::cin>>Pname;

    std::cout<< "How many hit points will it have? (1-50): ";
    std::cin>>healthPoints;

    std::cout<<"Split fifty points between attack level and defense level"<<'\n';
    std::cout<<"Enter your attack level (1-49): ";
    std::cin>>attackL;

            if (attackL > 49) { //checks that the input for attack level is within the acceptable range
                std::cout<<"Sorry. The attack level must be between 1 and 49: ";
                std::cin>>attackL;
            }
                else if (attackL <= 0) {
                 std::cout<<"Sorry. The attack level must be between 1 and 49: ";
                 std::cin>>attackL;
                }


    std::cout<<"Enter your defense level (1-30): "<<std::endl;
    std::cin>>defL;



            if (defL > 30) {    //checks that the input for defense level is within the acceptable range
                std::cout<<"Sorry. The defense level must be between 1 and 30: ";
                std::cin>>defL;
            }

                else (defL <= 0);{
                 std::cout<<"Sorry. The defense level must be between 1 and 30: ";
                 std::cin>>defL;
                }



}



//initiazion of getters
    int Pokemon::getHP(){

        return healthPoints;
    }
    int Pokemon::getAttackLevel(){
        return attackL;
    }
    int Pokemon::getDefenseLevel(){
        return defL;
    }
    std::string Pokemon::getname(){
        return Pname;
    }

    //initiation of setters
    void setHP(int HP){
        healthPoints=HP;
    }
    void setAttackLevel(int attackLevel){
        attackL=attackLevel;
    }
    void setDefenseLevel(int defenseLevel){
        defL=defenseLevel;
    }
    void setname(std::string name){
        Pname= name;
    }

1 个答案:

答案 0 :(得分:1)

你没有将setHP函数的定义作为Pokemon类的成员。它应该是

void Pokemon::setHP(int HP){
        healthPoints=HP;
    }

那里的其他功能大概也应该在这个范围内。