diff options
added bar task
7 files changed, 253 insertions, 0 deletions
diff --git a/Sem. 11/Examples/Person, Student, Teacher/Person/Person.cpp b/Sem. 11/Examples/Person, Student, Teacher/Person/Person.cpp new file mode 100644 index 0000000..a0cc714 --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/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, Student, Teacher/Person/Person.h b/Sem. 11/Examples/Person, Student, Teacher/Person/Person.h new file mode 100644 index 0000000..826b671 --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/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); +}; diff --git a/Sem. 11/Examples/Person, Student, Teacher/Source.cpp b/Sem. 11/Examples/Person, Student, Teacher/Source.cpp new file mode 100644 index 0000000..c2908c4 --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Source.cpp @@ -0,0 +1,21 @@ +#include <iostream> +#include "Person/Person.h" +#include "Student/Student.h" +#include "Teacher/Teacher.h" + +int main() +{ + char subjects[3][20] = { "English", "Mathematics", "Physics" }; + + char** subjectsPointers = new char* [3]; + subjectsPointers[0] = subjects[0]; + subjectsPointers[1] = subjects[1]; + subjectsPointers[2] = subjects[2]; + + Teacher t("Prof. Ivanov", 33, subjectsPointers, 3); + + std::cout << t.getName() << std::endl; + + delete[] subjectsPointers; //only the pointers - not the data! + +} diff --git a/Sem. 11/Examples/Person, Student, Teacher/Student/Student.cpp b/Sem. 11/Examples/Person, Student, Teacher/Student/Student.cpp new file mode 100644 index 0000000..a79d1fd --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Student/Student.cpp @@ -0,0 +1,34 @@ + #include "Student.h" + +Student::Student(const char* name, int age, size_t fn) : Person(name, age) +{ + setFn(fn); +} + +size_t Student::getFn() const +{ + return fn; +} + +void Student::setFn(size_t fn) +{ + + if (fn < 100) + fn = 100; + this->fn = fn; +} + +// The compiler will make: + +//Student(const Student& other) : Person(other), fn(other.fn), grade(other.grade) OK!!! +//{} + +//Student& operator=(const Student& other) OK!!!! +//{ +// if (this != &other) +// { +// Person::operator=(other); //handles the deletion and copying of the base class +// fn = other.fn; +// } +// return *this; +//}
\ No newline at end of file diff --git a/Sem. 11/Examples/Person, Student, Teacher/Student/Student.h b/Sem. 11/Examples/Person, Student, Teacher/Student/Student.h new file mode 100644 index 0000000..3e89213 --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Student/Student.h @@ -0,0 +1,24 @@ +#include <iostream> +#include "../Person/Person.h" + +class Student : public Person +{ + size_t fn = 0; +public: + Student() = default; + Student(const char* name, int age, size_t fn); + + size_t getFn() const; + void setFn(size_t fn); + + + // No need for big 4, since there is NO dyn. memory in Student + // The compiler will generate valid: + + //Student(const Student& other); + //Student& operator=(const Student& other); //no need + // ~Student() + + //See on the .cpp file how the compiler creates them + +}; diff --git a/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.cpp b/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.cpp new file mode 100644 index 0000000..d1b6bca --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.cpp @@ -0,0 +1,55 @@ +#include "Teacher.h" + +Teacher::Teacher(const char* name, int age, const char* const* subjects, size_t subjectsCount) : Person(name, age) +{ + this->subjects = new char* [subjectsCount]; + + for (size_t i = 0; i < subjectsCount; i++) + { + this->subjects[i] = new char[strlen(subjects[i]) + 1]; + strcpy(this->subjects[i], subjects[i]); + } + this->subjectsCount = subjectsCount; +} + +void Teacher::free() +{ + for (size_t i = 0; i < subjectsCount; i++) + delete[] subjects[i]; + delete[] subjects; +} +void Teacher::copyFrom(const Teacher& other) +{ + subjects = new char* [other.subjectsCount]; + + for (size_t i = 0; i < other.subjectsCount; i++) + { + subjects[i] = new char[strlen(other.subjects[i]) + 1]; + strcpy(subjects[i], other.subjects[i]); + } + + subjectsCount = other.subjectsCount; +} + +Teacher::Teacher(const Teacher& other) : Person(other) +{ + copyFrom(other); //only the teacher stuff is copied +} + +Teacher& Teacher::operator=(const Teacher& other) +{ + if (this != &other) + { + Person::operator=(other); //delete data of person + copy the data from person + free(); //teacher + copyFrom(other); //teacher + } + return *this; +} + + + +Teacher::~Teacher() +{ + free(); +} // destr person diff --git a/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.h b/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.h new file mode 100644 index 0000000..5ef0bd1 --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.h @@ -0,0 +1,16 @@ +#include "../Person/Person.h" + +class Teacher : public Person +{ + char** subjects; + size_t subjectsCount; + + void free(); + void copyFrom(const Teacher& other);// само нещата, които са от Teacher +public: + Teacher(const char* name, int age, const char* const * subjects, size_t subjectsCount); + + Teacher(const Teacher& other); + Teacher& operator=(const Teacher& other); + ~Teacher(); +};
\ No newline at end of file |
