Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. class Student
  4. {
  5. private:
  6. char* nume;
  7. int varsta;
  8. float medie;
  9. public:
  10. //constructori
  11. Student()
  12. {
  13. this->nume = NULL;
  14. this->varsta = 0;
  15. this->medie = 0;
  16. cout << "Apel constructor fara parametrii" << endl;;
  17. }
  18. Student(char* nume, int varsta = 19, float medie = 0)
  19. {
  20. this->nume = new char[strlen(nume) + 1];
  21. strcpy(this->nume, nume);
  22. this->varsta = varsta;
  23. this->medie = medie;
  24. cout << "Apel constructor cu parametrii" << endl;
  25. }
  26.  
  27. //accesori pentru atribute constante
  28. int getVarsta()
  29. {
  30. return this->varsta;
  31. }
  32.  
  33. void setVarsta(int varsta)
  34. {
  35. if(varsta >= 18)
  36. this->varsta = varsta;
  37. }
  38.  
  39. void afisare()
  40. {
  41. if(this->nume !=NULL)
  42. cout << "Nume student: " << this->nume << endl;
  43. cout << "Varsta: " << this->varsta << endl;
  44. cout << "Medie: " << this->medie << endl;
  45.  
  46. }
  47.  
  48. void afisare_this()
  49. {
  50. cout << this << endl;
  51. }
  52.  
  53. int comparare(Student s)
  54. {
  55. if (this->medie > s.medie)
  56. return 1;
  57. else if (this->medie == s.medie)
  58. return 0;
  59. return -1;
  60. }
  61. };
  62.  
  63. //transfer prin referinta
  64. int comparareVarsta(Student& s1, Student &s2)
  65. {
  66. if (s1.getVarsta() > s2.getVarsta())
  67. return 1;
  68. else if (s1.getVarsta() == s2.getVarsta())
  69. return 0;
  70. return -1;
  71. }
  72.  
  73. int main()
  74. {
  75. Student s;
  76. s.afisare();
  77. Student vs[5];
  78. Student *ps = new Student();
  79.  
  80. char nume[] = "Gigel";
  81. Student s2(nume, 19, 10);
  82. s2.setVarsta(23);
  83. s2.afisare_this();
  84. s2.afisare();
  85. Student s3(nume, 21);
  86. cout << s2.comparare(s3) << endl;
  87. cout << "Rezultat comparare varsta: " << comparareVarsta(s2, s3) << endl;
  88. Student s4(nume);
  89. s4.afisare();
  90. cout << s4.getVarsta();
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement