0

我有这个口袋妖怪应用程序,我想让这些口袋妖怪“战斗”我想让健康成为一个参考变量,它位于口袋妖怪类的私有部分。我不知道如何在 c++ 类中初始化引用变量。感谢您花时间阅读我的帖子。我希望有人能帮助我。

//
//  main.cpp
//  pokemon
//
//  Created by Frank Novello on 8/25/13.
//  Copyright (c) 2013 Frank Novello. All rights reserved.
//

#include <iostream>

using namespace std;
//<------------------------------------------------------------------------------------>
class Pokemon
{
public:
    Pokemon(){};
    ~Pokemon(){};

    void setName(string x){name = x;};
    string getName(){return name;};

    void setLevel(int x){level = x;};
    int getLevel(){return level;};

    void setHeight(int x){height = x;};
    int getHeight(){return height;};

    void setWeight(int x){weight = x;};
    int getWeight(){return weight;};

    void setHealth(int x){health = x;};
    int getHealth(){return health;};


    void displayStats()
    {
        cout << "pokemon: "      <<   name << endl;
        cout << "\t\t\tlevel: "  <<  level << endl;
        cout << "\t\t\thp: "     << health << endl;
        cout << "\t\t\theight: " << height << endl;
        cout << "\t\t\tweight: " << weight << endl;
    }

private:
    string name;
    int level;
    int height;
    int health;     //Want to make this a reference variable
    float weight;
};
//<------------------------------------------------------------------------------------------------->
class ElectricPokemon: public Pokemon
{
public:
    ElectricPokemon(){};
    ~ElectricPokemon(){};
    void attack()
    {
        cout <<"Shocked" << endl;
    }
private:
};
//<------------------------------------------------------------------------------------------------->
class WaterPokemon: public Pokemon
{
public:
    WaterPokemon(){};
    ~WaterPokemon(){};
private:
};
//<------------------------------------------------------------------------------>
class FirePokemon: public Pokemon
{
public:
    FirePokemon(){};
    ~FirePokemon(){};
private:
};
//<------------------------------------------------------------------------------------->
class PsycicPokemon: public Pokemon
{
public:
    PsycicPokemon(){};
    ~PsycicPokemon(){};
private:
};
//<------------------------------------------------------------------------------->

int main(int argc, const char * argv[])
{
    ElectricPokemon * pickachew = new ElectricPokemon();
    WaterPokemon * squirtle = new WaterPokemon();
    PsycicPokemon * kahdabra = new PsycicPokemon();
    FirePokemon * charmander = new FirePokemon();
    cout << "\n\t\t\t <-------------Pokedex------------->" << endl;
    bool quit = false;

    while (!quit) {

        cout << "Slect a Pokemon to view stats: "<< "\n\t\t 1-Pickachew" << "\n\t\t 2-Squirtle" <<  "\n\t\t 3-Charmander" <<  "\n\t\t 4-Kahdabra"<< endl;
        int x;
        cin >> x;

        switch (x) {
            case 1:
                pickachew -> setName("Pickachew");
                pickachew -> setLevel(1);
                pickachew -> setHealth(100);
                pickachew -> setHeight(1);
                pickachew -> setWeight(10);
                pickachew -> displayStats();
                break;
            case 2:
                squirtle -> setName("Squirtle");
                squirtle -> setLevel(1);
                squirtle -> setHealth(80);
                squirtle -> setHeight(1);
                squirtle -> setWeight(15);
                squirtle -> displayStats();
                break;
            case 3:
                charmander -> setName("Charmander");
                charmander -> setLevel(1);
                charmander -> setHealth(120);
                charmander -> setHeight(1);
                charmander -> setWeight(15);
                charmander -> displayStats();
                break;
            case 4:
                kahdabra -> setName("Kahdabra");
                kahdabra -> setLevel(60);
                kahdabra -> setHealth(800);
                kahdabra -> setHeight(6);
                kahdabra -> setWeight(150);
                kahdabra -> displayStats();
                break;
        }
        cout << "\n\n\t\t Would you like to select another Pokemon(y/n): " << endl;
        string choice;
        cin >> choice;

        if (choice == "n")
        {
            quit = true;

        }else quit = false;
    }

    cout << "Program Quiting..." << endl; return 0;
}
4

2 回答 2

0

使health变量protected而不是private. 然后,您将能够在每个派生自的类中访问它Pokemon

于 2013-08-31T17:47:07.040 回答
0

在 C++ 中,您可以使用关键字从另一个类或对象类型访问私有成员函数和受保护成员函数friend。您需要在头文件中包含包含私有成员函数或受保护成员函数的类,例如

class A {
  friend class B;
}

你也可以反过来做。因此 B 类可以是 A 类的朋友,A 类可以是 B 类的朋友。

于 2013-08-31T22:37:54.053 回答