diff options
added new examples
6 files changed, 234 insertions, 0 deletions
| -rw-r--r-- | Big4_basic_examples/InformaticsStudent/InformaticsStudent.cpp | 103 | ||||
| -rw-r--r-- | Big4_basic_examples/InformaticsStudent/InformaticsStudent.h | 40 | ||||
| -rw-r--r-- | Big4_basic_examples/README.md | 1 | ||||
| -rw-r--r-- | Big4_basic_examples/TestClass/Source.cpp | 20 | ||||
| -rw-r--r-- | Big4_basic_examples/TestClass/TestClass.cpp | 45 | ||||
| -rw-r--r-- | Big4_basic_examples/TestClass/TestClass.h | 25 |
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; + +}; diff --git a/Big4_basic_examples/README.md b/Big4_basic_examples/README.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Big4_basic_examples/README.md @@ -0,0 +1 @@ + diff --git a/Big4_basic_examples/TestClass/Source.cpp b/Big4_basic_examples/TestClass/Source.cpp new file mode 100644 index 0000000..bdb8002 --- /dev/null +++ b/Big4_basic_examples/TestClass/Source.cpp @@ -0,0 +1,20 @@ +#include <iostream> +#include "TestClass.h" + +using namespace std; + + +int main() +{ + TestClass obj1(3, 3, "abc"); + + TestClass obj2(obj1);//copy const; + + TestClass obj3(3, 1, "333"); + + obj3 = obj1; // operator=; + + TestClass obj4 = obj3; ////copy const; + + +} //destructors
\ No newline at end of file diff --git a/Big4_basic_examples/TestClass/TestClass.cpp b/Big4_basic_examples/TestClass/TestClass.cpp new file mode 100644 index 0000000..fe5692b --- /dev/null +++ b/Big4_basic_examples/TestClass/TestClass.cpp @@ -0,0 +1,45 @@ +#include "TestClass.h" + + +TestClass::TestClass() : TestClass(0, 0, "Unknown") +{ + +} +TestClass::TestClass(int a, int b, const char* str) +{ + this->a = a; + this->b = b; + name = new char[strlen(str) + 1]; + strcpy(name, str); +} +TestClass::TestClass(const TestClass& other) +{ + CopyFrom(other); + //name = other.name NO! only the pointer will be copied. +} + +TestClass& TestClass::operator=(const TestClass& other) +{ + if (this != &other) + { + Free(); + CopyFrom(other); + } + return *this; // this TestClass* +} +TestClass::~TestClass() +{ + Free(); +} +void TestClass::CopyFrom(const TestClass& other) +{ + name = new char[strlen(other.name) + 1]; + strcpy(name, other.name); + a = other.a; + b = other.b; +} +void TestClass::Free() +{ + delete[] name; + a = b = 0; +} diff --git a/Big4_basic_examples/TestClass/TestClass.h b/Big4_basic_examples/TestClass/TestClass.h new file mode 100644 index 0000000..4841785 --- /dev/null +++ b/Big4_basic_examples/TestClass/TestClass.h @@ -0,0 +1,25 @@ +#include <iostream> +class TestClass +{ +private: + + void CopyFrom(const TestClass& other); + void Free(); + int a; + int b; + char* name; + +public: + + + TestClass(int a, int b, const char* str); + + // big4 + TestClass(); + TestClass(const TestClass& other); + TestClass& operator=(const TestClass& other); + ~TestClass(); + + void Print() const; + +}; |
