Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
1.4 kB
23
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

class Heart {
 public:
  Heart(string person_name) : person_name_(person_name) {}
  ~Heart() { cout << "~Heart of " << person_name_ << endl; }
  void Beats();

 private:
  string person_name_;
};

// Declare
class Person;

class Home {
 public:
  Home(string address) : address_(address) {}
  ~Home() { cout << "~Home of address: " << address_ << endl; }

  void AddPerson(Person *person) {
    persons_.push_back(person);
  }

  string address_;

private:
  vector<Person*> persons_; // Association
};

// Define
class Person {
 public:
  Person(string person_name)
      : heart_(person_name),
        person_name_(person_name) {}

  ~Person() {
    cout << "~Person " << person_name_ << endl;
  }

  void AddHome(Home *home) {
    homes_.push_back(home);
  }

 private:
  // Member
  Heart heart_;  // Composition

  // Member
  vector<Home*> homes_;  // Association

  string person_name_;
};

int main() {
  Home *home1 = new Home("delhi");
  Home *home2 = new Home("bangalore");
  Home *home3 = new Home("mumbai");

  Person *person1 = new Person("A");
  Person *person2 = new Person("B");

  person1->AddHome(home1);
  home1->AddPerson(person1);

  person1->AddHome(home2);
  home2->AddPerson(person1);

  person2->AddHome(home2);
  home2->AddPerson(person2);

  person2->AddHome(home3);
  home3->AddPerson(person2);

  return 0;
}
Leave a Comment