面向对象C++帮助?出于某种原因,对象变量的输出返回数值数组位置而不是实际值
Object Oriented C++ Help? For some reason, output of object variables is returning numerical array locations instead of actual values.
好的。我正在通读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 中将枚举名称转换为字符串
相关文章:
- 为什么 C++ 中的字符数组返回其项目而不是地址?
- C++ bool 数组返回 1 或 0 以外的值
- C++ ostream 从对象数组返回数据
- 类中的重载运算符 [],因此它可以从模板类中的数组返回对象
- 指向对象的指针数组返回jibberish
- 具有基本结构的结构数组返回错误的答案
- 数组返回乐趣 movie_check()
- C++从字符数组返回类型为T的指针
- C++-将数组返回到主函数,并将数组传递给另一个函数
- C++:如何通过异常处理从越界数组返回值
- 整数数组返回类型
- 从字符串流复制到 char * 数组返回为函数参数
- C++函数从对象数组返回对象的副本.这是默认行为吗?
- 8字节数组返回长(C#到C++)
- 向c#数组返回一个字符数组c++
- 从并行数组返回值,并在以后的函数中使用这些值
- 将c++中的浮点数数组返回给Python
- 如何在gsoap中使字符串数组返回值
- 将两个字符相乘并向数组返回int
- C++套接字 - 从结构数组返回单个地址信息