提问人:JJGas 提问时间:3/29/2023 最后编辑:JJGas 更新时间:4/5/2023 访问量:39
创建操作方法 实际问题
Creating a method for action Real problem
问:
目标是为 Character 类的两个子类创建一个构造函数,以初始化 Character 类和子类中的所有公共和私有属性。我做错了什么?
我已经知道要做到这一点,我开始在每个类中放置两个单独的构造函数,但仍然有点困惑。代码如下:
// RequimOcculation.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <string>
class GameStructure { //Creating Base Class; derivative of Character class.
private: //Private property; for what to not be shown
int WarpWeapon[4] = {25, 16, 50, 100};// interger for WarpWeapon is assign an array of 4 elements
int gun [3] = {50, 10, 5}; // interger for Gun is assign an array of 3 elements
};
class Characters:public GameStructure { //class derived class is created
public: // public property
std::string Name; // string name
void trackDamage(int d) {// creating setter
health = d;
if (d==!100)
{
std::cout << "Bloop; Character has expired!\n";
}
else
{
std::cout << "Battle!\n";
}
}
int showDamage() { //created getter
return health;
}
class Speaking : public Characters {
public:
void Talk() {
std::cout << "Take that!";
}
};
private: //private property
int health; //interger for health is assign with 100
};
class Humans : public Characters { //class humans is created to derive from characters
std::string person = "Firing"; // creation of string person with the assignment of Firiing
public: //public property
void action() { //void function action
std::cout << person << "Pow Pow (Sound effect)!!!" << std::endl; // text to be seen with the person function
}
// Trigger1::Trigger1(int gun) { //attempt to create a constructor using as the object interger gun given from game structure classs
//}
};
class Aliens : public Characters { //class derive from the charaters class called Aliens
std::string green = "Blasting";
public:
void action() {
std::cout << green <<"Bling Bling(Sound effect)!!!" << std::endl;
}
//trigger::trigger(int WarpWeapon) {
//}
};
int main()
{
std::cout << "In the far far future,humanity now lives in glass dome cities\n";
std::cout << "All of sudden... ";
std::cout << "Warp(sound effect)...";
std::cout << "Aliens evade dome city sector A01 so it is time to battle." << std::endl;
Characters myCharacter1;
myCharacter1.Talk();
Characters showHe;
showHe.trackDamage(0);
std::cout << showHe.showDamage();
return 0;
}
答: 暂无答案
下一个:如何从外部将类的方法标记为虚拟?
评论
std::cout << ...