COMS W4995-1 COMS W4995-1 Lecture 7. LAN Switching: Bridges & Spanning Tree Protocol.
1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.
-
Upload
janis-gardner -
Category
Documents
-
view
214 -
download
1
Transcript of 1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.
![Page 1: 1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.](https://reader036.fdocuments.us/reader036/viewer/2022081211/56649e235503460f94b10208/html5/thumbnails/1.jpg)
1
COMS 261Computer Science I
Title: Classes
Date: November 7, 2005
Lecture Number: 28
![Page 2: 1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.](https://reader036.fdocuments.us/reader036/viewer/2022081211/56649e235503460f94b10208/html5/thumbnails/2.jpg)
2
Announcements
![Page 3: 1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.](https://reader036.fdocuments.us/reader036/viewer/2022081211/56649e235503460f94b10208/html5/thumbnails/3.jpg)
3
Review
• Classes– User defined data types
– Constructors• Default
– Constructors• With parameters
– Constructors• Parameters with default values
![Page 4: 1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.](https://reader036.fdocuments.us/reader036/viewer/2022081211/56649e235503460f94b10208/html5/thumbnails/4.jpg)
4
Outline
• Classes– Copy constructor
![Page 5: 1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.](https://reader036.fdocuments.us/reader036/viewer/2022081211/56649e235503460f94b10208/html5/thumbnails/5.jpg)
Classes
• Copy constructor– VEC v1(1.2f, 3.4f);
– VEC v2(v1);• Uses the copy constructor• Creates v2, an independent object that contains
a copy of the data members of v1
v1.x = 1.2;
v1.y = 3.4;
v2.x = 1.2;
v2.y = 3.4;
Same values asv1, but a differentobject
![Page 6: 1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.](https://reader036.fdocuments.us/reader036/viewer/2022081211/56649e235503460f94b10208/html5/thumbnails/6.jpg)
Copy Constructor
– Syntax error when compiling• VEC::VEC(VEC v) { … }
– Since, call by value requires we make a copy of the vector v when calling the copy constructor
– The copy constructor would be called over and over
int main () {VEC v1;VEC v2(v1); Call the copy constructor
Call by value make a copy of v1
![Page 7: 1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.](https://reader036.fdocuments.us/reader036/viewer/2022081211/56649e235503460f94b10208/html5/thumbnails/7.jpg)
Copy Constructor
• Copy constructor cannot us call by value
–Only other choice is call by reference• VEC::VEC(VEC& v) { … }
–Should the copy constructor ever change the data member values of the reference parameter?• No, it should only read the values but not
change them
• Make the parameter a const reference
![Page 8: 1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.](https://reader036.fdocuments.us/reader036/viewer/2022081211/56649e235503460f94b10208/html5/thumbnails/8.jpg)
Copy Constructor
• vec.h: definition file
• vec.cpp: implementation file
• Caution– If you don’t provide an implementation of the
copy constructor, the compiler will supply one• It may not do what you think it will
Run CodeWarrior vec01
![Page 9: 1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.](https://reader036.fdocuments.us/reader036/viewer/2022081211/56649e235503460f94b10208/html5/thumbnails/9.jpg)
Assignment Operator
• It would be nice to assign one VEC object to another– v1 = v2;
• To do this we must overload the assignment operator (=) to define a function when a VEC object is on both the lhs and the rhs
![Page 10: 1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.](https://reader036.fdocuments.us/reader036/viewer/2022081211/56649e235503460f94b10208/html5/thumbnails/10.jpg)
Assignment Operator
• Should there be any arguments?– Yes, the VEC we wish to assign
– Avoid making a copy, use call by reference
• Should the assignment operator change the RHS?– No, make it a const reference
– VEC& operator=(const VEC& v);
Run CodeWarrior vec03
![Page 11: 1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.](https://reader036.fdocuments.us/reader036/viewer/2022081211/56649e235503460f94b10208/html5/thumbnails/11.jpg)
Assignment Operator
• The assignment operator for this class does not behave the way the primitive data types do– They allow chaining
• A = B = C;
– What happens if we try to chain the VEC assignment operator?
– How do we get around this?