Add containers project with CMake setup and array-related functionality
- Implement `printArrayAtPosition` in `functions.cpp` with input validation. - Created `main.cpp` to demonstrate array operations using C arrays and `std::vector`. - Updated `ClassA` and `IComparable` with constructors, destructors, and deep copy logic. - Adjusted IntelliJ project configuration for new containers module.
This commit is contained in:
@@ -2,6 +2,36 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
#ifndef CLASSES_ICOMPARABLE_H
|
||||
#define CLASSES_ICOMPARABLE_H
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
@@ -13,5 +13,18 @@ int main() {
|
||||
|
||||
a.CompareTo(pA);
|
||||
|
||||
IComparable* pComparable = nullptr;
|
||||
pComparable = new ClassA();
|
||||
pComparable->CompareTo(&a);
|
||||
// pA = dynamic_cast<ClassA *>(pComparable);
|
||||
// if (pA != nullptr) {
|
||||
// pA->Print();
|
||||
// }
|
||||
delete pComparable;
|
||||
pComparable = nullptr;
|
||||
|
||||
ClassA a2 = a;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user