11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

218Part III: Introduction to ClassesThe destructor is invoked automatically when an object is destroyed, or inC++ parlance, when an object is destructed. That sounds sort of circular (“thedestructor is invoked when an object is destructed”), so I’ve avoided the termuntil now. For non-heap memory, you can also say, “when the object goes outof scope.” A local object goes out of scope when the function returns. A globalor static object goes out of scope when the program terminates.But what about heap memory? A pointer may go out of scope, but heapmemory doesn’t. By definition, it’s memory that is not part of a given function.An object that has been allocated off the heap is destructed when it’s returnedto the heap using the delete command. This is demonstrated in the followingDestructMembers program://// DestructMembers - this program both constructs and// destructs a set of data members//#include #include #include using namespace std;class Course{public:Course() { cout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!