#include <iostream>
class MyClass
{
public:
MyClass() {
itsAge = 1;
itsWeight=5;
}
~MyClass();
int GetAge() const { return itsAge; }
int GetWeight() const { return itsWeight; }
void SetAge(int age) { itsAge = age; }
private:
int itsAge;
int itsWeight;
};
MyClass :: ~MyClass()
{
std::cout << "Destructor called!\n";
}
int main()
{
MyClass * objectArrayPointer = new MyClass[50];
int i;
MyClass * currentPointer;
for (i = 0; i < 50; i++)
{
currentPointer = new MyClass;
currentPointer->SetAge(2*i +1);
objectArrayPointer[i] = *currentPointer;
delete currentPointer;
}
for (i = 0; i < 50; i++)
std::cout << "#" << i+1 << ": " << objectArrayPointer[i].GetAge() << s
td::endl;
delete [] objectArrayPointer;
return 0;
}
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
#1: 1
#2: 3
#3: 5
#4: 7
#5: 9
#6: 11
#7: 13
#8: 15
#9: 17
#10: 19
#11: 21
#12: 23
#13: 25
#14: 27
#15: 29
#16: 31
#17: 33
#18: 35
#19: 37
#20: 39
#21: 41
#22: 43
#23: 45
#24: 47
#25: 49
#26: 51
#27: 53
#28: 55
#29: 57
#30: 59
#31: 61
#32: 63
#33: 65
#34: 67
#35: 69
#36: 71
#37: 73
#38: 75
#39: 77
#40: 79
#41: 81
#42: 83
#43: 85
#44: 87
#45: 89
#46: 91
#47: 93
#48: 95
#49: 97
#50: 99
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!