Add containers module with initial implementation and integrate into CMake build system.

This commit is contained in:
2026-02-21 10:38:58 +01:00
parent 90fbcc38dc
commit 939e98d5b2
16 changed files with 272 additions and 12 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 4.1)
project(pointer)
project(classes)
set(CMAKE_CXX_STANDARD 20)
add_executable(pointer main.cpp ClassA.cpp)
add_executable(classes main.cpp ClassA.cpp)
+30
View File
@@ -2,6 +2,36 @@
#include <iostream>
ClassA::ClassA() : IComparable(), _number(0), _pDecimal(new float(0.0f)) {
std::cout << "ClassA()" << std::endl;
// _pDecimal = new float(0.0f);
}
ClassA::ClassA(const ClassA &other) :
IComparable(),
_number(other._number),
_pDecimal(new float(*other._pDecimal)) {
std::cout << "ClassA(const ClassA &other)" << std::endl;
// // for presentation only
// // shallow copy
// _number = other._number;
// *_pDecimal = *other._pDecimal;
//
// // deep copy
// _number = other._number;
// _pDecimal = new float(*other._pDecimal);
}
ClassA::~ClassA() {
std::cout << "~ClassA()" << std::endl;
delete _pDecimal;
_pDecimal = nullptr;
}
void ClassA::Print() {
std::cout << "ClassA" << std::endl;
}
+8 -1
View File
@@ -8,16 +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;
};
#endif //POINTER_CLASSA_H
+5
View File
@@ -1,8 +1,13 @@
#ifndef POINTER_ICOMPARABLE_H
#define POINTER_ICOMPARABLE_H
#include <iostream>
class IComparable {
public:
// only for presentation purposes
IComparable() { std::cout << "IComparable()" << std::endl; }
virtual ~IComparable() { std::cout << "~IComparable()" << std::endl; }
virtual int CompareTo(IComparable* other) = 0;
};
+20
View File
@@ -1,4 +1,5 @@
#include "ClassA.h"
#include <list>
int main() {
ClassA a{};
@@ -20,5 +21,24 @@ int main() {
delete pComparable;
pComparable = nullptr;
// copy object
ClassA a2 = a;
std::list<ClassA> list;
std::list<ClassA>::iterator it;
for (it = list.begin(); it != list.end(); ++it) {
it->Print();
}
std::list<IComparable*> list2;
std::list<IComparable*>::iterator it2;
for (it2 = list2.begin(); it2 != list2.end(); ++it2) {
(*it2)->CompareTo(&a);
}
IComparable* pComparables[10];
return 0;
}