C++ 在 OOP 中访问其他实例的数据

C++ Accessing other instances' data in OOP

本文关键字:实例 数据 其他 访问 OOP C++      更新时间:2023-10-16

我的OOP作业有问题。我必须编码一场1对1的战斗,将这些事情考虑在内:

回合顺序、命中率和伤害加成。

所有这三个都依赖于进攻方和防守方的数据。

我的问题是,如何访问来自同一类的另一个实例的数据?因为我有一个函数opponentSpawner(),它创建了我的Unit类的另一个实例。

你能帮我理解我做错了什么吗?

这是我的头文件(Unit.h):

#pragma once
#include <string>
class Unit
{
public:
    // Constructor and Destructor
    Unit();
    ~Unit();
    // Getters
    std::string getName();
    std::string getCharClass();
    int getHp();
    int getPow();
    int getVit();
    int getAgi();
    int getDex();
    // Basic functions
    void createCharacter();
    void createOpponent();
    void printStats();
    int attack();
    int checkClass();
private:
    std::string mName;
    std::string mCharClass;
    int mHp;
    int mPow;
    int mVit;
    int mAgi;
    int mDex;
};

这是我的.cpp文件(Unit.cpp):

#include "Unit.h"
#include <string>
#include <iostream>
#include <ctime>
Unit::Unit()
{
}
Unit::~Unit()
{
}
std::string Unit::getName()
{
    return mName;
}
std::string Unit::getCharClass()
{
    return mCharClass;
}
int Unit::getHp()
{
    return mHp;
}
int Unit::getPow()
{
    return mPow;
}
int Unit::getVit()
{
    return mVit;
}
int Unit::getAgi()
{
    return mAgi;
}
int Unit::getDex()
{
    return mDex;
}
void Unit::createCharacter()
{
    int choice;
    std::string name;
    std::cout << "[1] Warrior" << std::endl;
    std::cout << "[2] Assassin" << std::endl;
    std::cout << "[3] Mage" << std::endl;
    std::cout << "Select your class: ";
    std::cin >> choice;
    std::cout << std::endl;
    std::cout << "Enter your name: ";
    std::cin >> name;
    if (choice == 1)
    {
        mName = name;
        mCharClass = "Warrior";
        mHp = 20;
        mPow = 10;
        mVit = 10;
        mAgi = 5;
        mDex = 5;
    }
    else if (choice == 2)
    {
        mName = name;
        mCharClass = "Assassin";
        mHp = 15;
        mPow = 5;
        mVit = 5;
        mAgi = 10;
        mDex = 15;
    }
    else if (choice == 3)
    {
        mName = name;
        mCharClass = "Mage";
        mHp = 10;
        mPow = 15;
        mVit = 5;
        mAgi = 10;
        mDex = 10;
    }
    system("cls");
}
void Unit::createOpponent()
{
    srand(unsigned int(time(0)));
    int choice = (1 + rand() % 3);
    if (choice == 1)
    {
        mName = "Enemy Warrior";
        mCharClass = "Warrior";
        mHp = 20;
        mPow = 10;
        mVit = 10;
        mAgi = 5;
        mDex = 5;
    }
    else if (choice == 2)
    {
        mName = "Enemy Assassin";
        mCharClass = "Assassin";
        mHp = 15;
        mPow = 5;
        mVit = 5;
        mAgi = 10;
        mDex = 15;
    }
    else if (choice == 3)
    {
        mName = "Enemy Mage";
        mCharClass = "Mage";
        mHp = 10;
        mPow = 15;
        mVit = 5;
        mAgi = 10;
        mDex = 10;
    }
}
void Unit::printStats()
{
    std::cout << "Name: " << getName() << std::endl;
    std::cout << "Class: " << getCharClass() << std::endl;
    std::cout << "HP: " << getHp() << std::endl;
    std::cout << "POW: " << getPow() << std::endl;
    std::cout << "VIT: " << getVit() << std::endl;
    std::cout << "AGI: " << getAgi() << std::endl;
    std::cout << "DEX: " << getDex() << std::endl;
}
int Unit::attack()
{
    int damage = (this->getPow() - getVit());
    if (checkClass() == 1)
    {
        damage * 1.5;
        return damage;
    }
    if (checkClass() == 2)
    {
        damage * .5;
        return damage;
    }
    if (checkClass() == 3)
    {
        return damage;
    }
}
int Unit::checkClass()
{
    if (getCharClass() == "Warrior")
    {
        if ()
    }
}

这是我的主要.cpp:

#include "Unit.h"
#include <iostream>
#include <string>
void opponentSpawner();
int main()
{
    Unit* unit1 = new Unit();
    unit1->createCharacter();
    unit1->printStats();
    system("pause");
    system("cls");
    opponentSpawner();
    system("pause");
    system("cls");
    system("pause");
    return 0;
}
void opponentSpawner()
{
    // Flavor texts
    std::cout << "An enemy is approaching the Arena!" << std::endl;
    system("pause");
    system("cls");
    Unit* unit2 = new Unit();
    unit2->createOpponent();
    unit2->printStats();
}

如果您想访问opponentSpawner中的第一个实例,可以从方法参数传递实例。。

void opponentSpawner(Unit* u);

如果你想访问你在opponentSpawner中创建的实例。

你可以使用

 Unit* opponentSpawner();