Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Student {
- char* Name; // в хийпа
- char EGN[11]; // в стека
- int facultyNumber;
- double* grades;
- int number_of_grades;
- int capacity_of_grades;
- public:
- Student() {
- Name = new char[1];
- Name[0] = '\0';
- EGN[0] = '\0';
- facultyNumber = 0;
- grades = new double[10];
- capacity_of_grades = 10;
- number_of_grades = 0;
- }
- Student(const char* Name_, const char* EGN_, const int facultyNumber_) {
- int nameLength = strlen(Name_);
- Name = new char[nameLength + 1];
- strcpy_s(Name, nameLength + 1, Name_);
- strcpy_s(EGN, EGN_);
- facultyNumber = facultyNumber_;
- grades = new double[10];
- capacity_of_grades = 10;
- number_of_grades = 0;
- }
- Student(const Student& other) {
- int nameLength = strlen(other.Name);
- Name = new char[nameLength + 1];
- strcpy_s(Name, nameLength + 1, other.Name);
- strcpy_s(EGN, other.EGN);
- facultyNumber = other.facultyNumber;
- }
- Student& operator=(const Student& other) {
- if (this != &other) {
- delete[] Name; // delete-ваме всичко, което е с динамична памет
- int nameLength = strlen(other.Name);
- Name = new char[nameLength + 1];
- strcpy_s(Name, nameLength + 1, other.Name);
- strcpy_s(EGN, other.EGN);
- facultyNumber = other.facultyNumber;
- }
- return *this;
- }
- ~Student() {
- delete[] Name;
- }
- const char* get_Name() const { // const стои и отпред, КОГАТО ИМАМЕ МАСИВ ОТ СИМВОЛИ !!!
- return Name;
- }
- const char* get_EGN() const {
- return EGN;
- }
- int get_facultyNumber() const { // const стои само отзад !!! ЩОМ ФУНКЦИЯТА НЕ ПРОМЕНЯ НИЩО, ЗАДЪЛЖИТЕЛНО СЕ ПИШЕ ОТЗАД !!!
- return facultyNumber;
- }
- const double* get_grades() const {
- return grades;
- }
- void set_Name(const char* Name_) { // const се пише в този случай !!!
- delete[] Name;
- int nameLength = strlen(Name_);
- Name = new char[nameLength + 1];
- strcpy_s(Name, nameLength + 1, Name_);
- }
- void set_EGN(const char* EGN_) {
- strcpy_s(EGN, EGN_);
- }
- void set_facultyNumber(int facultyNumber_) { // const НЕ се пише в този случай !!!
- facultyNumber = facultyNumber_;
- }
- void add_grade(double grade) {
- if (number_of_grades == capacity_of_grades) { // заделям памет
- capacity_of_grades += 10;
- double* new_grade = new double[capacity_of_grades];
- for (int i = 0; i < number_of_grades; i++) {
- new_grade[i] = grades[i];
- }
- new_grade[number_of_grades] = grade;
- number_of_grades++;
- delete[] grades;
- grades = new_grade;
- }
- else {
- grades[number_of_grades] = grade;
- number_of_grades++;
- }
- }
- void print() const {
- cout << "Ime: " << Name << endl;
- cout << "EGN: " << EGN << endl;
- cout << "Faculty Number: " << facultyNumber << endl;
- cout << "Ocenki: ";
- for (int i = 0; i < number_of_grades; i++) {
- cout << grades[i] << " ";
- }
- cout << endl;
- }
- void average() {
- double sum = 0;
- for (int i = 0; i < number_of_grades; i++) {
- sum += grades[i];
- }
- cout << "Sredno aritmetichnoto na tekushtite ocenki e: " << sum / number_of_grades << endl;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement