Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 07
diff options
context:
space:
mode:
Commitaf355cb13b5cf168000b8461e6dfd89650cb6a86 (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2022-04-05 20:09:04 +0300
Committer
 GitHub <noreply@github.com>
2022-04-05 20:09:04 +0300
Files6370d58fd6574c08b29b7383d32c482f53877ac3 /Sem. 07
Object-oriented_programming_FMI-af355cb13b5cf168000b8461e6dfd89650cb6a86.tar
Object-oriented_programming_FMI-af355cb13b5cf168000b8461e6dfd89650cb6a86.tar.gz
Object-oriented_programming_FMI-af355cb13b5cf168000b8461e6dfd89650cb6a86.zip
Parent2cd0844968141790f40bb0225bebb00dbe4dea11 (diff)
Add files via upload
3 files changed, 109 insertions, 1 deletions
-rw-r--r--Sem. 07/Person/Person.cpp71
-rw-r--r--Sem. 07/Person/Person.h28
-rw-r--r--Sem. 07/Person/Source.cpp11
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 <cstring>
+
+
+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;
+}
+
diff --git a/Sem. 07/Person/Person.h b/Sem. 07/Person/Person.h
index 4bcfe98..960d96a 100644
--- a/Sem. 07/Person/Person.h
+++ b/Sem. 07/Person/Person.h
@@ -1 +1,27 @@
-d
+#pragma once
+#include <iostream>
+class Person
+{
+ char* name = nullptr;
+ int age;
+
+ 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;
+
+ void setName(const char* name);
+ void setAge(int age);
+
+ ~Person();
+ void print() const;
+
+};
diff --git a/Sem. 07/Person/Source.cpp b/Sem. 07/Person/Source.cpp
new file mode 100644
index 0000000..30ea7ba
--- /dev/null
+++ b/Sem. 07/Person/Source.cpp
@@ -0,0 +1,11 @@
+#include <iostream>
+#include <fstream>
+#include "Person.h"
+using namespace std;
+
+int main()
+{
+ Person p("Ivan", 43);
+ p.print();
+
+} \ No newline at end of file