1

好的。我正在为傻瓜阅读 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

main.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 进行编译。

4

1 回答 1

0

这里没有“阶级问题”。

在内部,enum在 C/C++ 中只是整数。编译器将标签更改为它们的(基于 0 的)整数值。事实上,你可以做

GoodPen.PenType = 2;

编译器会同意的。

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

于 2013-10-17T00:46:39.340 回答