Website Summary Files Commits Branches @commit @diff @stats
path: root / Big4_basic_examples / InformaticsStudent
diff options
context:
space:
mode:
Commit112a0d344da9e66d20ee9ab5d488e59c5d52e6ad (patch)
Author
 Angeld55 <angel_dimitriev@abv.bg>
2020-04-09 18:09:42 +0300
Committer
 Angeld55 <angel_dimitriev@abv.bg>
2020-04-09 18:09:42 +0300
Filese3bb9cf17c24a3b53c746d9142906d550d549465 /Big4_basic_examples/InformaticsStudent
Object-oriented_programming_FMI-112a0d344da9e66d20ee9ab5d488e59c5d52e6ad.tar
Object-oriented_programming_FMI-112a0d344da9e66d20ee9ab5d488e59c5d52e6ad.tar.gz
Object-oriented_programming_FMI-112a0d344da9e66d20ee9ab5d488e59c5d52e6ad.zip
Parentec26fab32d144b436d08f9da6049bd053156d783 (diff)
added new examples
diff --git a/Big4_basic_examples/InformaticsStudent/InformaticsStudent.cpp b/Big4_basic_examples/InformaticsStudent/InformaticsStudent.cpp
new file mode 100644
index 0000000..9073355
--- /dev/null
+++ b/Big4_basic_examples/InformaticsStudent/InformaticsStudent.cpp
@@ -0,0 +1,103 @@
+#include "InformaticsStudent.h"
+#include <cstring>
+
+
+bool hasDigits(const char* str,int len)
+{
+ for (int i = 0; i < len;i++)
+ {
+ if (str[i] >= '0' && str[i] <= '9')
+ return true;
+ }
+ return false;
+}
+
+void InformaticsStudent::CopyFrom(const InformaticsStudent& other)
+{
+ int len = strlen(other.name);
+ name = new char[len + 1];
+ strcpy(name, other.name); //copy name
+ fn = other.fn; //copy fn
+}
+void InformaticsStudent::Free()
+{
+ delete[] name;
+ fn = 0;
+}
+
+InformaticsStudent::InformaticsStudent()
+{
+ name = nullptr;
+ fn = 0;
+}
+
+InformaticsStudent::InformaticsStudent(const char* name, int fn) //При конструктура винаги КОПИРАМЕ подадения стринг за нашния нов обект.
+{ // Това е важно, защото не искаме някой друг да има достъп до него.
+ setName(name);
+ setFn(fn);
+}
+
+
+InformaticsStudent::InformaticsStudent(const InformaticsStudent& other)
+{
+ //Използваме функцията, която копира всичко от other в нашия обект.
+ CopyFrom(other);
+
+ //Понеже това е конструктор, няма нужда да трия нищо от обекта, понеже няма как да е имало данни тук,защото сега го създаваме.
+}
+
+InformaticsStudent& InformaticsStudent::operator=(const InformaticsStudent& other)
+{
+ //За разлика от при Копи-контструктора , тука е имало данни , преди копирането
+ // (обектът да е съществувал). За това преди да копираме трябва да изтрием това, което е седяло тук.
+ // Проверката на 44 ред е с цел да не стане нещо от сорта T1=T1. Това би изтрило данните в обекта, защото първо трием и после
+ // присвояваме вече изтритите данни.
+
+ if (this != &other)
+ {
+ Free(); //трия своята памет
+
+ CopyFrom(other); // копирам от другия обект
+ }
+ return *this; // Връщаме указател към обекта, в който сме копирали, за може да chain-ваме T1=T2=T3.
+}
+
+InformaticsStudent::~InformaticsStudent()
+{
+ Free();
+}
+
+
+void InformaticsStudent::setName(const char* name)
+{
+ delete[] name; //трием данните, които са били тук.
+ int len = strlen(name);
+ if (hasDigits(name, len))
+ {
+ this->name[0] = '\0';
+ return;
+ }
+ this->name = new char[len + 1];
+ strcpy(this->name, name);
+
+
+}
+void InformaticsStudent::setFn(int fn)
+{
+ if (fn < 45000 || fn >45999)
+ fn = 45000;
+ this->fn = fn;
+}
+const char* InformaticsStudent::getName() const
+{
+ return name;
+}
+int InformaticsStudent::getFn() const
+{
+ return fn;
+}
+
+void InformaticsStudent::printStudent() const
+{
+ std::cout << name << " fn: " << fn << std::endl;
+}
diff --git a/Big4_basic_examples/InformaticsStudent/InformaticsStudent.h b/Big4_basic_examples/InformaticsStudent/InformaticsStudent.h
new file mode 100644
index 0000000..6e9d743
--- /dev/null
+++ b/Big4_basic_examples/InformaticsStudent/InformaticsStudent.h
@@ -0,0 +1,40 @@
+
+// Голямта 4-ка - Функции, които трябва да разпишем, САМО когато имаме динамична памет в класа.
+
+// 1.Defualt constructor InformaticsStudent();
+// 2.Copy-constructor InformaticsStudent(const InformaticsStudent& other);
+// 3.operator= InformaticsStudent& operator=(const InformaticsStudent& other);
+// 4.Destructor ~InformaticsStudent()
+// (Най-лесно е като си разпишем функция за копиране CopyFrom и функция за триене Free())
+
+#pragma once
+#include <iostream>
+
+class InformaticsStudent
+{
+private:
+ char* name;
+ int fn;
+
+ void CopyFrom(const InformaticsStudent& other);
+ void Free();
+
+public:
+ /// BIG 4 starts here
+
+ InformaticsStudent();
+ InformaticsStudent(const InformaticsStudent& other);
+ InformaticsStudent& operator=(const InformaticsStudent& other);
+ ~InformaticsStudent();
+ // BIG 4 ends here
+
+ InformaticsStudent(const char* name, int fn);
+
+ void setName(const char* name);
+ void setFn(int fn);
+ const char* getName() const;
+ int getFn() const;
+
+ void printStudent() const;
+
+};