CS201- Introduction to Programming- Lecture 28

33
Introduction of Programmin Introduction of Programmin Lecture 28 Lecture 28

description

Virtual University Course CS201- Introduction to Programming Lecture No 28 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Transcript of CS201- Introduction to Programming- Lecture 28

Page 1: CS201- Introduction to Programming- Lecture 28

Introduction of ProgrammingIntroduction of Programming

Lecture 28Lecture 28

Page 2: CS201- Introduction to Programming- Lecture 28

Today’s Today’s Lecture Lecture

How memory allocation is done How memory allocation is done inin

C++C++ How is it different from C style How is it different from C style Advantages of memory allocation Advantages of memory allocation

inin C++C++ Uses of memory allocationUses of memory allocation

– ClassesClasses– ObjectsObjects

Page 3: CS201- Introduction to Programming- Lecture 28

Pointers to a Pointers to a data structuredata structure

Page 4: CS201- Introduction to Programming- Lecture 28

Pointers to Pointers to a class a class

Page 5: CS201- Introduction to Programming- Lecture 28

->->

Page 6: CS201- Introduction to Programming- Lecture 28

Memory AllocationMemory Allocation

Page 7: CS201- Introduction to Programming- Lecture 28

malloc ( ) ;malloc ( ) ;

calloc ( ) ;calloc ( ) ;

realloc ( ) ;realloc ( ) ;

Page 8: CS201- Introduction to Programming- Lecture 28

malloc ( 10 * ( sizeof ( int ) ) ) ; malloc ( 10 * ( sizeof ( int ) ) ) ;

Page 9: CS201- Introduction to Programming- Lecture 28

free ( )free ( )

Page 10: CS201- Introduction to Programming- Lecture 28

newnew

Page 11: CS201- Introduction to Programming- Lecture 28

new int ;new int ;

Page 12: CS201- Introduction to Programming- Lecture 28

int * iptr ;int * iptr ;

iptr = new int ;iptr = new int ;

Example

Page 13: CS201- Introduction to Programming- Lecture 28

new char ;new char ;new double ;new double ;

Example

Page 14: CS201- Introduction to Programming- Lecture 28

deletedelete

Page 15: CS201- Introduction to Programming- Lecture 28

int * iptr ;int * iptr ;

iptr = new int ;iptr = new int ;

delete iptr ;delete iptr ;

Example

Page 16: CS201- Introduction to Programming- Lecture 28

int * iptr ;int * iptr ;

iptr = new int [ 10 ] ;iptr = new int [ 10 ] ;

Example

Page 17: CS201- Introduction to Programming- Lecture 28

new data_type [ Number_of_locations ] ;new data_type [ Number_of_locations ] ;

new double [ 10 ] ;new double [ 10 ] ;

Example

Page 18: CS201- Introduction to Programming- Lecture 28

int *iptr ;int *iptr ;iptr = new int [ 10 ] ;iptr = new int [ 10 ] ;delete iptr ;delete iptr ;

Example Example

Page 19: CS201- Introduction to Programming- Lecture 28

Date *dptr ; Date *dptr ;

dptr is a pointer to an object of type dptr is a pointer to an object of type datedate

Example Example

Page 20: CS201- Introduction to Programming- Lecture 28

dptr = new Date ;dptr = new Date ;

Page 21: CS201- Introduction to Programming- Lecture 28

main ( )main ( ){{

Date mydate ;Date mydate ;cout<< sizeof cout<< sizeof

( mydate ) ;( mydate ) ;}}

Example Example

Page 22: CS201- Introduction to Programming- Lecture 28

int *iptr ;int *iptr ;

iptr = new int [ 10 ] ;iptr = new int [ 10 ] ;

Example Example

Page 23: CS201- Introduction to Programming- Lecture 28

Date *dptr ;Date *dptr ;

dptr = new Date [ 10 ] ;dptr = new Date [ 10 ] ;

Example Example

Page 24: CS201- Introduction to Programming- Lecture 28

Date date1 , Date date1 , *dptr *dptr ;;date1.setDate ( ) ;date1.setDate ( ) ;

dptr = new Date ;dptr = new Date ;dptr ->setDate ( );dptr ->setDate ( );

dptr.setDate ( ) ;dptr.setDate ( ) ; WrongWrong

Example Example

Page 25: CS201- Introduction to Programming- Lecture 28

ProtectedProtected

Page 26: CS201- Introduction to Programming- Lecture 28

DestructorDestructor

Page 27: CS201- Introduction to Programming- Lecture 28

Allocate enough space for Allocate enough space for

the new datathe new data Populate that spacePopulate that space Delete the previous spaceDelete the previous space Point the new space to thePoint the new space to the

pointer pointing to the pointer pointing to the

original dataoriginal data

Page 28: CS201- Introduction to Programming- Lecture 28

char *name =new char [ string_length ]char *name =new char [ string_length ]

Page 29: CS201- Introduction to Programming- Lecture 28

delete [ ] namedelete [ ] name

Page 30: CS201- Introduction to Programming- Lecture 28

delete [ ] pointer_namedelete [ ] pointer_name

Page 31: CS201- Introduction to Programming- Lecture 28

main ( )main ( )

{{

Date mydate ( “01-12-2002” ) ;Date mydate ( “01-12-2002” ) ;

mydate.display ( ) ;mydate.display ( ) ;

}}

Example Example

Page 32: CS201- Introduction to Programming- Lecture 28

MessagesMessages

Page 33: CS201- Introduction to Programming- Lecture 28

MethodMethod