fork 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.  
  10. //CARD CLASS
  11. class card {
  12. public:
  13. //Default constructor
  14. card();
  15. //Parameterized constructor
  16. card(const char*, const char*, int);
  17. //Destructor
  18. ~card();
  19.  
  20. //Getters and setters. I realize that right now, some of these
  21. //are pretty much useless. However, you will often want
  22. //to use these for more complicated operations, for example
  23. //how the setRank and setSuit functions reallocate memory
  24. char* getRank();
  25. void setRank(const char*);
  26. char* getSuit();
  27. void setSuit(const char*);
  28. int getValue();
  29. void setValue(int);
  30.  
  31. //A member that will print the card's data to the console
  32. void print();
  33.  
  34. private:
  35. char* rank;
  36. char* suit;
  37. int value;
  38. };
  39.  
  40. //Default constructor
  41. card::card() {
  42. cout << endl << "In default constructor!" << endl;
  43.  
  44. //Set data members to default values
  45. rank = NULL;
  46. suit = NULL;
  47. value = 0;
  48. }
  49.  
  50. //Parameterized constructor
  51. card::card(const char* r, const char* s, int v) {
  52. cout << endl << "In Parameterized constructor!" << endl;
  53.  
  54. //Set data members to the parameters
  55. rank = new char[strlen(r) + 1];
  56. suit = new char[strlen(s) + 1];
  57. strcpy(rank,r);
  58. strcpy(suit,s);
  59. value = v;
  60. }
  61.  
  62. //Destructor
  63. card::~card() {
  64. cout << endl << "In destructor!" << endl;
  65.  
  66. //Delete the data members, if we allocated them
  67. //(delete will not delete if it is given a null pointer)
  68. delete[] rank;
  69. delete[] suit;
  70. }
  71.  
  72. //Getters and setters
  73. char* card::getRank() {
  74. return rank;
  75. }
  76.  
  77. //As you can see, this setter is useful, because it automatically manages
  78. //the dynamic memory
  79. void card::setRank(const char* r) {
  80. delete[] rank;
  81. rank = new char[strlen(r) + 1];
  82. strcpy(rank,r);
  83. }
  84.  
  85. char* card::getSuit() {
  86. return suit;
  87. }
  88.  
  89. void card::setSuit(const char* s) {
  90. delete[] suit;
  91. suit = new char[strlen(s) + 1];
  92. strcpy(suit,s);
  93. }
  94.  
  95. int card::getValue() {
  96. return value;
  97. }
  98.  
  99. void card::setValue(int v) {
  100. value = v;
  101. }
  102.  
  103. //This is a normal member function, and will output the card's values
  104. void card::print() {
  105. //If suit points to null, say we don't have a suit
  106. if(!rank)
  107. cout << "no rank ";
  108. else
  109. cout << rank << " ";
  110. if(!suit)
  111. cout << "no suit ";
  112. else
  113. cout << suit << " ";
  114. cout << value << endl;
  115. }
  116. //END CARD CLASS
  117.  
  118.  
  119. int main() {
  120.  
  121. cout << endl << "Creating static card" << endl;
  122.  
  123. //Create a card and a card pointer
  124. card card1;
  125. card* card2 = NULL;
  126.  
  127. //Print inital values
  128. cout << endl << "Initial card:" << endl;
  129. card1.print();
  130.  
  131. //Use setters
  132. card1.setRank("king");
  133. card1.setSuit("hearts");
  134. card1.setValue(13);
  135.  
  136. //Print set valeus
  137. cout << endl << "After setters called:" << endl;
  138. card1.print();
  139.  
  140. //Use getters
  141. char* rank = card1.getRank();
  142. char* suit = card1.getSuit();
  143. int value = card1.getValue();
  144.  
  145. //Print gotten values
  146. cout << endl << "Values from card:" << endl
  147. << rank << " " << suit << " " << value << endl;
  148.  
  149. //Create dynamic card using the parameterized constructor
  150. cout << endl << "Creating dynamic card" << endl;
  151. card2 = new card("two","spades",2);
  152.  
  153. //Print the card (note the use of the arrow operator)
  154. cout << endl << "Dynamic card:" << endl;
  155. card2->print();
  156.  
  157. //Delete dynamic card. Note the destructor will be called here
  158. cout << endl << "Deleting dynamic card" << endl;
  159. delete card2;
  160.  
  161.  
  162. cout << endl << "End of program --- ";
  163. system("pause");
  164. //Note that the destructor for the statically declared card will
  165. //be called as main exits.
  166. return 0;
  167. }
  168.  
Success #stdin #stdout #stderr 0s 3464KB
stdin
Standard input is empty
stdout
Creating static card

In default constructor!

Initial card:
no rank no suit 0

After setters called:
king hearts 13

Values from card:
king hearts 13

Creating dynamic card

In Parameterized constructor!

Dynamic card:
two spades 2

Deleting dynamic card

In destructor!

End of program --- 
In destructor!
stderr
sh: 1: pause: not found