Advertisement
35657

Untitled

Mar 9th, 2024
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. struct date {
  7.     int day_;
  8.     int month_;
  9.     int year_;
  10. };
  11.  
  12. class person {
  13.  
  14. public:
  15.     person(const int id, const char* last_name, const char* first_name, const char* middle_name, const date birthday) : id_(id), last_name_(new char[strlen(last_name) + 1]), first_name_(new char[strlen(first_name) + 1]), middle_name_(new char[strlen(middle_name) + 1]), birthday_(birthday) {
  16.         strcpy(last_name_, last_name);
  17.         strcpy(first_name_, first_name);
  18.         strcpy(middle_name_, middle_name);
  19.         number_person_++;
  20.     }
  21.  
  22.     person() : person(111111, "Ivanov", "Ivan", "Ivanovich", { 1,1,1993 }) {}
  23.  
  24.     person(const person& other) : person(other.id_, other.last_name_, other.first_name_, other.middle_name_, other.birthday_) {}
  25.  
  26.     person& operator=(const person& other) {
  27.         if (this != &other) {
  28.             id_ = other.id_;
  29.             birthday_ = other.birthday_;
  30.             delete[] last_name_;
  31.             last_name_ = new char[strlen(other.last_name_) + 1];
  32.             strcpy(last_name_, other.last_name_);
  33.             delete[] first_name_;
  34.             first_name_ = new char[strlen(other.first_name_) + 1];
  35.             strcpy(first_name_, other.first_name_);
  36.             delete[] middle_name_;
  37.             middle_name_ = new char[strlen(other.middle_name_) + 1];
  38.             strcpy(middle_name_, other.middle_name_);
  39.         }
  40.         return *this;
  41.     }
  42.  
  43.     void print() {
  44.         cout << last_name_ << " " << first_name_ << " " << middle_name_ << ", " << birthday_.day_ << '.' << birthday_.month_ << '.' << birthday_.year_ << ", id - " << id_ << endl;
  45.     }
  46.  
  47.     static int get_number_person() {
  48.         return number_person_;
  49.     }
  50.  
  51.     ~person() {
  52.         delete[] last_name_;
  53.         delete[] first_name_;
  54.         delete[] middle_name_;
  55.         number_person_--;
  56.     }
  57.  
  58.     void set_id(const int id) {
  59.         id_ = id;
  60.     }
  61.  
  62.  
  63.     void set_last_name(const char* last_name) {
  64.         delete[] last_name_;
  65.         last_name_ = new char[strlen(last_name) + 1];
  66.         strcpy(last_name_, last_name);
  67.     }
  68.  
  69.     void set_first_name(const char* first_name) {
  70.         delete[] first_name_;
  71.         first_name_ = new char[strlen(first_name) + 1];
  72.         strcpy(first_name_, first_name);
  73.     }
  74.  
  75.     void set_middle_name(const char* middle_name) {
  76.         delete[] middle_name_;
  77.         middle_name_ = new char[strlen(middle_name) + 1];
  78.         strcpy(middle_name_, middle_name);
  79.     }
  80.  
  81.     void set_birthday(const int day, const int month, const int year) {
  82.         birthday_.day_ = day;
  83.         birthday_.month_ = month;
  84.         birthday_.year_ = year;
  85.     }
  86.  
  87. private:
  88.     int id_; // идентификационный номер
  89.     char* last_name_; // фамилия
  90.     char* first_name_; // имя
  91.     char* middle_name_; // отчество
  92.     date birthday_; // дата рождения
  93.     static int number_person_;
  94. };
  95.  
  96. class flat {
  97.  
  98.     flat() : residents_count_(0), max_count_(5), residents_(new person[max_count_]) {}
  99.  
  100.     flat(const flat& other) : residents_count_(other.residents_count_), max_count_(other.max_count_), residents_(new person[max_count_]) {
  101.         for (int i = 0; i < residents_count_; i++) {
  102.             residents_[i] = other.residents_[i];
  103.         }
  104.     }
  105.  
  106.     void add_resident(const person& new_resident) {
  107.         if (residents_count_ == max_count_) {
  108.             person* temp = new person[max_count_ * 2];
  109.             for (int i = 0; i < residents_count_; i++) {
  110.                 temp[i] = residents_[i];
  111.             }
  112.             delete[] residents_;
  113.             residents_ = temp;
  114.             max_count_ *= 2;
  115.         }
  116.         residents_[residents_count_] = new_resident;
  117.         residents_count_++;
  118.     }
  119.  
  120.     // реализовать методы для удаления жильца и вывода информации по жильцу
  121.  
  122. private:
  123.     int residents_count_;
  124.     int max_count_;
  125.     person* residents_;
  126. };
  127.  
  128. int person::number_person_ = 0;
  129.  
  130. int main() {
  131.     person p1(222222, "Petrov", "Petr", "Petrovich", { 23,8,1997 });
  132.  
  133.     person p2;
  134.  
  135.     person p3(p1);
  136.  
  137.     p1.print();
  138.     p2.print();
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement