This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 50 Object Oriented Programming C++ Classes R.B. Lenin Winter 2006 DA-IICT Dynamic Memory Allocation with Operators new and delete – new and delete – Better dynamic memory allocation than C’s malloc and free – new - automatically creates object of proper size, calls constructor, returns pointer of the correct type – delete- destroys object and frees space – Example: – TypeName *typeNamePtr; – Creates pointer to a TypeName object – typeNamePtr = new TypeName; – new creates TypeName object, returns pointer (which typeNamePtr is set equal to) – delete typeNamePtr; – Calls destructor for TypeName object and frees memory 51 Object Oriented Programming C++ Classes R.B. Lenin Winter 2006 DA-IICT Dynamic Memory Allocation with Operators new and delete (II) • Initializing objects double *thingPtr = new double( 3.14159 ); – Initializes object of type double to 3.14159 int *arrayPtr = new int[ 10 ]; – Create ten element int array, assign to arrayPtr ....
View Full Document
This note was uploaded on 09/15/2010 for the course CSE IT325 taught by Professor Maniklaldas during the Spring '09 term at Punjab Engineering College.
- Spring '09