如何使这些子类相互作用
How to make these subclasses interact with each other?
我有这个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的朋友。
相关文章:
- 继承期间显示未知行为的子类
- 通过指向指针数组的指针访问子类的属性
- 从父类方法返回子类对象
- c++, 在子类中,如何在没有对象的情况下访问父类的方法?
- 将父类对象强制转换为子类的问题
- 避免在C++中重复子类定义
- 将QOpenGLWidget子类转换为使用Metal而不是OpenGL的子类是否可行?
- 如何初始化矢量的模板化子类
- C++ 继承:将子类传递给需要基类的函数并获取子类行为
- 有没有办法按值将纯抽象类的所有子类传递给 C++ 中的函数?
- 使用子类覆盖基类中定义的函数
- 子类地址等于虚拟基类地址?
- 将子类方法声明为基类的友元
- C++子类共享变量?
- 如何检查模板专用化是否是基本模板的子类?
- 仅让特定类'Fabric'构造类'Foo'及其所有子类的实例
- 使用模板参数重载C++方法:如何使其适用于模板的子类?
- 基类中的默认析构函数禁用子类中的移动构造函数(如果有成员)
- 如何在C++子类中访问父类的私有变量
- 如何使这些子类相互作用