Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Student
  5. {
  6. private:
  7. const int nrStud;
  8. int nrNote = 0;
  9. int* note = NULL;
  10. public:
  11. static int nrPctCredit;
  12. Student(const int nrStud) :nrStud(nrStud)
  13. {
  14. }
  15. Student(const int nrStud, int* note, int nrNote) :nrStud(nrStud)
  16. {
  17. this->note = new int[nrNote];
  18. for (int i = 0; i < nrNote; i++)
  19. this->note[i] = note[i];
  20. this->nrNote = nrNote;
  21. }
  22. Student(const Student& s) :nrStud(s.nrStud)
  23. {
  24. this->nrNote = s.nrNote;
  25. this->note = new int[s.nrNote];
  26. for (int i = 0; i < s.nrNote; i++)
  27. this->note[i] = s.note[i];
  28. }
  29. void setNote(int* note, int nrNote)
  30. {
  31. this->nrNote = nrNote;
  32. this->note = new int[nrNote];
  33. for (int i = 0; i < nrNote; i++)
  34. this->note[i] = note[i];
  35. }
  36. int getNrNote()
  37. {
  38. return this->nrNote;
  39. }
  40. int getNrStud()
  41. {
  42. return this->nrStud;
  43. }
  44. int getNota(int index)
  45. {
  46. return note[index];
  47. }
  48. Student& operator=(const Student& s)
  49. {
  50. nrNote = s.nrNote;
  51. if (note != NULL)
  52. delete[] note;
  53. note = new int[s.nrNote];
  54. for (int i = 0; i < s.nrNote; i++)
  55. note[i] = s.note[i];
  56. return *this;
  57.  
  58. }
  59. bool estePromovat(int nrPctCredit)
  60. {
  61. if (nrPctCredit > 14)
  62. return true;
  63. else
  64. return false;
  65. }
  66. explicit operator float()
  67. {
  68. float medie = 0;
  69. for (int i = 0; i < nrNote; i++)
  70. medie = medie + note[i];
  71. if (nrNote > 0)
  72. return medie / nrNote;
  73. else
  74. return 0;
  75. }
  76. Student operator++(int)
  77. {
  78. int* copy = new int[nrNote];
  79. for (int i = 0; i < nrNote; i++)
  80. copy[i] = note[i];
  81. nrNote++;
  82. delete[] note;
  83. note = new int[nrNote];
  84. for (int i = 0; i < nrNote - 1; i++)
  85. note[i] = copy[i];
  86. note[nrNote - 1] = note[nrNote - 2];
  87. delete[] copy;
  88. return *this;
  89. }
  90. ~Student()
  91. {
  92. delete[] note;
  93. }
  94.  
  95. };
  96.  
  97. int Student::nrPctCredit = 3;
  98.  
  99. bool operator==(Student s1, Student s2)
  100. {
  101. int n1 = 0, n2 = 0;
  102. for (int i = 0; i < s1.getNrNote(); i++)
  103. if (s1.getNota(i) >= 5)
  104. n1++;
  105. for (int i = 0; i < s2.getNrNote(); i++)
  106. if (s2.getNota(i) >= 5)
  107. n2++;
  108. return n1 == n2;
  109. }
  110.  
  111. void operator<<(ostream& out, Student& s)
  112. {
  113. out << "Studentul cu codul " << s.getNrStud() << " are " << s.getNrNote() << " note: ";
  114. for (int i = 0; i < s.getNrNote(); i++)
  115. out << s.getNota(i) << " ";
  116. }
  117.  
  118. int main()
  119. {
  120. Student s1(102);
  121. cout << s1.getNrNote() << endl;
  122. int note[] = { 10, 7, 8, 10, 4 };
  123. Student s2(104, note, 5);
  124. cout << s2.getNrNote() << endl;
  125. Student s3 = s2;
  126. cout << s3.getNrStud() << endl;
  127. int note2[] = { 5, 5, 4, 10 };
  128. s1.setNote(note2, 4);
  129. cout << s1.getNrNote() << endl;
  130. Student::nrPctCredit = 4;
  131. s3 = s1;
  132. bool promovat = s3.estePromovat(15);
  133. if (promovat)
  134. cout << endl << "Studentul are un numar suficient de credite" << endl;
  135. else
  136. cout << endl << "Studentul nu are un numar suficient de credite" << endl;
  137. Student* ps1 = new Student(109);
  138. *ps1 = s1;
  139. cout << *ps1;
  140. delete ps1;
  141. float medie = (float)s1;
  142. cout << endl << medie << endl;
  143.  
  144. s1 = s3++;
  145. cout << endl;
  146. cout << s1;
  147. cout << endl;
  148. cout << s3;
  149. cout << endl;
  150. if (s1 == s3)
  151. cout << "Studentul 1 are acelasi numar de note de trecere ca studentul 3";
  152. else
  153. cout << "Studentul 1 nu are acelasi numar de note de trecere ca studentul 3";
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement