Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "Person.h"
- #include <string.h>
- #include <iostream>
- #include <exception>
- int Person::next_ID = 0;
- Person::Person() : ID(++next_ID)
- {
- if ((this->ID != 1) && (this->ID != 2))
- throw std::exception("Every person should have mother(except Adam and Eva, first people)");
- if (this->ID == 1)
- {
- SetName("Adam");
- SetGender("male");
- }
- else
- {
- SetName("Eva");
- SetGender("female");
- }
- }
- Person::Person(const char* name, const Person* mother) : ID(++next_ID)
- {
- if (name == NULL)
- throw "Invalid name";
- if (name[0] == '\0')
- throw std::exception("Invalid name length");
- SetName(name);
- this->mother = mother;
- this->father = nullptr;
- }
- Person::Person(const char* name, const Person* mother, const Person* father) :ID(++next_ID)
- {
- if (name == NULL)
- throw "Invalid name";
- if (name[0] == '\0')
- throw std::exception("Invalid name length");
- SetName(name);
- this->mother = mother;
- this->father = father;
- }
- const char* Person::GetName() const
- {
- return name;
- }
- void Person::SetName(const char* s)
- {
- this->name = new char [strlen(s) + 1];
- strcpy(name, s);
- }
- void Person::SetGender()
- {
- int a = rand() % 2;
- if (a == 0)
- {
- this->gender = "male";
- }
- else
- {
- this->gender = "female";
- }
- }
- void Person::SetGender(const char* s)
- {
- this->gender = s;
- }
- void Person::Erase() {
- delete[] name;
- }
- void Person::Clone(const Person& p) {
- name = new char[strlen(p.name) + 1];
- strcpy(name, p.name);
- }
- Person:: ~Person() {
- Erase();
- }
- Person::Person(const Person& p) : ID(++next_ID) {
- Clone(p);
- }
- void Person::SetMother(const Person* other)
- {
- this->mother = other;
- }
- void Person::SetFather(const Person* other)
- {
- this->father = other;
- }
- Person Person::GetMother() const
- {
- return *(this->mother);
- }
- Person Person::GetFather() const
- {
- return *(this->father);
- }
- //const char* Person::GetGender() const
- //{
- // return this->gender;
- //}
- Person Person::giveBirth(const Person* father)
- {
- if (this->gender = "male")
- throw std::exception("Today there is no way how man can give birth to a child by himself");
- char s[200];
- std::cout << "Enter child's name" << std::endl;
- std::cin.getline(s, 200);
- return Person(s, this, father);
- }
- Person Person::giveBirth()
- {
- if (this->gender = "male")
- throw std::exception("Today there is no way how man can give birth to a child by himself");
- char s[200];
- std::cout << "Enter child's name" << std::endl;
- std::cin.getline(s, 200);
- return Person(s, this);
- }
- std::ostream& operator << (std::ostream& s, const Person& p)
- {
- s << p.GetName()<<" "<<p.GetMother()<<" "<<p.GetFather();
- return s;
- }
- const Person& Person::operator =(const Person& p) {
- if (this != &p) {
- Erase(); Clone(p);
- }
- return *this;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement