0% found this document useful (0 votes)
4 views

Pointers Part 3

The document discusses copy constructors in object-oriented programming. It states that a copy constructor is a special type of constructor used to copy one object (the source) into another object (the target) of the same class. When no copy constructor is defined, the compiler implicitly generates a default copy constructor that copies all data members. The document provides examples of how copy constructors are used when initializing one object with another, passing objects by value, and returning objects from functions.

Uploaded by

Inshal Yousaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Pointers Part 3

The document discusses copy constructors in object-oriented programming. It states that a copy constructor is a special type of constructor used to copy one object (the source) into another object (the target) of the same class. When no copy constructor is defined, the compiler implicitly generates a default copy constructor that copies all data members. The document provides examples of how copy constructors are used when initializing one object with another, passing objects by value, and returning objects from functions.

Uploaded by

Inshal Yousaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Object Oriented Programming

Pointers continued . . .

Mr. Usman Wajid

[email protected]

Usman Wajid Object Oriented Programming Pointers continued . . . 1 / 11


Copy Constructor

Copy Constructor
It is special type of constructor intended to copy one object (source) into another (target)
of the same class

Usman Wajid Object Oriented Programming Pointers continued . . . 2 / 11


Copy Constructor

Copy Constructor
It is special type of constructor intended to copy one object (source) into another (target)
of the same class

• When no copy constructor is defined, then a default copy constructor is implicitly


invoked by the compiler. All data and function members will be copied.

Usman Wajid Object Oriented Programming Pointers continued . . . 2 / 11


Copy Constructor

Copy Constructor
It is special type of constructor intended to copy one object (source) into another (target)
of the same class

• When no copy constructor is defined, then a default copy constructor is implicitly


invoked by the compiler. All data and function members will be copied.
• Basic Syntax 1:
< class - name > < target - object >( < source - object >) ;

Usman Wajid Object Oriented Programming Pointers continued . . . 2 / 11


Copy Constructor

Copy Constructor
It is special type of constructor intended to copy one object (source) into another (target)
of the same class

• When no copy constructor is defined, then a default copy constructor is implicitly


invoked by the compiler. All data and function members will be copied.
• Basic Syntax 1:
< class - name > < target - object >( < source - object >) ;

• Basic Syntax 2:
< class - name > < target - object > = < source - object >;

Usman Wajid Object Oriented Programming Pointers continued . . . 2 / 11


Copy Constructor Example 1
class Student {
private :
char section ;
public :
void setSection ( char section ) {
this - > section = section ;
}
char getSection () {
return section ;
}
Output
};
int main () {
Student ali ;
ali . setSection ( ’A ’) ;
cout < < " ali sec : " << ali . getSection () << endl ;

Student mahad ( ali ) ;


cout < < " mahad sec : " << mahad . getSection () << endl ;

Student zain = ali ;


cout < < " zain sec : " << zain . getSection () << endl ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 3 / 11
Copy Constructor Example 1
class Student {
private :
char section ;
public :
void setSection ( char section ) {
this - > section = section ;
}
char getSection () {
return section ;
}
Output
};
int main () {
Student ali ;
ali . setSection ( ’A ’) ;
cout < < " ali sec : " << ali . getSection () << endl ;

Student mahad ( ali ) ;


cout < < " mahad sec : " << mahad . getSection () << endl ;

Student zain = ali ;


cout < < " zain sec : " << zain . getSection () << endl ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 3 / 11
Copy Constructor Example 2
int main () {
Student ali ;
ali . setSec ( ’A ’) ;
cout < < " ali sec : " << ali . getSec () <<" \ n \ n " ;

class Student { Student mahad ( ali ) ;


private : cout < < " mahad sec : " << mahad . getSec () <<" \ n \ n " ;
char sec ;
public : Student zain = ali ;
Student () { cout < < " zain sec : " << zain . getSec () <<" \ n \ n " ;
cout < < " Default constructor \ n " ;
} Student akhtar ;
Student ( Student & obj ) { akhtar = ali ;
setSec ( obj . sec ) ; cout < < " akhtar sec : " << zain . getSec () << endl ;
cout < < " copy constructor \ n " ; }
}
void setSec ( char sec ) {
this - > sec = sec ;
}
char getSec () {
return sec ;
}
};

Usman Wajid Object Oriented Programming Pointers continued . . . 4 / 11


Copy Constructor Example 2
int main () {
Student ali ;
ali . setSec ( ’A ’) ;
cout < < " ali sec : " << ali . getSec () <<" \ n \ n " ;

class Student { Student mahad ( ali ) ;


private : cout < < " mahad sec : " << mahad . getSec () <<" \ n \ n " ;
char sec ;
public : Student zain = ali ;
Student () { cout < < " zain sec : " << zain . getSec () <<" \ n \ n " ;
cout < < " Default constructor \ n " ;
} Student akhtar ;
Student ( Student & obj ) { akhtar = ali ;
setSec ( obj . sec ) ; cout < < " akhtar sec : " << zain . getSec () << endl ;
cout < < " copy constructor \ n " ; }
}
void setSec ( char sec ) {
this - > sec = sec ;
}
char getSec () {
return sec ;
}
};

Usman Wajid Object Oriented Programming Pointers continued . . . 4 / 11


Copy Constructor continued . . .

• A default copy constructor is implicitly called in the following scenarios,

Usman Wajid Object Oriented Programming Pointers continued . . . 5 / 11


Copy Constructor continued . . .

• A default copy constructor is implicitly called in the following scenarios,

1 When an object is declared and initialized by using the value of another object

Usman Wajid Object Oriented Programming Pointers continued . . . 5 / 11


Copy Constructor continued . . .

• A default copy constructor is implicitly called in the following scenarios,

1 When an object is declared and initialized by using the value of another object

2 When an object is passed by value to a function

Usman Wajid Object Oriented Programming Pointers continued . . . 5 / 11


Copy Constructor continued . . .

• A default copy constructor is implicitly called in the following scenarios,

1 When an object is declared and initialized by using the value of another object

2 When an object is passed by value to a function

3 When the return value of a function is an object

Usman Wajid Object Oriented Programming Pointers continued . . . 5 / 11


Copy Constructor Example 3
int main () {
Student ali ;
ali . setSec ( ’A ’) ;
cout < < " ali sec : " << ali . getSec () <<" \ n \ n " ;
class Student {
private : Student imran ( ali ) ;
char sec ; cout < < " imran sec : " << imran . getSec () <<" \ n \ n " ;
public :
Student () { Student zain = fun ( ali ) ;
cout < < " Default constructor \ n " ; cout < < " zain sec : " << zain . getSec () <<" \ n \ n " ;
}
void setSec ( char sec ) { Student akhtar ;
this - > sec = sec ; akhtar = ali ;
} cout < < " akhtar sec : " << zain . getSec () << endl ;
char getSec () { }
return sec ;
}
};
Student fun ( Student obj ) {
Student mahad ( obj ) ;
cout < < " mahad sec : " << mahad . getSec () <<" \ n \ n " ;
return mahad ;
}

Usman Wajid Object Oriented Programming Pointers continued . . . 6 / 11


Copy Constructor Example 3
int main () {
Student ali ;
ali . setSec ( ’A ’) ;
cout < < " ali sec : " << ali . getSec () <<" \ n \ n " ;
class Student {
private : Student imran ( ali ) ;
char sec ; cout < < " imran sec : " << imran . getSec () <<" \ n \ n " ;
public :
Student () { Student zain = fun ( ali ) ;
cout < < " Default constructor \ n " ; cout < < " zain sec : " << zain . getSec () <<" \ n \ n " ;
}
void setSec ( char sec ) { Student akhtar ;
this - > sec = sec ; akhtar = ali ;
} cout < < " akhtar sec : " << zain . getSec () << endl ;
char getSec () { }
return sec ;
}
};
Student fun ( Student obj ) {
Student mahad ( obj ) ;
cout < < " mahad sec : " << mahad . getSec () <<" \ n \ n " ;
return mahad ;
}

Usman Wajid Object Oriented Programming Pointers continued . . . 6 / 11


Copy Constructor with pointer member

pointerDataClass objectThree ( objectOne ) ;

Usman Wajid Object Oriented Programming Pointers continued . . . 7 / 11


Copy Constructor with pointer member

pointerDataClass objectThree ( objectOne ) ;

• This initialization is called the default member-wise initialization

Usman Wajid Object Oriented Programming Pointers continued . . . 7 / 11


Copy Constructor with pointer member

pointerDataClass objectThree ( objectOne ) ;

• This initialization is called the default member-wise initialization


• It implicitly invokes the default copy constructor

Usman Wajid Object Oriented Programming Pointers continued . . . 7 / 11


Copy Constructor Example 4

class List {
private : int main () {
int * data ;
int size ; List objectOne (5) ;
public :
List ( int size ) { objectOne . fill () ;
this - > size = size ; cout < < " obj1 : " ;
data = new int [ size ];
}
objectOne . print () ;
void fill () {
for ( int i =0; i < size ; i ++) List objectThree ( objectOne ) ;
data [ i ] = i * i ;
}
objectThree . update (0 ,3) ;
void update ( int index , int val ) {
data [ index ] = val ;

} cout < < " \ n \ nobj1 : " ;


void print () { objectOne . print () ;
for ( int i =0; i < size ; i ++) }
cout < < data [ i ] < < " " ;
}
~ List () {
delete [] data ;
}
};

Usman Wajid Object Oriented Programming Pointers continued . . . 8 / 11


Copy Constructor Example 4

class List {
private : int main () {
int * data ;
int size ; List objectOne (5) ;
public :
List ( int size ) { objectOne . fill () ;
this - > size = size ; cout < < " obj1 : " ;
data = new int [ size ];
}
objectOne . print () ;
void fill () {
for ( int i =0; i < size ; i ++) List objectThree ( objectOne ) ;
data [ i ] = i * i ;
}
objectThree . update (0 ,3) ;
void update ( int index , int val ) {
data [ index ] = val ;

} cout < < " \ n \ nobj1 : " ;


void print () { objectOne . print () ;
for ( int i =0; i < size ; i ++) }
cout < < data [ i ] < < " " ;
}
~ List () {
delete [] data ;
}
};

Usman Wajid Object Oriented Programming Pointers continued . . . 8 / 11


Copy Constructor with pointer member continued . . .

void destroyList ( pointerDataClass paramObject ) ;

Usman Wajid Object Oriented Programming Pointers continued . . . 9 / 11


Copy Constructor with pointer member continued . . .

void destroyList ( pointerDataClass paramObject ) ;

• Default Initialization leads to shallow copying of data

Usman Wajid Object Oriented Programming Pointers continued . . . 9 / 11


Copy Constructor with pointer member continued . . .

void destroyList ( pointerDataClass paramObject ) ;

• Default Initialization leads to shallow copying of data


• Similar problem occurs when passing objects by value

Usman Wajid Object Oriented Programming Pointers continued . . . 9 / 11


Copy Constructor with pointer member continued . . .

void destroyList ( pointerDataClass paramObject ) ;

• Default Initialization leads to shallow copying of data


• Similar problem occurs when passing objects by value
• Solution: Write a user defined copy constructor to create a deep copy

Usman Wajid Object Oriented Programming Pointers continued . . . 9 / 11


Copy Constructor Example 5

class List {
private :
int * data ; int main () {
int size ;
public : List objectOne (5) ;
List ( int size ) {
this - > size = size ; objectOne . fill () ;
data = new int [ size ]; cout < < " obj1 : " ;
}
void fill () {
objectOne . print () ;
for ( int i =0; i < size ; i ++)
data [ i ] = i * i ; destroyList ( objectOne ) ;
}
void print () {
for ( int i =0; i < size ; i ++) cout < < " \ n \ nobj1 : " ;
cout < < data [ i ] < < " " ; objectOne . print () ;
}
~ List () { }
delete [] data ;
}
};

void destroyList ( List ObjectThree ) {}

Usman Wajid Object Oriented Programming Pointers continued . . . 10 / 11


Copy Constructor Example 5

class List {
private :
int * data ; int main () {
int size ;
public : List objectOne (5) ;
List ( int size ) {
this - > size = size ; objectOne . fill () ;
data = new int [ size ]; cout < < " obj1 : " ;
}
void fill () {
objectOne . print () ;
for ( int i =0; i < size ; i ++)
data [ i ] = i * i ; destroyList ( objectOne ) ;
}
void print () {
for ( int i =0; i < size ; i ++) cout < < " \ n \ nobj1 : " ;
cout < < data [ i ] < < " " ; objectOne . print () ;
}
~ List () { }
delete [] data ;
}
};

void destroyList ( List ObjectThree ) {}

Usman Wajid Object Oriented Programming Pointers continued . . . 10 / 11


Copy Constructor Example 6
class List {
private :
int * data ;
int size ;
public :
List ( int size ) {
int main () {
this - > size = size ;
data = new int [ size ]; List objectOne (5) ;
}
List ( List & obj ) { objectOne . fill () ;
size = obj . size ; cout < < " obj1 : " ;
data = new int [ size ]; objectOne . print () ;
for ( int i =0; i < size ; i ++)
data [ i ] = obj . data [ i ];
} destroyList ( objectOne ) ;
void fill () {
for ( int i =0; i < size ; i ++)
data [ i ] = i * i ; cout < < " \ n \ nobj1 : " ;
} objectOne . print () ;
void print () {
for ( int i =0; i < size ; i ++)
}
cout < < data [ i ] < < " " ;
}
~ List () {
delete [] data ;
}
};

void destroyList ( List ObjectThree ) {}

Usman Wajid Object Oriented Programming Pointers continued . . . 11 / 11


Copy Constructor Example 6
class List {
private :
int * data ;
int size ;
public :
List ( int size ) {
int main () {
this - > size = size ;
data = new int [ size ]; List objectOne (5) ;
}
List ( List & obj ) { objectOne . fill () ;
size = obj . size ; cout < < " obj1 : " ;
data = new int [ size ]; objectOne . print () ;
for ( int i =0; i < size ; i ++)
data [ i ] = obj . data [ i ];
} destroyList ( objectOne ) ;
void fill () {
for ( int i =0; i < size ; i ++)
data [ i ] = i * i ; cout < < " \ n \ nobj1 : " ;
} objectOne . print () ;
void print () {
for ( int i =0; i < size ; i ++)
}
cout < < data [ i ] < < " " ;
}
~ List () {
delete [] data ;
}
};

void destroyList ( List ObjectThree ) {}

Usman Wajid Object Oriented Programming Pointers continued . . . 11 / 11

You might also like