diff options
3 files changed, 90 insertions, 0 deletions
| -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/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; + +}; |
