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:
Generated
+1
-1
@@ -1 +1 @@
|
|||||||
classes
|
containers
|
||||||
Generated
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
<component name="CMakePythonSetting">
|
<component name="CMakePythonSetting">
|
||||||
<option name="pythonIntegrationState" value="YES" />
|
<option name="pythonIntegrationState" value="YES" />
|
||||||
</component>
|
</component>
|
||||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$/classes">
|
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$/containers">
|
||||||
<contentRoot DIR="$PROJECT_DIR$" />
|
<contentRoot DIR="$PROJECT_DIR$" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -2,6 +2,36 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#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()
|
void ClassA::Print()
|
||||||
{
|
{
|
||||||
std::cout << "ClassA" << std::endl;
|
std::cout << "ClassA" << std::endl;
|
||||||
|
|||||||
@@ -8,15 +8,23 @@ class ClassA : public IComparable {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// public
|
// public
|
||||||
|
ClassA(); // default constructor
|
||||||
|
ClassA(const ClassA& other); // copy constructor
|
||||||
|
~ClassA() override; // destructor
|
||||||
|
|
||||||
void Print();
|
void Print();
|
||||||
int CompareTo(IComparable* other) override;
|
int CompareTo(IComparable* other) override;
|
||||||
|
|
||||||
|
const float& GetDecimal() const { return *_pDecimal; }
|
||||||
|
void SetDecimal(float decimal) { *_pDecimal = decimal; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// protected
|
// protected
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// private
|
// private
|
||||||
int _number = 0;
|
int _number = 0;
|
||||||
|
float* _pDecimal = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
#ifndef CLASSES_ICOMPARABLE_H
|
#ifndef CLASSES_ICOMPARABLE_H
|
||||||
#define CLASSES_ICOMPARABLE_H
|
#define CLASSES_ICOMPARABLE_H
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
class IComparable {
|
class IComparable {
|
||||||
public:
|
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;
|
virtual int CompareTo(IComparable* other) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -13,5 +13,18 @@ int main() {
|
|||||||
|
|
||||||
a.CompareTo(pA);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -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)
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#include "functions.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -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
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#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<int> 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user