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.

220Part III: Introduction to ClassesThe function main() invokes a function fn() that defines the object tp —this is to allow you to watch the variable go out of scope when control exitsthe function. fn() also allocates heap memory that it returns to main()where the memory is returned to the heap.If you execute this program, it generates the following output:Creating TutorPair object in function fn()constructing studentconstructing teacherconstructing courseconstructing tutorpairAllocating TutorPair off the heapconstructing studentconstructing teacherconstructing courseconstructing tutorpairReturning from fn()destructing tutorpairdestructing teacherdestructing coursedestructing studentReturn heap object to the heapdestructing tutorpairdestructing teacherdestructing coursedestructing studentPress any key to continue . . .Each constructor is called in turn as the TutorPair object is built up, startingfrom the smallest data member and working its way up to the TutorPair::TutorPair() constructor function.Two TutorPair objects are created. The first, tp, is defined locally to thefunction fn(), the second, pTP, is allocated off the heap. tp goes out of scopeand is destructed when control passes out of the function. The heap memorywhose address is returned from fn() is not destructed until main() deletes it.The sequence of destructors invoked when an object is destructed is invokedin the reverse order in which the constructors were called.

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

Saved successfully!

Ooh no, something went wrong!