Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. class Student {
  2.  
  3.     char* Name; // в хийпа
  4.     char EGN[11]; // в стека
  5.     int facultyNumber;
  6.     double* grades;
  7.     int number_of_grades;
  8.     int capacity_of_grades;
  9.  
  10. public:
  11.  
  12.     Student() {
  13.  
  14.         Name = new char[1];
  15.         Name[0] = '\0';
  16.         EGN[0] = '\0';
  17.         facultyNumber = 0;
  18.         grades = new double[10];
  19.         capacity_of_grades = 10;
  20.         number_of_grades = 0;
  21.  
  22.     }
  23.  
  24.     Student(const char* Name_, const char* EGN_, const int facultyNumber_) {
  25.  
  26.         int nameLength = strlen(Name_);
  27.         Name = new char[nameLength + 1];
  28.  
  29.         strcpy_s(Name, nameLength + 1, Name_);
  30.         strcpy_s(EGN, EGN_);
  31.         facultyNumber = facultyNumber_;
  32.         grades = new double[10];
  33.         capacity_of_grades = 10;
  34.         number_of_grades = 0;
  35.  
  36.     }
  37.  
  38.     Student(const Student& other) {
  39.  
  40.         int nameLength = strlen(other.Name);
  41.         Name = new char[nameLength + 1];
  42.  
  43.         strcpy_s(Name, nameLength + 1, other.Name);
  44.         strcpy_s(EGN, other.EGN);
  45.         facultyNumber = other.facultyNumber;
  46.  
  47.     }
  48.  
  49.     Student& operator=(const Student& other) {
  50.  
  51.         if (this != &other) {
  52.  
  53.             delete[] Name; // delete-ваме всичко, което е с динамична памет
  54.  
  55.             int nameLength = strlen(other.Name);
  56.             Name = new char[nameLength + 1];
  57.  
  58.             strcpy_s(Name, nameLength + 1, other.Name);
  59.             strcpy_s(EGN, other.EGN);
  60.             facultyNumber = other.facultyNumber;
  61.  
  62.         }
  63.  
  64.         return *this;
  65.  
  66.     }
  67.  
  68.     ~Student() {
  69.  
  70.         delete[] Name;
  71.  
  72.     }
  73.  
  74.     const char* get_Name() const { // const стои и отпред, КОГАТО ИМАМЕ МАСИВ ОТ СИМВОЛИ !!!
  75.  
  76.         return Name;
  77.  
  78.     }
  79.  
  80.     const char* get_EGN() const {
  81.  
  82.         return EGN;
  83.  
  84.     }
  85.  
  86.     int get_facultyNumber() const { // const стои само отзад !!! ЩОМ ФУНКЦИЯТА НЕ ПРОМЕНЯ НИЩО, ЗАДЪЛЖИТЕЛНО СЕ ПИШЕ ОТЗАД !!!
  87.  
  88.         return facultyNumber;
  89.  
  90.     }
  91.  
  92.     const double* get_grades() const {
  93.        
  94.         return grades;
  95.  
  96.     }
  97.  
  98.     void set_Name(const char* Name_) { // const се пише в този случай !!!
  99.  
  100.         delete[] Name;
  101.  
  102.         int nameLength = strlen(Name_);
  103.  
  104.         Name = new char[nameLength + 1];
  105.         strcpy_s(Name, nameLength + 1, Name_);
  106.  
  107.     }
  108.  
  109.     void set_EGN(const char* EGN_) {
  110.  
  111.         strcpy_s(EGN, EGN_);
  112.  
  113.     }
  114.  
  115.     void set_facultyNumber(int facultyNumber_) { // const НЕ се пише в този случай !!!
  116.  
  117.         facultyNumber = facultyNumber_;
  118.  
  119.     }
  120.  
  121.     void add_grade(double grade) {
  122.  
  123.         if (number_of_grades == capacity_of_grades) { // заделям памет
  124.  
  125.             capacity_of_grades += 10;
  126.             double* new_grade = new double[capacity_of_grades];
  127.  
  128.             for (int i = 0; i < number_of_grades; i++) {
  129.  
  130.                 new_grade[i] = grades[i];
  131.  
  132.             }
  133.  
  134.             new_grade[number_of_grades] = grade;
  135.  
  136.             number_of_grades++;
  137.  
  138.             delete[] grades;
  139.  
  140.             grades = new_grade;
  141.  
  142.         }
  143.  
  144.         else {
  145.  
  146.             grades[number_of_grades] = grade;
  147.             number_of_grades++;
  148.  
  149.         }
  150.     }
  151.  
  152.  
  153.     void print() const {
  154.  
  155.         cout << "Ime: " << Name << endl;
  156.         cout << "EGN: " << EGN << endl;
  157.         cout << "Faculty Number: " << facultyNumber << endl;
  158.         cout << "Ocenki: ";
  159.         for (int i = 0; i < number_of_grades; i++) {
  160.  
  161.             cout << grades[i] << " ";
  162.  
  163.         }
  164.         cout << endl;
  165.  
  166.     }
  167.  
  168.     void average() {
  169.  
  170.         double sum = 0;
  171.         for (int i = 0; i < number_of_grades; i++) {
  172.  
  173.             sum += grades[i];
  174.  
  175.         }
  176.  
  177.         cout << "Sredno aritmetichnoto na tekushtite ocenki e: " << sum / number_of_grades << endl;
  178.  
  179.     }
  180.  
  181. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement