add smart pointers

add polymorphism
add destructor & copy constructor
add pointer-pointer
This commit is contained in:
2025-09-22 18:22:03 +02:00
parent d8f6014fab
commit 3b35b973c9
13 changed files with 208 additions and 7 deletions
+37 -1
View File
@@ -1,6 +1,7 @@
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include "grid.h"
@@ -44,7 +45,18 @@ int main()
printArray(numbers);
printArray(numbers, size(numbers));
// "dynamic" (...) arrays
// "dynamic" (heap) arrays
int count2 = 10;
int* pNumbers = new int[count2];
pNumbers[0] = 10;
int* pNumbers2 = pNumbers; // pointer looks onto first element
for (size_t i = 0; i < count2; i++) // size can't be used for heap arrays
{
cout << pNumbers2 << ": " << *pNumbers2 << endl;
pNumbers2++;
}
delete[] pNumbers; // CAUTION - heap arrays have to be deleted with delete[]
pNumbers = pNumbers2 = nullptr;
// stl collections
vector<int> numbers5;
@@ -62,6 +74,30 @@ int main()
numbersList.push_back(10);
numbersList.push_front(0);
//numbersList[0] = 5; // random access is not available for lists
list<int>::iterator it = numbersList.begin();
for (; it != numbersList.end(); it++)
{
cout << *it << " ";
}
cout << endl;
numbersList.insert(--it, 20);
for (it = numbersList.begin(); it != numbersList.end(); it++)
{
cout << *it << " ";
}
cout << endl;
for (int n : numbersList)
{
}
for_each(numbers6.begin(), numbers6.end(), [](const int& n) {
cout << n << " ";
});
cout << endl;
return 0;
}