Advertisement
Guest User

Системы/прогр ПМ-16 Николаев Влад - практика 7, обе задания

a guest
Dec 14th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.88 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.     delete father2;
  207.  
  208.     delete father;
  209.     delete wife;
  210.     delete child_one;
  211.     delete child_two;
  212. }
  213.  
  214. // Zadanie 2 ---------------------------------------------------------
  215. #include <iostream>
  216. #include <clocale>
  217. #include <string>
  218. #include <iomanip>
  219.  
  220. using namespace std;
  221.  
  222. class Matrix {
  223. public:
  224.     int x, y;
  225.     Matrix(int x, int y) {
  226.         this->x = x;
  227.         this->y = y;
  228.     }
  229.     int get_resolution() {
  230.         return x * y;
  231.     }
  232.     void print() {
  233.         cout << "Разрешение матрицы: " << x << "x" << y << endl;
  234.         cout << "Количество пикселей: " << get_resolution() << endl;
  235.     }
  236.     ~Matrix() {
  237.         cout << "Объект класса матрица разрушен" << endl;
  238.     }
  239. };
  240.  
  241. class CPU {
  242. public:
  243.     int frequency;
  244.     string arch;
  245.     char* cpu_name;
  246.     CPU(char* name, string arch, int frequency) {
  247.         this->cpu_name = name;
  248.         this->arch = arch;
  249.         this->frequency = frequency;
  250.     }
  251.     void set(char* name, string arch, int frequency) {
  252.         this->cpu_name = name;
  253.         this->arch = arch;
  254.         this->frequency = frequency;
  255.     }
  256.     void print() {
  257.         cout << "Название процессора: " << cpu_name << endl;
  258.         cout << "Архитектура: " << arch << endl;
  259.         cout << setprecision(1);
  260.         cout << "Частота: " << frequency << "GHz" << endl;
  261.     }
  262.     ~CPU(){
  263.         cout << "Объект класса процессор разрушен" << endl;
  264.     }
  265. };
  266.  
  267. class Smartphone: public Matrix, public CPU {
  268. private:
  269.     int memory;
  270.     float weight;
  271.     char* name;
  272. public:
  273.     Smartphone(char* name, float weight, int memory, int x, int y, char* cpu_name, string arch, int frequency):
  274.         Matrix(x, y),
  275.         CPU(cpu_name, arch, frequency) {
  276.         this->name = name;
  277.         this->weight = weight;
  278.         this->memory = memory;
  279.         this->x = x;
  280.         this->y = y;
  281.         this->cpu_name = cpu_name;
  282.         this->arch = arch;
  283.         this->frequency = frequency;
  284.     }
  285.     void print() {
  286.         cout << "Модель: " << name << endl;
  287.         cout << "Вес: " << weight << endl;
  288.         cout << "Память: " << memory << "GB" << endl;
  289.         this->Matrix::print();
  290.         this->CPU::print();
  291.     }
  292. };
  293.  
  294. int main() {
  295.     setlocale(0, "");
  296.     Smartphone* Z5c = new Smartphone(
  297.         "Sony Xperia Z5 Compact",
  298.         114.2f,
  299.         32,
  300.         1280, 720,
  301.         "Qualcomm Snapdragon 810",
  302.         "ARM",
  303.         2
  304.     );
  305.     Z5c->print();
  306.     cout << endl;
  307.  
  308.     delete Z5c;
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement