Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 11 / Examples / Person
diff options
context:
space:
mode:
Commit5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2022-05-10 20:06:53 +0300
Committer
 angeld55 <angel_dimitriev@abv.bg>
2022-05-10 20:06:53 +0300
Files52f913ec9532fd07c8f679ccf1e68876d1fc9ff2 /Sem. 11/Examples/Person
Object-oriented_programming_FMI-5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf.tar
Object-oriented_programming_FMI-5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf.tar.gz
Object-oriented_programming_FMI-5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf.zip
Parent459d3ac280aafdba703930455f3200b6e7b0f31b (diff)
sem 11
2 files changed, 103 insertions, 0 deletions
-rw-r--r--Sem. 11/Examples/Person/Person.cpp76
-rw-r--r--Sem. 11/Examples/Person/Person.h27
diff --git a/Sem. 11/Examples/Person/Person.cpp b/Sem. 11/Examples/Person/Person.cpp
new file mode 100644
index 0000000..a0cc714
--- /dev/null
+++ b/Sem. 11/Examples/Person/Person.cpp
@@ -0,0 +1,76 @@
+#pragma once
+#include "Person.h"
+#include <cstring>
+#pragma warning (disable:4996)
+
+
+
+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)
+{
+ 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 (name == nullptr || 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;
+}
+
diff --git a/Sem. 11/Examples/Person/Person.h b/Sem. 11/Examples/Person/Person.h
new file mode 100644
index 0000000..826b671
--- /dev/null
+++ b/Sem. 11/Examples/Person/Person.h
@@ -0,0 +1,27 @@
+#pragma once
+#include <iostream>
+class Person
+{
+ char* name = nullptr;
+ int age = 0;
+
+ void copyFrom(const Person& other);
+ void free();
+
+public:
+ Person() = default;
+ Person(const char* name, int age);
+
+ Person(const Person& other);
+ Person& operator=(const Person& other);
+
+ const char* getName() const;
+ int getAge() const;
+
+ ~Person();
+ void print() const;
+
+protected:
+ void setName(const char* name);
+ void setAge(int age);
+};