面向对象C++帮助?出于某种原因,对象变量的输出返回数值数组位置而不是实际值

Object Oriented C++ Help? For some reason, output of object variables is returning numerical array locations instead of actual values.

本文关键字:数组 返回 位置 输出 帮助 C++ 某种原因 变量 对象 面向对象      更新时间:2023-10-16

好的。我正在通读C++傻瓜书,我正处于他们讨论面向对象的地步。在尝试从我学到的概念编写代码失败后,我几乎从书中复制了代码。代码的要点是创建一个名为 Pen 的类,其中包含描述对象 GoodPen 和 BadPen 的枚举。我创建了名为 Color 和 Pentype 的枚举变量,并为每个变量放置了单独的选项。这似乎是问题所在。我为对象分配了值,但由于某种原因,当我计算分配的值时,它们返回的是每个值的数值数组位置,而不是它们的实际值。这是代码

Pen.h 头文件:

#ifndef PEN_H_INCLUDED
#define PEN_H_INCLUDED
using namespace std;
enum Color
{
    red,
    blue,
    yellow,
    green,
    black,
    gray
};
enum PenType
{
    ballpoint,
    fountain,
    felttip,
    flammable
};
class Pen
{
public:
    Color InkColor;
    Color ShellColor;
    Color CapColor;
    PenType PenType;
    double length;
    double inklevel;
    string brand;
    void write(string words){
        if(inklevel <= 0){
            cout << "Uh-Oh, you're out of ink!" << endl;
        } else {
            cout << words << endl;
            inklevel -= words.length();
        }
    }
    void explode(){
        cout << "You used explosive ink, the tip became heated via friction with the paper" << endl << " and the pen exploded, killing you and your family..." << endl;
        inklevel = 0;
    }
};
#endif // PEN_H_INCLUDED

主.cpp文件:

#include<iostream>
#include<string>
#include "pen.h"
using namespace std;
extern void explode();
int main(){
    string inpt;
    Pen GoodPen;
    Pen BadPen;


    GoodPen.brand = "OfficeDepot";
    GoodPen.CapColor = black;
    GoodPen.InkColor = gray;
    GoodPen.ShellColor = gray;
    GoodPen.PenType = ballpoint;
    GoodPen.length = 6;             //inches
    GoodPen.inklevel = 100;         //percent
    BadPen.brand = "Staples";
    BadPen.CapColor = red;
    BadPen.InkColor = red;
    BadPen.ShellColor = red;
    BadPen.PenType = flammable;
    BadPen.length = 6.66;           //inches
    BadPen.inklevel = 100;          //percent
    cout << "You have a choice: black pen or red pen. Choose wisely. ";
    getline(cin, inpt);
    if(inpt == "black" || inpt == "Black"){
        cout << "You picked the right pen. The ink level is " << GoodPen.inklevel << "%" << endl;
        cout << "The pen is " << GoodPen.length << " inches long, it is a " << GoodPen.PenType << " from " << GoodPen.brand << endl;
        cout << "The cap is " << GoodPen.CapColor << " and the shell is " << GoodPen.ShellColor << endl;
        cout << "The ink is " << GoodPen.InkColor << endl;
    }else if(inpt == "red" || inpt == "Red"){
        //explode();
        cout << "You picked the wrong pen. The ink level is " << BadPen.inklevel << endl;
        cout << "The pen is " << BadPen.length << " inches long, it is a " << BadPen.PenType << "from " << BadPen.brand << endl;
        cout << "The cap is " << BadPen.CapColor << " and the shell is " << BadPen.ShellColor << endl;
        cout << "The ink is " << BadPen.InkColor << endl;
    }
    return 0;
}

对不起,我知道我的暗语很糟糕。我承认我是初学者,可能犯了一个初学者错误。Pen.h 代码创建 Pen 类并为其分配属性,main.cpp 文件为 Pen 类创建对象,并将属性分配给这些对象。但是,如果选择"黑色"选项,这是输出:

You have a choice: black pen or red pen. Choose wisely. black
You picked the right pen. The ink level is 100%
The pen is 6 inches long, it is a 0 from OfficeDepot
The cap is 4 and the shell is 5
The ink is 5
Press any key to continue . . .

非常感谢您抽出宝贵时间。对不起小说。:P

PS 我正在使用Visual Studio进行编译。

这里没有"类问题"。

在内部,C/C++ 中的enum只是整数。编译器将标签更改为其(从 0 开始)整数值。事实上,你可以做到

GoodPen.PenType = 2;

编译器可以接受。

因此,获取枚举的字符串表示形式需要生成它; v.g.如何在 c 中将枚举名称转换为字符串