"模具"类 [英] "Die" class

查看:74
本文介绍了"模具"类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c ++课程的新手。我定义了这个cDie。将返回的类

a介于1和6之间(包括)它运行正常并且在编译期间没有警告




我想知道你们是否可以接受任何错误/不做这个代码:


#include< iostream> ;

#include< cstdlib>


使用命名空间std;


class cDie

{

public:

cDie();

int roll();

};

cDie :: cDie()

{

srand(time(NULL));

}

int cDie :: roll()

{

返回rand()%6 + 1;

}


int main()

{

cDie die;


for(int i = 0; i< 10; i ++)

{

cout<< Die Dieled: << die.roll()<<结束;

}

返回0;

}

解决方案

ma******@gmail.com 写道:

我是c ++类的新手。我定义了这个cDie。将返回的值为1到6之间的值(包括)它在编译期间运行正常并且没有警告

我想知道你们是否可以发现任何错误/不要做来自
此代码:

#include< iostream>
#include< cstdlib>

using namespace std;

class cDie
{
public:
cDie();
int roll();
};
cDie :: cDie()
{/ / srand(time(NULL));
}
int cDie :: roll()
{
return rand()%6 + 1;
}
int main()
{
cDie die;

for(int i = 0; i< 10; i ++)
{
cout<< Die Dieled: << die.roll()<<返回0;
}




立即不要做是全局的using namespace std;。将std

引入全局命名空间会完全失去命名空间的点。

显式引用std中的项目(例如std :: cout<<" Die Rolled :

" ..."),在本地引入命名空间,或者只从命名空间中引入你将要使用的项目。


考虑从main而不是0返回EXIT_SUCCESS。


我会在main中修改,而不是cDie的构造函数。如果你选择从bDie的构造函数中删除它,你就不需要编写你的

自己的构造函数了(虽然我知道你可能在做这个

作为一项任务,指示你写一个构造函数来表明

你/可以/)。


当你''只使用i作为循环计数器,考虑将其更改为++ i,

这可能会更快,因为没有临时生成。


而不是使用6作为roll方法中的模数,定义一个常量

,例如MAX_ROLLS。它可能是cDie的私人成员。拥有魔法

数字在代码中降低了可读性,如果你想要一个87面的骰子,就更难改变

代码。


这里''我的清单:


*避免使用命名空间标准像瘟疫一样。要么明确

限定该命名空间的成员,要么为他们使用单独的使用声明

。在一个功能范围内使用它是好的,如果你喜欢

,那么 - 只是没有更广泛的东西。

*不要使用"魔术数字在你的代码中 - 创建符号常量

(使用const,当然不是#define)。

*格式化有点奇怪,但那是'主观的,当你学习

时,你会接触到大量的规范来选择或杂交。

你真的使用了八个空格标签,还是只是我的新闻阅读器对我这么做?
?哦,我建议取消缩进public:所以它更多地支持

;这是典型的事情。

*尽可能使用++ i而不是i ++。后递增的效率较低

有效(它需要一个额外的临时变量,想想

)并且排序含糊不清。

*匈牙利表示法是该死的魔鬼。说真的,不,不。它b / b
扫描得不好,这是一个可维护性的噩梦。变量名

应描述变量的作用,而不是实现



*尽可能经常使用const作为方法和变量(并且没有

更多 - Sutter(?))。在这种情况下,roll()方法应该是const,

,因为它不会修改封闭对象的状态。

*因为roll()不依赖于类状态,它应该是一个静态的

方法。

*通常情况下,最好不要将自己绑定到特定的原语

无理由的数据类型,如

roll()的返回值。也许以后你会发现,出于效率原因,你宁愿使用较短的

表示(例如unsigned char,单个
字节)。最好的办法是创建一个嵌套的public typedef并使用

作为返回值。例如:

class Die {

public:

typedef int result_type;

result_type roll()const;

};


干杯,

卢克


< blockquote>W Marsh写道:

而不是使用6作为roll方法中的模数,而是定义一个常量
,例如MAX_ROLLS。它可能是cDie的私人成员。拥有魔法数字和魔法数字在代码中降低了可读性,如果你想要一个87面的骰子,就更难改变代码。




我发现魔术数字几乎总是如此比一些丑陋的更具可读性,

长啰嗦的名字。人们知道一周有7天,有一年365天,一年有52张牌,有一张牌有6张牌。只有一种方法可以写出$ 7 $ b写7,有很多方法可以写DaysInTheWeek,其中很多都很烦人

我。我知道这是一场宗教战争,所以不要再为我的利益发布反驳。

给OP。对我来说看起来很不错,但我也倾向于把srand放在

main。我不能真正指出为什么我更喜欢它。

我认为原件有缩进,在我看到之前它被剥离了。


I am new to c++ classes. I defined this "cDie" class that would return
a value between 1 and 6 (inclusive) It runs fine and gives no warnings
during compilation.

I was wondering if you guys can pick up any mistakes/"don''t do"s from
this code:

#include <iostream>
#include <cstdlib>

using namespace std;

class cDie
{
public:
cDie();
int roll();
};
cDie :: cDie()
{
srand(time(NULL));
}
int cDie :: roll()
{
return rand()%6 + 1;
}

int main()
{
cDie die;

for (int i = 0; i < 10; i++)
{
cout << "Die Rolled: " << die.roll() << endl;
}
return 0;
}

解决方案

ma******@gmail.com wrote:

I am new to c++ classes. I defined this "cDie" class that would return
a value between 1 and 6 (inclusive) It runs fine and gives no warnings
during compilation.

I was wondering if you guys can pick up any mistakes/"don''t do"s from
this code:

#include <iostream>
#include <cstdlib>

using namespace std;

class cDie
{
public:
cDie();
int roll();
};
cDie :: cDie()
{
srand(time(NULL));
}
int cDie :: roll()
{
return rand()%6 + 1;
}

int main()
{
cDie die;

for (int i = 0; i < 10; i++)
{
cout << "Die Rolled: " << die.roll() << endl;
}
return 0;
}



Immediate "don''t do" is the global "using namespace std;". Bringing std
into the global namespace defeats the point of namespacing entirely.
Reference items in std explicitly (e.g. "std::cout << "Die Rolled:
"..."), bring in the namespace locally, or only bring in the items you
are going to use from the namespace.

Consider returning EXIT_SUCCESS from main instead of 0.

I would srand in main, not the constructor of cDie. If you choose to
remove it from the constructor of cDie, you wouldn''t need to write your
own constructor at all (although I understand that you may be doing this
as an assignment which instructs you to write a constructor to show that
you /can/).

As you''re only using i as a loop counter, consider changing it to ++i,
which may be faster as no temporary is generated.

Instead of using 6 as the modulo in the roll method, define a constant
such as "MAX_ROLLS". It could be a private member of cDie. Having "magic
numbers" in code decreases readability, and makes it harder to alter the
code should you want an 87-sided die.


Hi, here''s my list:

* Avoid "using namespace std" like the plague. Either explicitly
qualify members of that namespace, or use individual using-declarations
for them. It''s alright to use it within a single function scope, if
you like, though -- just not anything broader.
* Don''t use "magic numbers" in your code -- create symbolic constants
(with const, not #define, of course).
* Formatting''s a bit odd, but that''s subjective, and as you learn
you''ll be exposed to plenty of norms to choose among or hybridize. Are
you really using eight space tabs, or is that just my newsreader doing
that to me? Oh, and I recommend un-indenting "public:" so it stands
out more; that''s typical to do.
* Use ++i rather than i++ whenever you can. Post-incrementing is less
efficient (it requires an additional temporary variable, think about
it) and has ordering ambiguity.
* Hungarian notation is the goddamn devil. Seriously, just don''t. It
doesn''t scan well and it''s a maintainability nightmare. Variable names
should describe the role of the variable, not the implementation
thereof.
* Use const for methods and variables as often as possible ("and no
more" -- Sutter (?)). In this case, the roll() method should be const,
since it does not modify the state of the enclosing object.
* Since roll() does not depend on class state, it should be a static
method.
* Often, it''s a good idea not to tie yourself to a particular primitive
data type for no reason, as in the case of the return value from
roll(). Maybe later you''d discover that you''d rather use a shorter
representation for efficiency reasons (e.g. unsigned char, a single
byte). Best thing to do is to create a nested public typedef and use
that for your return value. For example:
class Die {
public:
typedef int result_type;
result_type roll() const;
};

Cheers,
Luke


"W Marsh" writes:

Instead of using 6 as the modulo in the roll method, define a constant
such as "MAX_ROLLS". It could be a private member of cDie. Having "magic
numbers" in code decreases readability, and makes it harder to alter the
code should you want an 87-sided die.



I find that magic numbers are almost always more readable than some ugly,
long winded name. People know there are 7 days in a week, 365 days in a
year, 52 cards in a deck and 6 sides on a die. There is only one way to
write 7, there are tons of ways to write DaysInTheWeek, a lot of them annoy
me. I know this is a religious war so don''t bother posting a rebuttal for
my benefit.
To the OP. Looks pretty good to me, but I would be inclined to put srand in
main too. I can''t really put my finger on why I prefer it that way, though.
I assume the original had indentation, it got stripped before I saw it.


这篇关于&QUOT;模具&QUOT;类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆