diff --git a/c++/example2.txt b/c++/example2.txt new file mode 100644 index 0000000000000000000000000000000000000000..2a8c16d2efee5f18352bffbcb20c91cb3fb83d7d --- /dev/null +++ b/c++/example2.txt @@ -0,0 +1 @@ +Writing this to a file diff --git a/c++/mytest b/c++/mytest new file mode 100755 index 0000000000000000000000000000000000000000..e8e418ddb6388371aa793da557f3454e49bc75ee Binary files /dev/null and b/c++/mytest differ diff --git a/c++/mytest.cpp b/c++/mytest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e33d7348d8399ded3bedba69cbe8f4da0c7e2e5 --- /dev/null +++ b/c++/mytest.cpp @@ -0,0 +1,20 @@ +#include <iostream> +#include <fstream> +using namespace std; + +class File_Type { +public: + void doit() { + } +//private: + fstream fs; +}; + +int main (void) { + File_Type l_file; + l_file.fs.open ("example2.txt",ios_base::out); + l_file.fs << "Writing this to a file" << endl; + l_file.fs.close(); +; + return 0; +} diff --git a/c++/testvector b/c++/testvector new file mode 100755 index 0000000000000000000000000000000000000000..da2e15fec86a91f94f5a5134e8fcad8812be2cc3 Binary files /dev/null and b/c++/testvector differ diff --git a/c++/testvector.cpp b/c++/testvector.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac5a9008c78edeccb70cabb16251890658463946 --- /dev/null +++ b/c++/testvector.cpp @@ -0,0 +1,69 @@ +#include <iostream> +#include <vector> +#include <map> + +using namespace std; + +class A { +public: + // constructor + A(int i) + { + // it always begins with this constructor + // set the reference count to 1 + ptr = new int(i); + ref[ptr] = 1; + + cout << "CTOR NEW " << *ptr << " - Ref Count = " << ref[ptr] << endl; + } + + // copy constructor + A(const A & other ) : ptr(other.ptr) + { + // in the copy constructor + // no memory is allocated + // so just increment the reference count + ++ref[ptr]; + + cout << "CTOR COPY " << *ptr << " - Ref Count = " << ref[ptr] << endl; + } + + // Destructor + ~A() + { + // it always ends with this destructor + // decrement the reference count + --ref[ptr]; + cout << "DTOR " << *ptr << " - Ref Count = " << ref[ptr] ; + + // when the reference count is 0 then + // delete the memory + if(!ref[ptr]) + { + cout << " DELETE "; + delete ptr ; + } + + cout << endl; + } + +private: + static map<int *, int> ref; + int *ptr; +}; +map<int *, int> A::ref; + +int main ( void ) +{ + A a(9); + vector<A>vec; + vec.push_back(a); + vec.push_back(A(1)); + vec.push_back(A(2)); + vec.push_back(A(3)); + vec.push_back(A(4)); + vec.push_back(A(5)); + vec.push_back(A(6)); + vec.push_back(A(7)); + return 0; +} diff --git a/c++/testvector2 b/c++/testvector2 new file mode 100755 index 0000000000000000000000000000000000000000..232bbaa6b6801b58d43469f63cd3bbb17ed16351 Binary files /dev/null and b/c++/testvector2 differ diff --git a/c++/testvector2.cpp b/c++/testvector2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d4e5c0424c1814e1e56451f015045296a643673 --- /dev/null +++ b/c++/testvector2.cpp @@ -0,0 +1,68 @@ +#include <iostream> +#include <vector> + +using namespace std; + +class A { +public: + // constructor + A(int i) + { + // it always begins with this constructor + // set the reference count to 1 + ptr = new int(i); + ref = new int(1); + + cout << "CTOR NEW " << *ptr << " - Ref Count = " << *ref << endl; + } + + // copy constructor + A(const A & other ) : ref(other.ref), ptr(other.ptr) + { + // in the copy constructor + // no memory is allocated + // so just increment the reference count + ++*ref; + + cout << "CTOR COPY " << *ptr << " - Ref Count = " << *ref << endl; + } + + // Destructor + ~A() + { + // it always ends with this destructor + // decrement the reference count + --*ref; + cout << "DTOR " << *ptr << " - Ref Count = " << *ref; + + // when the reference count is 0 then + // delete the memory + if(!*ref) + { + cout << " DELETE "; + delete ptr ; + delete ref ; + } + + cout << endl; + } + +private: + int *ref; + int *ptr; +}; + +int main ( void ) +{ + A a(9); + vector<A>vec; + vec.push_back(a); + vec.push_back(A(1)); + vec.push_back(A(2)); + vec.push_back(A(3)); + vec.push_back(A(4)); + vec.push_back(A(5)); + vec.push_back(A(6)); + vec.push_back(A(7)); + return 0; +}