From af355cb13b5cf168000b8461e6dfd89650cb6a86 Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Tue, 5 Apr 2022 20:09:04 +0300 Subject: Add files via upload --- Sem. 07/Person/Person.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Sem. 07/Person/Person.cpp (limited to 'Sem. 07/Person/Person.cpp') diff --git a/Sem. 07/Person/Person.cpp b/Sem. 07/Person/Person.cpp new file mode 100644 index 0000000..36262a4 --- /dev/null +++ b/Sem. 07/Person/Person.cpp @@ -0,0 +1,71 @@ +#pragma once +#include "Person.h" +#include + + +void Person::copyFrom(const Person& other) +{ + name = new char[strlen(other.name) + 1]; + strcpy(name, other.name); + age = other.age; +} +void Person::free() +{ + delete[] name; +} + +Person::Person(const char* name, int age) : age(3) +{ + setName(name); + setAge(age); +} +Person::Person(const Person& other) +{ + copyFrom(other); +} + +Person& Person::operator=(const Person& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +const char* Person::getName() const +{ + return name; +} + +int Person::getAge() const +{ + return age; +} + +void Person::setName(const char* name) +{ + if (this->name == name) + return; + + delete[] this->name; + size_t nameLen = strlen(name); + this->name = new char[nameLen + 1]; + strcpy(this->name, name); +} + +void Person::setAge(int age) +{ + this->age = age; +} + + +Person::~Person() +{ + free(); +} +void Person::print() const +{ + std::cout << name << " " << age << std::endl; +} + -- cgit v1.3.1-23-gd82d