Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. class Human {
  7. public:
  8.     Human();
  9.     Human(string, long, char);
  10.     ~Human();
  11.     void setName(string);
  12.     void setPassport(long);
  13.     void setGender(char);
  14.     void add(Human*);
  15.     void print();
  16.  
  17. private:
  18.     string name;
  19.     long passport;
  20.     char gender;
  21.     Human **siblings;
  22.     int n;
  23. };
  24.  
  25. Human::Human() {
  26.     n = 0;
  27.     siblings = NULL;
  28. }
  29. Human::Human(string name, long passport, char gender) {
  30.     n = 0;
  31.     siblings = NULL;
  32.     this->name = name;
  33.     this->passport = passport;
  34.     this->gender = gender;
  35. }
  36.  
  37. Human::~Human() {
  38. }
  39. void Human::setName(string name) {
  40.     this->name = name;
  41. }
  42. void Human::setPassport(long passport) {
  43.     this->passport = passport;
  44. }
  45. void Human::setGender(char gender) {
  46.     this->gender = gender;
  47. }
  48.  
  49. void Human::add(Human* a) {
  50.     if (n == 0) {
  51.         siblings = new Human*;
  52.         siblings[0] = a;
  53.     }
  54.     else {
  55.         Human **temp = new Human*;
  56.         temp[0] = new Human[n];
  57.         for (int i = 0; i < n; i++) {
  58.             temp[0][i].name = siblings[0][i].name;
  59.             temp[0][i].passport = siblings[0][i].passport;
  60.             temp[0][i].gender = siblings[0][i].gender;
  61.         }
  62.         delete[] siblings[0];
  63.         delete[] siblings;
  64.         siblings = new Human*;
  65.         siblings[0] = new Human[n + 1];
  66.         for (int i = 0; i < n; i++) {
  67.             siblings[0][i].name = temp[0][i].name;
  68.             siblings[0][i].passport = temp[0][i].passport;
  69.             siblings[0][i].gender = temp[0][i].gender;
  70.         }
  71.         siblings[0][n].name = a->name;
  72.         siblings[0][n].gender = a->gender;
  73.         siblings[0][n].passport = a->passport;
  74.     }
  75.     n++;
  76. }
  77. void Human::print() {
  78.     cout << "Имя: " << name << endl
  79.         << "Пол: " << gender << endl
  80.         << "Паспорт: " << passport << endl
  81.         << "-------------" << endl;
  82.     for (int i = 0; i < n; i++) {
  83.         siblings[0][i].print();
  84.     }
  85. }
  86.  
  87. int main() {
  88.     setlocale(LC_ALL, "");
  89.     Human mom("Мать", 1, 'Ж');
  90.     Human dad("Отец", 2, 'М');
  91.     Human son("Сын", 3, 'М');
  92.     mom.add(&son);
  93.     dad.add(&son);
  94.     mom.print();
  95.     cout << "**************************" << endl;
  96.     dad.print();
  97.     cout << "**************************" << endl;
  98.     Human waifu;
  99.     waifu.setName("Жена сына");
  100.     waifu.setPassport(4);
  101.     waifu.setGender('Ж');
  102.     Human gson;
  103.     gson.setName("Внук");
  104.     gson.setPassport(5);
  105.     gson.setGender('М');
  106.     waifu.add(&gson);
  107.     son.add(&gson);
  108.     son.print();
  109.     cout << "**************************" << endl;
  110.     waifu.print();
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement