如何使这些子类相互交互?

时间:2013-08-31 17:26:09

标签: c++ class pointers reference polymorphism

我有这个口袋妖怪应用程序,我想让这些口袋妖怪“战斗”我想让健康成为一个引用变量,它位于pokemon类的私有部分。我不知道如何在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;
}

2 个答案:

答案 0 :(得分:0)

使health变量protected代替private。然后,您将能够在从Pokemon派生的每个类中访问它。

答案 1 :(得分:0)

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

class A {
  friend class B;
}

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