Advertisement
Guest User

Сиспрогр ПМ-16 Николаев Влад - практика 7

a guest
Dec 14th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.86 KB | None | 0 0
  1. // Zadanie 1
  2. #include <iostream>
  3. #include <clocale>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class Human {
  9. public:
  10.     string name;
  11.     int age;
  12.     int id;
  13.     char gender;
  14.     Human(Human* h) {
  15.         name = h->name;
  16.         age = h->age;
  17.         id = h->id;
  18.         gender = h->gender;
  19.     }
  20.     Human(string name, int age, int id, char gender) {
  21.         this->name = name;
  22.         this->age = age;
  23.         this->id = id;
  24.         this->gender = gender;
  25.     }
  26.     virtual void print() {
  27.         cout << "Имя: " << name << ", " << age << " лет" << endl;
  28.         cout << "Пол: " << gender << endl;
  29.         cout << "Номер паспорта: " << id << endl;
  30.     }
  31.     virtual ~Human() {
  32.         cout << "Объект класса Human разрушен" << endl;
  33.     }
  34. };
  35.  
  36. class Student : public Human {
  37. public:
  38.     int course;
  39.     string university;
  40.     string faculty;
  41.     Student(Student* h): Human(h->name, h->age, h->id, h->gender) {
  42.         course = h->course;
  43.         university = h->university;
  44.         faculty = h->faculty;
  45.     }
  46.     Student(string name, int age, int id, char gender, int course, string university, string faculty) :
  47.         Human(name, age, id, gender) {
  48.         this->course = course;
  49.         this->university = university;
  50.         this->faculty = faculty;
  51.     }
  52.     virtual void print() {
  53.         this->Human::print();
  54.         cout << "Университет: " << university << endl;
  55.         cout << "Факультет: " << faculty << endl;
  56.     }
  57.     virtual ~Student() {
  58.         cout << "Объект класса Student разрушен" << endl;
  59.     }
  60. };
  61.  
  62. class Father : public Human {
  63. public:
  64.     Human* wife;
  65.     int children_count;
  66.     Human** children;
  67.     Father(Father* h): Human(h) {
  68.     }
  69.     Father(string name, int age, int id, char gender, Human* wife) : Human(name, age, id, gender) {
  70.         this->wife = wife;
  71.         this->children_count = 0;
  72.         this->children = NULL;
  73.     }
  74.     virtual void print() {
  75.         this->Human::print();
  76.         cout << "Дети: " << children_count << endl;
  77.         if (children_count > 0) {
  78.             cout << "Имена детей: ";
  79.             for (int i = 0; i < children_count; i++) {
  80.                 cout << children[i]->name;
  81.                 if (i != children_count - 1) {
  82.                     cout << ", ";
  83.                 }
  84.                 else {
  85.                     cout << endl;
  86.                 }
  87.             }
  88.         }
  89.     }
  90.     void add_child(Human* child) {
  91.         if (children_count == 0) {
  92.             children = new Human*[1];
  93.             children[0] = child;
  94.             children_count++;
  95.             return; // предотвратить выполнение else
  96.         }
  97.         else {
  98.             children_count++;
  99.             Human** temp = new Human*[children_count]; // копия старого массива указателей
  100.             for (int i = 0; i < children_count - 1; i++) {
  101.                 temp[i] = children[i];
  102.             }
  103.             delete[] children; // удаляем только массив указателей
  104.             children = new Human*[children_count];
  105.             for (int i = 0; i < children_count - 1; i++) {
  106.                 children[i] = temp[i];
  107.             }
  108.             children[children_count - 1] = child;
  109.             delete[] temp;
  110.         }
  111.     }
  112.     void delete_child(Human* child) {
  113.         int pos = -1;
  114.         for (int i = 0; i < children_count; i++) {
  115.             if (children[i] == child) {
  116.                 pos = i;
  117.                 break;
  118.             }
  119.         }
  120.         if (pos == -1) {
  121.             cout << "[ОШИБКА] delete_child() -> " << child->name << " это не ребенок " << name << "!" << endl;
  122.         }
  123.         else {
  124.             Human** temp = new Human*[children_count]; // копия старого массива указателей
  125.             for (int i = 0; i < children_count - 1; i++) {
  126.                 if (i >= pos) {
  127.                     temp[i] = children[i + 1];
  128.                 }
  129.             }
  130.             delete[] children; // удаляем только массив указателей
  131.             children_count--;
  132.             children = new Human*[children_count];
  133.             for (int i = 0; i < children_count; i++) {
  134.                 children[i] = temp[i];
  135.             }
  136.             delete[] temp;
  137.         }
  138.         if (children_count == 0) {
  139.             children = NULL;
  140.         }
  141.     }
  142.     virtual ~Father() {
  143.         if (children != NULL) {
  144.             delete[] children;
  145.             children = NULL;
  146.         }
  147.         cout << "Объект класса Father разрушен" << endl;
  148.     }
  149. };
  150.  
  151. class FatherStudent : public Father, public Student {
  152. public:
  153.     FatherStudent(FatherStudent* h) : Student(h), Father(h) {
  154.         // дети не копируются, т.к они прикрепляются снаружи
  155.     }
  156.     FatherStudent(string name, int age, int id, char gender,
  157.         int course, string university, string faculty, Human* wife) :
  158.         Student(name, age, id, gender, course, university, faculty),
  159.         Father(name, age, id, gender, wife) {
  160.         this->course = course;
  161.         this->university = university;
  162.         this->faculty = faculty;
  163.         this->wife = wife;
  164.         this->children_count = 0;
  165.         this->children = NULL;
  166.     }
  167.     virtual void print() {
  168.         this->Father::print();
  169.     }
  170.     virtual ~FatherStudent() {
  171.         cout << "Объект класса FatherStudent разрушен" << endl;
  172.     }
  173. };
  174.  
  175. int main() {
  176.     setlocale(0, "");
  177.     Human* child_one = new Human("Школьница", 13, 289080, 'ж');
  178.     Student* child_two = new Student("Студент", 18, 160969, 'м', 1, "СВФУ", "ИМИ");
  179.  
  180.     Student* wife = new Student("Жена", 30, 160961, 'ж', 1, "СВФУ", "ИМИ");
  181.     FatherStudent* father = new FatherStudent("Папа", 33, 160962, 'м', 2, "СВФУ", "ИМИ", wife);
  182.     father->add_child(child_one);
  183.     father->add_child(child_two);
  184.  
  185.     father->print(); // будет два ребенка
  186.     cout << endl;
  187.  
  188.     father->children[0]->print();
  189.     cout << endl;
  190.  
  191.     father->children[1]->print();
  192.     cout << endl;
  193.  
  194.     father->delete_child(child_one);
  195.     father->print(); // останется только один ребенок
  196.     cout << endl;
  197.  
  198.     father->delete_child(wife); // не удалится
  199.     father->delete_child(child_one); // не удалится
  200.     father->delete_child(child_two);
  201.     father->print(); // не будет детей
  202.     cout << endl;
  203.  
  204.     FatherStudent* father2 = new FatherStudent(father);
  205.     father2->print();
  206.  
  207.     delete father;
  208.     delete wife;
  209.     delete child_one;
  210.     delete child_two;
  211. }
  212.  
  213. // Zadanie 2 ---------------------------------------------------------
  214. #include <iostream>
  215. #include <clocale>
  216. #include <string>
  217. #include <iomanip>
  218.  
  219. using namespace std;
  220.  
  221. class Matrix {
  222. public:
  223.     int x, y;
  224.     Matrix(int x, int y) {
  225.         this->x = x;
  226.         this->y = y;
  227.     }
  228.     int get_resolution() {
  229.         return x * y;
  230.     }
  231.     void print() {
  232.         cout << "Разрешение матрицы: " << x << "x" << y << endl;
  233.         cout << "Количество пикселей: " << get_resolution() << endl;
  234.     }
  235.     ~Matrix() {
  236.         cout << "Объект класса матрица разрушен" << endl;
  237.     }
  238. };
  239.  
  240. class CPU {
  241. public:
  242.     int frequency;
  243.     string arch;
  244.     char* cpu_name;
  245.     CPU(char* name, string arch, int frequency) {
  246.         this->cpu_name = name;
  247.         this->arch = arch;
  248.         this->frequency = frequency;
  249.     }
  250.     void set(char* name, string arch, int frequency) {
  251.         this->cpu_name = name;
  252.         this->arch = arch;
  253.         this->frequency = frequency;
  254.     }
  255.     void print() {
  256.         cout << "Название процессора: " << cpu_name << endl;
  257.         cout << "Архитектура: " << arch << endl;
  258.         cout << setprecision(1);
  259.         cout << "Частота: " << frequency << "GHz" << endl;
  260.     }
  261.     ~CPU(){
  262.         cout << "Объект класса процессор разрушен" << endl;
  263.     }
  264. };
  265.  
  266. class Smartphone: public Matrix, public CPU {
  267. private:
  268.     int memory;
  269.     float weight;
  270.     char* name;
  271. public:
  272.     Smartphone(char* name, float weight, int memory, int x, int y, char* cpu_name, string arch, int frequency):
  273.         Matrix(x, y),
  274.         CPU(cpu_name, arch, frequency) {
  275.         this->name = name;
  276.         this->weight = weight;
  277.         this->memory = memory;
  278.         this->x = x;
  279.         this->y = y;
  280.         this->cpu_name = cpu_name;
  281.         this->arch = arch;
  282.         this->frequency = frequency;
  283.     }
  284.     void print() {
  285.         cout << "Модель: " << name << endl;
  286.         cout << "Вес: " << weight << endl;
  287.         cout << "Память: " << memory << "GB" << endl;
  288.         this->Matrix::print();
  289.         this->CPU::print();
  290.     }
  291. };
  292.  
  293. int main() {
  294.     setlocale(0, "");
  295.     Smartphone* Z5c = new Smartphone(
  296.         "Sony Xperia Z5 Compact",
  297.         114.2f,
  298.         32,
  299.         1280, 720,
  300.         "Qualcomm Snapdragon 810",
  301.         "ARM",
  302.         2
  303.     );
  304.     Z5c->print();
  305.     cout << endl;
  306.  
  307.     delete Z5c;
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement