Advertisement
Guest User

.cpp

a guest
Dec 8th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include "Person.h"
  4. #include <string.h>
  5. #include <iostream>
  6. #include <exception>
  7.  
  8.  
  9.  
  10. int Person::next_ID = 0;
  11.  
  12. Person::Person() : ID(++next_ID)
  13. {
  14. if ((this->ID != 1) && (this->ID != 2))
  15. throw std::exception("Every person should have mother(except Adam and Eva, first people)");
  16. if (this->ID == 1)
  17. {
  18. SetName("Adam");
  19. SetGender("male");
  20. }
  21. else
  22. {
  23. SetName("Eva");
  24. SetGender("female");
  25. }
  26. }
  27. Person::Person(const char* name, const Person* mother) : ID(++next_ID)
  28. {
  29. if (name == NULL)
  30. throw "Invalid name";
  31. if (name[0] == '\0')
  32. throw std::exception("Invalid name length");
  33. SetName(name);
  34. this->mother = mother;
  35. this->father = nullptr;
  36. }
  37. Person::Person(const char* name, const Person* mother, const Person* father) :ID(++next_ID)
  38. {
  39. if (name == NULL)
  40. throw "Invalid name";
  41. if (name[0] == '\0')
  42. throw std::exception("Invalid name length");
  43. SetName(name);
  44. this->mother = mother;
  45. this->father = father;
  46. }
  47. const char* Person::GetName() const
  48. {
  49. return name;
  50. }
  51. void Person::SetName(const char* s)
  52. {
  53. this->name = new char [strlen(s) + 1];
  54. strcpy(name, s);
  55. }
  56. void Person::SetGender()
  57. {
  58. int a = rand() % 2;
  59. if (a == 0)
  60. {
  61. this->gender = "male";
  62. }
  63. else
  64. {
  65. this->gender = "female";
  66. }
  67. }
  68. void Person::SetGender(const char* s)
  69. {
  70. this->gender = s;
  71. }
  72. void Person::Erase() {
  73. delete[] name;
  74. }
  75. void Person::Clone(const Person& p) {
  76. name = new char[strlen(p.name) + 1];
  77. strcpy(name, p.name);
  78. }
  79. Person:: ~Person() {
  80. Erase();
  81. }
  82. Person::Person(const Person& p) : ID(++next_ID) {
  83. Clone(p);
  84. }
  85. void Person::SetMother(const Person* other)
  86. {
  87. this->mother = other;
  88. }
  89. void Person::SetFather(const Person* other)
  90. {
  91. this->father = other;
  92. }
  93. Person Person::GetMother() const
  94. {
  95. return *(this->mother);
  96. }
  97. Person Person::GetFather() const
  98. {
  99. return *(this->father);
  100. }
  101. //const char* Person::GetGender() const
  102. //{
  103. // return this->gender;
  104. //}
  105. Person Person::giveBirth(const Person* father)
  106. {
  107. if (this->gender = "male")
  108. throw std::exception("Today there is no way how man can give birth to a child by himself");
  109. char s[200];
  110. std::cout << "Enter child's name" << std::endl;
  111. std::cin.getline(s, 200);
  112. return Person(s, this, father);
  113. }
  114. Person Person::giveBirth()
  115. {
  116. if (this->gender = "male")
  117. throw std::exception("Today there is no way how man can give birth to a child by himself");
  118. char s[200];
  119. std::cout << "Enter child's name" << std::endl;
  120. std::cin.getline(s, 200);
  121. return Person(s, this);
  122. }
  123. std::ostream& operator << (std::ostream& s, const Person& p)
  124. {
  125. s << p.GetName()<<" "<<p.GetMother()<<" "<<p.GetFather();
  126. return s;
  127. }
  128. const Person& Person::operator =(const Person& p) {
  129. if (this != &p) {
  130. Erase(); Clone(p);
  131. }
  132. return *this;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement