cs201- introduction to programming- lecture 39
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No39 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
Introduction to Introduction to ProgrammingProgramming
Lecture 39Lecture 39
Copy Copy ConstructoConstructo
rr
ReviewReview PointersPointers ReferencesReferences Memory AllocationMemory Allocation
PointersPointers
A pointer is a special type ofA pointer is a special type ofvariable that contain a memoryvariable that contain a memoryaddressaddress
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
ReferenceReferencess
&&
consconstt
Dynamic Dynamic Memory Memory
AllocationAllocation
Native OperatorNative Operator
newnew deletedelete
int *p ;int *p ;
p = new int ;p = new int ;
delete p ;delete p ;
Dynamic Memory Allocation
int *p ;int *p ;
p = new int [ 10 ] ;p = new int [ 10 ] ;
delete [ ] p ;delete [ ] p ;
Dynamic Memory AllocationDynamic Memory Allocation
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
delete [ ] delete [ ] m ;m ;
AssignmenAssignmentt
int i = 0 ;int i = 0 ;
//Initialization//Initialization
int i ;int i ;
i = 0 ; //Assignmenti = 0 ; //Assignment
Matrix m1 , m2 ;Matrix m1 , m2 ;…………
m2 = m1 ;m2 = m1 ;//Assignment Statement//Assignment Statement
Member to Member to Member Member
AssignmentAssignment
m2 = m1m2 = m1
0xefffdad0mx
Pointing to the same region in memoryint *m of m1
0xefffdad0
int *m of m2
0xefffdad0mx
Pointing to the same region in memoryint *m of m1
0xefffdad0
int *m of m2
Copy Copy ConstructoConstructo
rr
Call by Call by valuevalue
Deep CopyDeep CopyShallow Shallow CopyCopy
Matrix ( Matrix & ) Matrix ( Matrix & ) ; ;
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.
} ;} ;
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 ( ) ;
int i ; int i ; i = 10 ;i = 10 ;i = i ;i = i ;
Matrix m2 ( m1 ) ;Matrix m2 ( m1 ) ;
Matrix m2 = m1 ;Matrix m2 = m1 ;
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
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