fork(5) download
  1. class Student
  2. {
  3. public:
  4. // If you can, don't use numbers:
  5. // you have a 3 on the variable,
  6. // a 3 on the function, etc.
  7. // Use a #define on C or a static const on C++
  8. static const int SIZE= 3;
  9. // You can also use it outside the class as Student::SIZE
  10.  
  11. public:
  12. void SetDaysToCompleteCourse(int* newDaysToCompleteCourse);
  13. // Safer version:
  14. void SetDaysToCompleteCourseSafe(int (&newDaysToCompleteCourse)[SIZE])
  15. {
  16. SetDaysToCompleteCourse(newDaysToCompleteCourse);
  17. }
  18.  
  19. // The consts are for "correctness"
  20. const int* GetDaysToCompleteCourse() const; // Ditto @ above comment.
  21.  
  22. Student()
  23. {
  24. // Always initialize variables
  25. for (int i= 0; i < SIZE; i++) {
  26. daysToCompleteCourse[i]= 0;
  27. }
  28. }
  29.  
  30. private:
  31. int daysToCompleteCourse[SIZE];
  32. // On GCC, you can do
  33. //int daysToCompleteCourse[SIZE]{};
  34. // Which will allow you not to specify it on the constructor
  35. };
  36.  
  37. void Student::SetDaysToCompleteCourse(int* newDaysToCompleteCourse)
  38. {
  39. // It's not wrong, just that
  40. // this->daysToCompleteCourse[0] = daysToCompleteCourse[0];
  41. // use another name like newDaysToCompleteCourse and then you can suppress this->
  42. // And use a for loop
  43. for (int i= 0; i < SIZE; i++) {
  44. daysToCompleteCourse[i]= newDaysToCompleteCourse[i];
  45. }
  46. }
  47.  
  48. const int* Student::GetDaysToCompleteCourse() const
  49. {
  50. return daysToCompleteCourse;
  51. }
  52.  
  53. #include <iostream>
  54.  
  55. std::ostream& operator<<(std::ostream& stream, const Student& student)
  56. {
  57. const int* toShow= student.GetDaysToCompleteCourse();
  58. for (int i= 0; i < Student::SIZE; i++) {
  59. stream << toShow[i] << ' ';
  60. }
  61. return stream;
  62. }
  63.  
  64. int main()
  65. {
  66. Student student;
  67. int daysToCompleteCourse[3] = { 1, 2, 3 };
  68. int daysToCompleteCourseBad[2] = { 1, 2 };
  69. // You don't need this
  70. //int* ptr = daysToCompleteCourse;
  71. //student.SetDaysToCompleteCourse(ptr);
  72. //You can just do:
  73. student.SetDaysToCompleteCourse(daysToCompleteCourse);
  74. student.SetDaysToCompleteCourseSafe(daysToCompleteCourse);
  75.  
  76. // This would work but what's the number stored on the third element?
  77. // student.SetDaysToCompleteCourse(daysToCompleteCourseBad);
  78. // Safe version takes a reference to a 3 element array (no more, no less)
  79. // This will not work:
  80. // student.SetDaysToCompleteCourseSafe(daysToCompleteCourseBad);
  81.  
  82. // On C++ int* is "a pointer to an int"
  83. // It doesn't specify how many of them
  84. // Arrays are represented just by the pointer to the first element
  85. // It's the FASTEST and CHEAPEST way... but you need the SIZE
  86. const int* toShow= student.GetDaysToCompleteCourse();
  87. for (int i= 0; i < Student::SIZE; i++) {
  88. std::cout << toShow[i] << ' ';
  89. }
  90. std::cout << std::endl;
  91.  
  92. // Or you can do:
  93. std::cout << student << std::endl;
  94. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
1 2 3 
1 2 3