Andy Wang Object Oriented Programming in C COP 3330 Automatic Functions In C some default functions are automatically built Constructor if you dont provide one Destructor if you dont provide one ID: 641345
Download Presentation The PPT/PDF document "Automatics, Copy Constructor, and Assign..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Automatics, Copy Constructor, and Assignment Operator
Andy Wang
Object Oriented Programming in C++
COP 3330Slide2
Automatic Functions
In C++, some default functions are automatically built
Constructor // if you don’t provide one
Destructor // if you don’t provide one
Copy Constructor
Assignment operator=Slide3
Copy Constructor
Just like a constructor
Invoked implicitly when a COPY of an existing object is created; in particular, when
An object is defined to have the value of another object of the same type
An object is passed by value into function
An object is returned (by value) from a function
Examples
Fraction f1, f2(3,4);
Fraction f3 = f2; // a copy of f2 is created to initialize f3
f
1 = f2; // calls the assignment operator, since f1 and f2
// already existSlide4
Declaring and Defining
A copy constructor creates a new object, initialized as a copy of another existing object
Always has one parameter of the same type
Passed by reference
Since passing by value will invoke a copy constructor
Format
className
(
const
className
&);
The
const
is not required, but a good idea for protecting the original
Example
Fraction(
const
Fraction &f);Slide5
Shallow vs. Deep Copy
The default version makes a
shallow copy
Each member data location is copied
Sufficient for classes like Fraction
Only has a private numerator and denominator
May not be sufficient for pointer member data
Example: Phonebook
The
entryList
pointer will be copied and will point to the original dynamically allocated memorySlide6
Deep Copy
Creates a copy of dynamically allocated memory
Directory::Directory(
const
Directory &d) {
maxsize
=
d.maxsize
;
currentsize
=
d.currentsize
;
entryList
= new Entry[
d.maxsize
];
for (
int
j = 0; j <
currentsize
;
j++
)
entryList
[j] =
d.entryList
[j];
}Slide7
Visualize the Execution
i
nt
main() {
…
stack (high address)
main
h
eap (low address)Slide8
Visualize the Execution
i
nt
main() {
Directory d;
…
i
nt
maxSize
= 5
i
nt
curentSize
= 0Entry *
entryList =
Entry[5]
stack (high address)
main
h
eap (low address)
dSlide9
Shallow Copy
i
nt
main() {
Directory d;
Directory d2 = d;
i
nt
maxSize
= 5
i
nt
curentSize
= 0Entry *
entryList =
Entry[5]
stack (high address)
main
h
eap (low address)
d
i
nt
maxSize
= 5
i
nt
curentSize
= 0
Entry *
entryList
=Slide10
Problems of Shallow Copy
Potential inconsistencies
Non dynamically allocated variables are copied
Dynamically allocated variables point to the same memory region
Double free
Runtime errorSlide11
Deep Copy
i
nt
main() {
Directory d;
Directory d2 = d;
i
nt
maxSize
= 5
i
nt
curentSize
= 0Entry *
entryList =
Entry[5]
stack (high address)
main
h
eap (low address)
d
i
nt
maxSize
= 5
i
nt
curentSize
= 0
Entry *
entryList
=
Entry[5]Slide12
Assignment Operator
o
perator= is similar to the copy constructor
Called when one object is assigned to another
Fraction f1, f2;
f
1 = f2;
Assignment operator also has to make a copy
The default version makes a shallow copy
A deep copy needs to be overloaded by the userSlide13
Copy Constructor vs. Assignment Operator
Copy Constructor
Initializes a new object as a copy of an existing one
Initialize data for the first time
Has no return value
Operator=
Sets the current state of an object to that of another existing object
May need to free up old dynamically allocated memory
Returns the value that was assigned, to support
a
= b = c = 4;
Must be a member functionSlide14
Chained Assignments
a
= b = c = 4;
Same as a = (b = (c =
4));
Same as a = (b = 4);
Thus, (c = 4) returns 4 by reference
Need the ability to refer to an object from inside the objectSlide15
The this
pointer
Inside a member function
An object can access its own address using the
this
keyword
To return the object itself by reference, return the target of the this pointer, or *this
Like a copy constructor, operator= will take one parameter of the same object type
Examples
Directory &operator=(
const
Directory &);
Fraction &operator=(
const
Fraction &);Slide16
Directory Operator= Implementation
Directory &Directory::operator=(
const
Directory &d) {
if (this == &d) return *this; // don’t self copy
// if you don’t check this, this can self destruct…
delete []
entryList
;
maxsize
=
d.maxsize
;
currentsize = d.currentsize;
entryList = new Entry[
d.maxsize]; for (int
j = 0; j <
currentsize
;
j++
)
entryList[j] =
d.entryList[j]; return *this;
}Slide17
Phonebook Example
http://www.cs.fsu.edu/~myers/cop3330/examples/copyconst/phonebook
/Slide18
directory.h
#include “
entry.h
”
c
lass Directory {
public:
…
Directory(
const
Directory &);
Directory& operator=(
const Directory &);
… private:
…};Slide19
directory.cpp
…
Directory
::Directory(
const
Directory &d) {
maxsize
=
d.maxsize
;
currentsize
=
d.currentsize
;
entryList = new Entry[d.maxsize]; for (int
j = 0; j < currentsize; j++)
entryList[j] = d.entryList[j];}Slide20
directory.cpp
Directory &Directory::operator=(
const
Directory &d) {
if (this == &d) return *this; // don’t self copy
delete []
entryList
;
maxsize
=
d.maxsize
;
currentsize
= d.currentsize;
entryList = new Entry[d.maxsize
]; for (int j = 0; j <
currentsize
;
j++
)
entryList[j] = d.entryList
[j]; return *this;}
…Slide21
menu.cpp
…
Int
main() {
Directory d;
d.Insert
();
Directory d2 = d; // copy constructor
d.DisplayDirectory
(); d2.DisplayDirectory;
d.Update();
d.DisplayDirectory(); d2.DisplayDirectory;}Slide22
Fraction Example
http://www.cs.fsu.edu/~myers/cop3330/examples/copyconst/frac
/
No need for copy constructor and operator=
Fraction does not have dynamically allocated memory
Automatic versions are sufficient with the use of shallow copiesSlide23
frac.h
c
lass Fraction {
public:
…
Fraction(
const
Fraction &f);
Fraction &operator=(
const
Fraction &f);
…
private:
…};Slide24
frac.cpp
…
Fraction::Fraction(
const
Fraction &f) {
numerator =
f.numerator
;
denominator =
f.denominator
;
}
Fraction &Fraction::operator=(
const
Fraction &f) {
numerator = f.numerator;
denominator = f.denominator; return *this;
}
…