Add containers module with initial implementation and integrate into CMake build system.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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,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
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user