如何使这些子类相互作用

How to make these subclasses interact with each other?

本文关键字:相互作用 子类 何使这      更新时间:2023-10-16

我有这个pokemon应用程序,我想让这些pokemon"战斗"我想让health成为一个参考变量,它位于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 << "tttlevel: "  <<  level << endl;
        cout << "ttthp: "     << health << endl;
        cout << "tttheight: " << height << endl;
        cout << "tttweight: " << 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 << "nttt <-------------Pokedex------------->" << endl;
    bool quit = false;
    while (!quit) {
        cout << "Slect a Pokemon to view stats: "<< "ntt 1-Pickachew" << "ntt 2-Squirtle" <<  "ntt 3-Charmander" <<  "ntt 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 << "nntt 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;
}

health设为变量protected而不是private。然后,您将能够在从Pokemon派生的每个类中访问它。

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

class A {
  friend class B;
}

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