-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_destructorTest.cpp
More file actions
43 lines (34 loc) · 1.03 KB
/
class_destructorTest.cpp
File metadata and controls
43 lines (34 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cassert>
#include <cstddef>
class IntArray
{
private:
int* m_array{};
int m_length{};
public:
IntArray(const IntArray&) = delete;
IntArray& operator=(const IntArray&) = delete;
IntArray(int length) // constructor
{
assert(length > 0);
m_array = new int[static_cast<std::size_t>(length)]{};
m_length = length;
}
~IntArray() // destructor
{
// Dynamically delete the array we allocated earlier
delete[] m_array;
}
void setValue(int index, int value) { m_array[index] = value; }
int getValue(int index) { return m_array[index]; }
int getLength() { return m_length; }
};
int main()
{
IntArray ar ( 10 ); // allocate 10 integers. As we initialize the array with its length so we use parentheses here
for (int count{ 0 }; count < ar.getLength(); ++count)
ar.setValue(count, count+1);
std::cout << "The value of element 5 is: " << ar.getValue(5) << '\n';
return 0;
} // ar is destroyed here, so the ~IntArray() destructor function is called here