fork(1) download
  1. // If you are not sure what some lines of code do, try looking back at
  2. // previous example programs, notes, or ask a question.
  3.  
  4. #include <iostream>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. class deck {};
  10. class card {
  11. public:
  12. card();
  13. card(const char* r); // Constant char pointer parameter means that the pointer
  14. // cannot be moved. Also, it allows you to pass string
  15. // literals ("literal")
  16. card(const card& src); // Copy constructor
  17.  
  18. char* getRank() const; // Constant because it will not change the rank member
  19. void setRank(const char* r); // Not constant because it will change the rank member
  20.  
  21. private:
  22. // Private data member
  23. char rank[10];
  24.  
  25. // The print function will be able to access the private rank member of a card
  26. friend void print(const card& c);
  27. // The deck class will be able to access private members of cards
  28. friend class deck;
  29. };
  30.  
  31. card::card() {
  32. // Clear rank
  33. strcpy(rank,"");
  34. }
  35.  
  36. card::card(const char* r) {
  37. // Copy rank
  38. strcpy(rank,r);
  39. }
  40.  
  41. // Passed by const reference in copy constructor to prevent infinite call
  42. // loop
  43. card::card(const card& src) {
  44. // Copy the rank member from the source card
  45. strcpy(rank,src.rank);
  46. }
  47.  
  48. char* card::getRank() const {
  49. // This function cannot change rank
  50. return (char*)rank;
  51. }
  52.  
  53. void card::setRank(const char* r) {
  54. // Copy rank
  55. strcpy(rank,r);
  56. }
  57.  
  58. // Note that if this was a member function (it is NOT), this could also be constant
  59. // because it does not change any data in the card. However, normal functions
  60. // (as in not member functions) cannot be constant.
  61. void print(const card& c) {
  62. // Output card. This function can access "c.rank" even though it's a private
  63. // data member because this is a friend function to the card class
  64. cout << "Card rank: " << c.rank << endl;
  65. }
  66.  
  67. int main() {
  68.  
  69. card c1;
  70. const card c2("king"); // You must give the rank here, because you cannot call
  71. // setRank on a const card
  72.  
  73. c1.setRank("queen");
  74. // c2.setRank("jack"); This would give an error
  75.  
  76. card c3(c1); // Create a copy of card one using the copy constructor
  77.  
  78. // Note that the print function is not called from an instance of the class,
  79. // as it is not a member function.
  80. print(c1);
  81. print(c2);
  82. print(c3);
  83.  
  84. // getRank is allowed on c2 because it is a constant member
  85. char* r1 = c1.getRank();
  86. char* r2 = c2.getRank();
  87. char* r3 = c3.getRank();
  88.  
  89. cout << "rank 1: " << r1 << endl
  90. << "rank 2: " << r2 << endl
  91. << "rank 3: " << r3 << endl;
  92.  
  93. system("pause");
  94. return 0;
  95. }
  96.  
Success #stdin #stdout #stderr 0s 3412KB
stdin
Standard input is empty
stdout
Card rank: queen
Card rank: king
Card rank: queen
rank 1: queen
rank 2: king
rank 3: queen
stderr
sh: 1: pause: not found