SHARE
TWEET

Untitled

a guest Feb 17th, 2020 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class Man;
  8. class Database;
  9.  
  10. class Man {
  11. private:
  12.     string name, surname;
  13.     int age;
  14.     Man* partner;
  15.     vector<Man*> kids;
  16.     Man* par1;
  17.     Man* par2;
  18.     Database* db;
  19.     Man(string name_, string surname_, Man* par1_, Man* par2_, Database* db_ = nullptr) {
  20.         name = name_;
  21.         surname = surname_;
  22.         age = 0;
  23.         partner = nullptr;
  24.         db = db_;
  25.         if (db != nullptr) {
  26.             db->addMan(this);
  27.         }
  28.         par1 = par1_;
  29.         par2 = par2_;
  30.     }
  31. public:
  32.     Man() :name("undefined"), surname("undefined"), age(-1), partner(nullptr), db(nullptr), par1(nullptr), par2(nullptr) {}
  33.     Man(string name_, string surname_, int age_,
  34.         Man* partner_ = nullptr, Database* db_ = nullptr,
  35.         Man* par1_ = nullptr, Man* par2_ = nullptr) :
  36.         name(name_), surname(surname_), age(age_),
  37.         partner(partner_), par1(par1_), par2(par2_) {
  38.         if (db_ != nullptr) {
  39.             db = db_;
  40.             db->addMan(this);
  41.         }
  42.     }
  43.     ~Man() {
  44.         if (partner != nullptr) {
  45.             partner->delPartner();
  46.         }
  47.         if (db != nullptr) {
  48.             db->delMan(this);
  49.         }
  50.         if (par1) {
  51.             par1->delKid(this);
  52.         }
  53.         if (par2) {
  54.             par2->delKid(this);
  55.         }
  56.     }
  57.     void giveBirth(string name_) {
  58.         Man* kid = new Man(name_, surname, this, partner, db);
  59.         kids.push_back(kid);
  60.         if (partner != nullptr) {
  61.             partner->addKid(kid, this);
  62.         }
  63.     }
  64.     void addKid(Man* kid, Man* kid_partner) {
  65.         if (partner == kid_partner) {
  66.             kids.push_back(kid);
  67.         }
  68.     }
  69.     void delPartner() {
  70.         partner = nullptr;
  71.     }
  72.     string getName() {
  73.         return name;
  74.     }
  75.     string getSurname() {
  76.         return surname;
  77.     }
  78.     void delKid(Man* kid) {
  79.         for (size_t i = 0; i < kids.size(); ++i) {
  80.             if (kids[i] == kid) {
  81.                 kids.erase(kids.begin() + i);
  82.                 break;
  83.             }
  84.         }
  85.     }
  86.     void marry(Man* p) {
  87.         partner = p;
  88.         p->setPartner(this);
  89.     }
  90.     void setPartner(Man* p) {
  91.         partner = p;
  92.     }
  93. };
  94.  
  95. class Database {
  96. private:
  97.     vector<Man*> people;
  98. public:
  99.     Man* findByName(const string& name, const string& surname) {
  100.         for (size_t i = 0; i < people.size(); ++i) {
  101.             if (people[i]->getName() == name && people[i]->getSurname() == surname) {
  102.                 return people[i];
  103.             }
  104.         }
  105.     }
  106.     void addMan(Man* man) {
  107.         people.push_back(man);
  108.     }
  109.     void delMan(Man* man) {
  110.         for (size_t i = 0; i < people.size(); ++i) {
  111.             if (people[i] == man) {
  112.                 people.erase(people.begin() + i);
  113.                 break;
  114.             }
  115.         }
  116.     }
  117. };
  118.  
  119. int main() {
  120.    
  121. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top