创建操作方法 实际问题

Creating a method for action Real problem

提问人:JJGas 提问时间:3/29/2023 最后编辑:JJGas 更新时间:4/5/2023 访问量:39

问:

目标是为 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;
}
C++ 方法 子类

评论

0赞 wohlstad 3/29/2023
你不能像直接在类中那样有语句。它应该放在类的方法中。std::cout << ...
0赞 JJGas 3/29/2023
只要把它放在那里就可以了
0赞 Jabberwocky 3/29/2023
你是想使用遗产吗?完全不清楚你想实现什么。整件事没有任何意义......顺便说一句,没有C++“脚本”。
0赞 Botje 3/29/2023
你能用通俗易懂的英语解释一下你想完成什么吗?因为 C++ 代码没有任何意义。
0赞 Jabberwocky 3/29/2023
也许你只需要谷歌 c++ 中的简单继承示例

答: 暂无答案