cs201- introduction to programming- lecture 39

31
Introduction to Introduction to Programming Programming Lecture 39 Lecture 39

Upload: bilal-ahmed

Post on 18-Dec-2014

21 views

Category:

Education


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: CS201- Introduction to Programming- Lecture 39

Introduction to Introduction to ProgrammingProgramming

Lecture 39Lecture 39

Page 2: CS201- Introduction to Programming- Lecture 39

Copy Copy ConstructoConstructo

rr

Page 3: CS201- Introduction to Programming- Lecture 39

ReviewReview PointersPointers ReferencesReferences Memory AllocationMemory Allocation

Page 4: CS201- Introduction to Programming- Lecture 39

PointersPointers

A pointer is a special type ofA pointer is a special type ofvariable that contain a memoryvariable that contain a memoryaddressaddress

Page 5: CS201- Introduction to Programming- Lecture 39

Void PointerVoid Pointer Pointer to an IntegerPointer to an Integer Pointer to a CharacterPointer to a Character Pointer to a FloatPointer to a Float Pointer to ObjectsPointer to Objects

Page 6: CS201- Introduction to Programming- Lecture 39

ReferenceReferencess

Page 7: CS201- Introduction to Programming- Lecture 39

&&

Page 8: CS201- Introduction to Programming- Lecture 39

consconstt

Page 9: CS201- Introduction to Programming- Lecture 39

Dynamic Dynamic Memory Memory

AllocationAllocation

Page 10: CS201- Introduction to Programming- Lecture 39

Native OperatorNative Operator

newnew deletedelete

Page 11: CS201- Introduction to Programming- Lecture 39

int *p ;int *p ;

p = new int ;p = new int ;

delete p ;delete p ;

Dynamic Memory Allocation

Page 12: CS201- Introduction to Programming- Lecture 39

int *p ;int *p ;

p = new int [ 10 ] ;p = new int [ 10 ] ;

delete [ ] p ;delete [ ] p ;

Dynamic Memory AllocationDynamic Memory Allocation

Page 13: CS201- Introduction to Programming- Lecture 39

class Matrixclass Matrix{{

private :private :int * m ;int * m ;int row , col ;int row , col ;

public :public : Matrix ( int rows , int cols ) Matrix ( int rows , int cols ) {{

m = new int [ rows * cols ] ;m = new int [ rows * cols ] ; }}

} ;} ;

ExampleExample

Page 14: CS201- Introduction to Programming- Lecture 39

delete [ ] delete [ ] m ;m ;

Page 15: CS201- Introduction to Programming- Lecture 39

AssignmenAssignmentt

Page 16: CS201- Introduction to Programming- Lecture 39

int i = 0 ;int i = 0 ;

//Initialization//Initialization

int i ;int i ;

i = 0 ; //Assignmenti = 0 ; //Assignment

Page 17: CS201- Introduction to Programming- Lecture 39

Matrix m1 , m2 ;Matrix m1 , m2 ;…………

m2 = m1 ;m2 = m1 ;//Assignment Statement//Assignment Statement

Page 18: CS201- Introduction to Programming- Lecture 39

Member to Member to Member Member

AssignmentAssignment

Page 19: CS201- Introduction to Programming- Lecture 39

m2 = m1m2 = m1

Page 20: CS201- Introduction to Programming- Lecture 39

0xefffdad0mx

Pointing to the same region in memoryint *m of m1

0xefffdad0

int *m of m2

Page 21: CS201- Introduction to Programming- Lecture 39

0xefffdad0mx

Pointing to the same region in memoryint *m of m1

0xefffdad0

int *m of m2

Page 22: CS201- Introduction to Programming- Lecture 39

Copy Copy ConstructoConstructo

rr

Page 23: CS201- Introduction to Programming- Lecture 39

Call by Call by valuevalue

Page 24: CS201- Introduction to Programming- Lecture 39

Deep CopyDeep CopyShallow Shallow CopyCopy

Page 25: CS201- Introduction to Programming- Lecture 39

Matrix ( Matrix & ) Matrix ( Matrix & ) ; ;

Page 26: CS201- Introduction to Programming- Lecture 39

Example Example class Stringclass String

{{

char * c ;char * c ;

public :public :

void void copy ( char * s ) ;copy ( char * s ) ;

String ( char * data ) ;String ( char * data ) ;

void print ( ) ;void print ( ) ;

// etc.// etc.

} ;} ;

Page 27: CS201- Introduction to Programming- Lecture 39

Example Example String s1 ( “test1” ) ;String s1 ( “test1” ) ;

String s2 = s1 ;String s2 = s1 ;

s1.copy ( “this is a test” ) ; s1.copy ( “this is a test” ) ;

s2.print ( ) ;s2.print ( ) ;

Page 28: CS201- Introduction to Programming- Lecture 39

int i ; int i ; i = 10 ;i = 10 ;i = i ;i = i ;

Page 29: CS201- Introduction to Programming- Lecture 39

Matrix m2 ( m1 ) ;Matrix m2 ( m1 ) ;

Matrix m2 = m1 ;Matrix m2 = m1 ;

Page 30: CS201- Introduction to Programming- Lecture 39

Rules Rules For dynamic memory allocationFor dynamic memory allocation

1.1. Define copy constructorDefine copy constructor

2.2. Write assignment operatorWrite assignment operator

3.3. Provide destructor Provide destructor

Page 31: CS201- Introduction to Programming- Lecture 39

What have we covered What have we covered today today

Review of pointersReview of pointers– Dynamic memory allocationDynamic memory allocation

newnew DeleteDelete

For memory allocation in classes we must provideFor memory allocation in classes we must provide– ConstructorConstructor– Copy constructorCopy constructor– Assignment operatorAssignment operator– destructordestructor