diff --git a/cpp/.idea/.name b/cpp/.idea/.name index d366d2e..70a1f63 100644 --- a/cpp/.idea/.name +++ b/cpp/.idea/.name @@ -1 +1 @@ -classes \ No newline at end of file +containers \ No newline at end of file diff --git a/cpp/.idea/misc.xml b/cpp/.idea/misc.xml index 7a865bb..cb02c5a 100644 --- a/cpp/.idea/misc.xml +++ b/cpp/.idea/misc.xml @@ -3,7 +3,7 @@ - + \ No newline at end of file diff --git a/cpp/classes/ClassA.cpp b/cpp/classes/ClassA.cpp index 1a156d4..09ac5be 100644 --- a/cpp/classes/ClassA.cpp +++ b/cpp/classes/ClassA.cpp @@ -2,6 +2,36 @@ #include +ClassA::ClassA() : IComparable(), _number(0), _pDecimal(new float(0.0f)) +{ + std::cout << "ClassA created" << std::endl; + + // _pDecimal = new float(0.0f); +} + +ClassA::ClassA(const ClassA &other) : + IComparable(other), + _number(other._number), + _pDecimal(new float(*other._pDecimal)) +{ + std::cout << "ClassA copy created" << std::endl; + + // shallow copy + // _number = other._number; + // _pDecimal = other._pDecimal; + + // deep copy + // _number = other._number; + // _pDecimal = new float(*other._pDecimal); +} + +ClassA::~ClassA() { + std::cout << "ClassA destroyed" << std::endl; + + delete _pDecimal; + _pDecimal = nullptr; +} + void ClassA::Print() { std::cout << "ClassA" << std::endl; diff --git a/cpp/classes/ClassA.h b/cpp/classes/ClassA.h index f80ce9b..91683f4 100644 --- a/cpp/classes/ClassA.h +++ b/cpp/classes/ClassA.h @@ -8,15 +8,23 @@ class ClassA : public IComparable { public: // public + ClassA(); // default constructor + ClassA(const ClassA& other); // copy constructor + ~ClassA() override; // destructor + void Print(); int CompareTo(IComparable* other) override; + const float& GetDecimal() const { return *_pDecimal; } + void SetDecimal(float decimal) { *_pDecimal = decimal; } + protected: // protected private: // private int _number = 0; + float* _pDecimal = nullptr; }; diff --git a/cpp/classes/IComparable.h b/cpp/classes/IComparable.h index 904dd2f..48806d1 100644 --- a/cpp/classes/IComparable.h +++ b/cpp/classes/IComparable.h @@ -1,8 +1,13 @@ #ifndef CLASSES_ICOMPARABLE_H #define CLASSES_ICOMPARABLE_H +#include class IComparable { public: + // only for presentation purpose + IComparable() { std::cout << "IComparable created" << std::endl; } + virtual ~IComparable() { std::cout << "IComparable destroyed" << std::endl; } + virtual int CompareTo(IComparable* other) = 0; }; diff --git a/cpp/classes/main.cpp b/cpp/classes/main.cpp index 8033109..ead7640 100644 --- a/cpp/classes/main.cpp +++ b/cpp/classes/main.cpp @@ -13,5 +13,18 @@ int main() { a.CompareTo(pA); + IComparable* pComparable = nullptr; + pComparable = new ClassA(); + pComparable->CompareTo(&a); + // pA = dynamic_cast(pComparable); + // if (pA != nullptr) { + // pA->Print(); + // } + delete pComparable; + pComparable = nullptr; + + ClassA a2 = a; + + return 0; } \ No newline at end of file diff --git a/cpp/containers/CMakeLists.txt b/cpp/containers/CMakeLists.txt new file mode 100644 index 0000000..0b71bc0 --- /dev/null +++ b/cpp/containers/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 4.1) +project(containers) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_FLAGS -pedantic-errors) + +add_executable(containers + main.cpp + functions.cpp) diff --git a/cpp/containers/functions.cpp b/cpp/containers/functions.cpp new file mode 100644 index 0000000..35d70f9 --- /dev/null +++ b/cpp/containers/functions.cpp @@ -0,0 +1,16 @@ +#include "functions.h" + +#include + +void printArrayAtPosition(int* array, int position) { + if (array == nullptr) { + return; + } + + if (position < 0) { + return; + } + + std::cout << "size of array: " << sizeof(array) << std::endl; + std::cout << array[position] << std::endl; +} diff --git a/cpp/containers/functions.h b/cpp/containers/functions.h new file mode 100644 index 0000000..d3c1340 --- /dev/null +++ b/cpp/containers/functions.h @@ -0,0 +1,10 @@ +// +// Created by David Hackbarth on 20.02.26. +// + +#ifndef CONTAINERS_FUNCTIONS_H +#define CONTAINERS_FUNCTIONS_H + +void printArrayAtPosition(int* array, int position); + +#endif //CONTAINERS_FUNCTIONS_H diff --git a/cpp/containers/main.cpp b/cpp/containers/main.cpp new file mode 100644 index 0000000..bbdfb98 --- /dev/null +++ b/cpp/containers/main.cpp @@ -0,0 +1,39 @@ +#include +#include + +#include "functions.h" + +int main(int argc, const char* argv[]) { + // C array + int numbers[5] = {1, 2, 3, 4, 5}; // stack array + const int count = 10; + int numbers2[count]; + + int count2 = 10; + int* numbers3 = new int[count2]; // heap array + delete[] numbers3; + numbers3 = nullptr; + + // numbers[10] = 100; + std::cout << "size of numbers: " << sizeof(numbers) << std::endl; + + printArrayAtPosition(numbers, 19); + + // vector + std::vector v = {1, 2, 3, 4, 5}; + + std::cout << "size of v: " << v.size() << "/" << v.capacity() << std::endl; + + v.push_back(6); + + std::cout << "size of v: " << v.size() << "/" << v.capacity() << std::endl; + + // v[20] = 100; + printArrayAtPosition(v.data(), 2); + + v.pop_back(); + + std::cout << "size of v: " << v.size() << "/" << v.capacity() << std::endl; + + return 0; +}