Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Zadanie 1
- #include <iostream>
- #include <clocale>
- #include <string>
- using namespace std;
- class Human {
- public:
- string name;
- int age;
- int id;
- char gender;
- Human(Human* h) {
- name = h->name;
- age = h->age;
- id = h->id;
- gender = h->gender;
- }
- Human(string name, int age, int id, char gender) {
- this->name = name;
- this->age = age;
- this->id = id;
- this->gender = gender;
- }
- virtual void print() {
- cout << "Имя: " << name << ", " << age << " лет" << endl;
- cout << "Пол: " << gender << endl;
- cout << "Номер паспорта: " << id << endl;
- }
- virtual ~Human() {
- cout << "Объект класса Human разрушен" << endl;
- }
- };
- class Student : public Human {
- public:
- int course;
- string university;
- string faculty;
- Student(Student* h): Human(h->name, h->age, h->id, h->gender) {
- course = h->course;
- university = h->university;
- faculty = h->faculty;
- }
- Student(string name, int age, int id, char gender, int course, string university, string faculty) :
- Human(name, age, id, gender) {
- this->course = course;
- this->university = university;
- this->faculty = faculty;
- }
- virtual void print() {
- this->Human::print();
- cout << "Университет: " << university << endl;
- cout << "Факультет: " << faculty << endl;
- }
- virtual ~Student() {
- cout << "Объект класса Student разрушен" << endl;
- }
- };
- class Father : public Human {
- public:
- Human* wife;
- int children_count;
- Human** children;
- Father(Father* h): Human(h) {
- }
- Father(string name, int age, int id, char gender, Human* wife) : Human(name, age, id, gender) {
- this->wife = wife;
- this->children_count = 0;
- this->children = NULL;
- }
- virtual void print() {
- this->Human::print();
- cout << "Дети: " << children_count << endl;
- if (children_count > 0) {
- cout << "Имена детей: ";
- for (int i = 0; i < children_count; i++) {
- cout << children[i]->name;
- if (i != children_count - 1) {
- cout << ", ";
- }
- else {
- cout << endl;
- }
- }
- }
- }
- void add_child(Human* child) {
- if (children_count == 0) {
- children = new Human*[1];
- children[0] = child;
- children_count++;
- return; // предотвратить выполнение else
- }
- else {
- children_count++;
- Human** temp = new Human*[children_count]; // копия старого массива указателей
- for (int i = 0; i < children_count - 1; i++) {
- temp[i] = children[i];
- }
- delete[] children; // удаляем только массив указателей
- children = new Human*[children_count];
- for (int i = 0; i < children_count - 1; i++) {
- children[i] = temp[i];
- }
- children[children_count - 1] = child;
- delete[] temp;
- }
- }
- void delete_child(Human* child) {
- int pos = -1;
- for (int i = 0; i < children_count; i++) {
- if (children[i] == child) {
- pos = i;
- break;
- }
- }
- if (pos == -1) {
- cout << "[ОШИБКА] delete_child() -> " << child->name << " это не ребенок " << name << "!" << endl;
- }
- else {
- Human** temp = new Human*[children_count]; // копия старого массива указателей
- for (int i = 0; i < children_count - 1; i++) {
- if (i >= pos) {
- temp[i] = children[i + 1];
- }
- }
- delete[] children; // удаляем только массив указателей
- children_count--;
- children = new Human*[children_count];
- for (int i = 0; i < children_count; i++) {
- children[i] = temp[i];
- }
- delete[] temp;
- }
- if (children_count == 0) {
- children = NULL;
- }
- }
- virtual ~Father() {
- if (children != NULL) {
- delete[] children;
- children = NULL;
- }
- cout << "Объект класса Father разрушен" << endl;
- }
- };
- class FatherStudent : public Father, public Student {
- public:
- FatherStudent(FatherStudent* h) : Student(h), Father(h) {
- // дети не копируются, т.к они прикрепляются снаружи
- }
- FatherStudent(string name, int age, int id, char gender,
- int course, string university, string faculty, Human* wife) :
- Student(name, age, id, gender, course, university, faculty),
- Father(name, age, id, gender, wife) {
- this->course = course;
- this->university = university;
- this->faculty = faculty;
- this->wife = wife;
- this->children_count = 0;
- this->children = NULL;
- }
- virtual void print() {
- this->Father::print();
- }
- virtual ~FatherStudent() {
- cout << "Объект класса FatherStudent разрушен" << endl;
- }
- };
- int main() {
- setlocale(0, "");
- Human* child_one = new Human("Школьница", 13, 289080, 'ж');
- Student* child_two = new Student("Студент", 18, 160969, 'м', 1, "СВФУ", "ИМИ");
- Student* wife = new Student("Жена", 30, 160961, 'ж', 1, "СВФУ", "ИМИ");
- FatherStudent* father = new FatherStudent("Папа", 33, 160962, 'м', 2, "СВФУ", "ИМИ", wife);
- father->add_child(child_one);
- father->add_child(child_two);
- father->print(); // будет два ребенка
- cout << endl;
- father->children[0]->print();
- cout << endl;
- father->children[1]->print();
- cout << endl;
- father->delete_child(child_one);
- father->print(); // останется только один ребенок
- cout << endl;
- father->delete_child(wife); // не удалится
- father->delete_child(child_one); // не удалится
- father->delete_child(child_two);
- father->print(); // не будет детей
- cout << endl;
- FatherStudent* father2 = new FatherStudent(father);
- father2->print();
- delete father;
- delete wife;
- delete child_one;
- delete child_two;
- }
- // Zadanie 2 ---------------------------------------------------------
- #include <iostream>
- #include <clocale>
- #include <string>
- #include <iomanip>
- using namespace std;
- class Matrix {
- public:
- int x, y;
- Matrix(int x, int y) {
- this->x = x;
- this->y = y;
- }
- int get_resolution() {
- return x * y;
- }
- void print() {
- cout << "Разрешение матрицы: " << x << "x" << y << endl;
- cout << "Количество пикселей: " << get_resolution() << endl;
- }
- ~Matrix() {
- cout << "Объект класса матрица разрушен" << endl;
- }
- };
- class CPU {
- public:
- int frequency;
- string arch;
- char* cpu_name;
- CPU(char* name, string arch, int frequency) {
- this->cpu_name = name;
- this->arch = arch;
- this->frequency = frequency;
- }
- void set(char* name, string arch, int frequency) {
- this->cpu_name = name;
- this->arch = arch;
- this->frequency = frequency;
- }
- void print() {
- cout << "Название процессора: " << cpu_name << endl;
- cout << "Архитектура: " << arch << endl;
- cout << setprecision(1);
- cout << "Частота: " << frequency << "GHz" << endl;
- }
- ~CPU(){
- cout << "Объект класса процессор разрушен" << endl;
- }
- };
- class Smartphone: public Matrix, public CPU {
- private:
- int memory;
- float weight;
- char* name;
- public:
- Smartphone(char* name, float weight, int memory, int x, int y, char* cpu_name, string arch, int frequency):
- Matrix(x, y),
- CPU(cpu_name, arch, frequency) {
- this->name = name;
- this->weight = weight;
- this->memory = memory;
- this->x = x;
- this->y = y;
- this->cpu_name = cpu_name;
- this->arch = arch;
- this->frequency = frequency;
- }
- void print() {
- cout << "Модель: " << name << endl;
- cout << "Вес: " << weight << endl;
- cout << "Память: " << memory << "GB" << endl;
- this->Matrix::print();
- this->CPU::print();
- }
- };
- int main() {
- setlocale(0, "");
- Smartphone* Z5c = new Smartphone(
- "Sony Xperia Z5 Compact",
- 114.2f,
- 32,
- 1280, 720,
- "Qualcomm Snapdragon 810",
- "ARM",
- 2
- );
- Z5c->print();
- cout << endl;
- delete Z5c;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement