加载中...

12.3 printCard函数


创建新类型时,第一步一般是声明实例变量并编写构造函数,第二步一般是编写一个可以将对象以可读形式打印出来的函数。

对于纸牌的情况,“可读”指的是我们必须将大小和花色的内部表示映射为单词。一种自然的方法是使用apstring的向量完成该功能。你可以像创建其他类型的向量一样创建apstring的向量:

  1. apvector<apstring> suits (4);

当然,为了使用apvector和apstring类型,必须包含它们的头文件【注】。

为了初始化向量的元素,我们可以使用一系列赋值语句:

  1. suits[0] = "Clubs";
  2. suits[1] = "Diamonds";
  3. suits[2] = "Hearts";
  4. suits[3] = "Spades";

这个向量的状态图如下所示: enter image description here

我们可以构建一个类似的向量来解码牌的大小。然后,我们就能以花色和大小为索引选择适当的元素了。最后,我们能够编写print函数来输出调用该函数的纸牌的信息:

  1. void Card::print () const
  2. {
  3. apvector<apstring> suits (4);
  4. suits[0] = "Clubs";
  5. suits[1] = "Diamonds";
  6. suits[2] = "Hearts";
  7. suits[3] = "Spades";
  8. apvector<apstring> ranks (14);
  9. ranks[1] = "Ace";
  10. ranks[2] = "2";
  11. ranks[3] = "3";
  12. ranks[4] = "4";
  13. ranks[5] = "5";
  14. ranks[6] = "6";
  15. ranks[7] = "7";
  16. ranks[8] = "8";
  17. ranks[9] = "9";
  18. ranks[10] = "10";
  19. ranks[11] = "Jack";
  20. ranks[12] = "Queen";
  21. ranks[13] = "King";
  22. cout << ranks[rank] << " of " << suits[suit] << endl;
  23. }

表达式suits[suit]的意义是“以当前对象的实例变量suit为索引从向量suits选择适当的字符串”。

因为print是Card类的成员函数,所以它能隐式地(即不适用点记法指定对象)引用当前对象的实例变量。比如下面代码:

  1. Card card (1, 11);
  2. card.print ();

其输出是“Jack of Diamonds”。

你可能注意到了,我们没有使用表示牌大小的向量的第0个元素。那是因为只有1-13之间的牌大小值才是有效的。通过在向量的开头留下一个未用元素,我们得到了从2映射到“2”,3映射到“3”等这样的编码。从用户的观点看,编码是什么并不重要,因为所有的输入和输出都是用可读的形式表示的。

另一方面,如果映射易于记忆,这对程序员来说是有帮助的。

注:apvectors are a little different from apstrings in this regard. The file apvector.cpp contains a template that allows the compiler to create vectors of various kinds. The first time you use a vector of integers, the compiler generates code to support that kind of vector. If you use a vector of apstrings, the compiler generates different code to handle that kind of vector. As a result, it is usually sufficient to include the header file apvector.h; you do not have to compile apvector.cpp at all! Unfortunately, if you do, you are likely to get a long stream of error messages. I hope this footnote helps you avoid an unpleasant surprise, but the details in your development environment may differ.!


还没有评论.