Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.

Similar presentations


Presentation on theme: "1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29."— Presentation transcript:

1 1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29

2 2 Announcements

3 3 Review Classes –User defined data types –Constructors Of all types –Copy Constructor –Assignment operator

4 4 Outline Classes –Assignment operator

5 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

6 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 –void operator=(const VEC& v); Run CodeWarrior vec03

7 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? –VEC& operator=(const VEC& v);

8 Assignment Operator VEC& operator=(const VEC& v); Which VEC reference will we return? –Can not be v, as v is a const reference to a VEC, not a reference to a VEC! –It must be the VEC corresponding to the lhs The object that invoked the assignment operator, the operator= member function Which raises the question

9 Assignment Operator How do we access the object that invokes any member function? –So far we have accessed the data members of the object, but not the object itself Constructors Accessors Facilitators VEC::VEC (float xval, float yval) { x = xval; y = yval; } How do we access the object that contains x and y?

10 Assignment Operator Likewise The invoking object within a member function is referred to as this –That is correct, this Run CodeWarrior vec04 void VEC::setX (float xval) { x = xval; } Run CodeWarrior vec03

11 Outputting Vectors The print member function of the VEC class is rather lame –It always inserts the stream into cout –Modify print() so that it takes a parameter of the output stream we wish to insert the vector VEC v(1.2f, 3.4f); v.print(cout); Run CodeWarrior vec05

12 Outputting Vectors The real power of C++ now come into play –We can use the print function with any output stream!! For example, a file Run CodeWarrior vec06

13 Outputting Vectors The print member function of the VEC class is still rather lame –A different sequence of statements to output an object of the VEC class –How can we make vectors act like the primitive types? int a = 5; cout << a << endl; VEC v(1.2f, 3.4f); v.print(cout);

14 Outputting Vectors What do we need to do to allow this in our programs? –Overload the output stream insertion operator (<<) ostream object on the lhs VEC on the rhs VEC v(1.2f, 3.4f); cout << v << endl;

15 Outputting Vectors The insertion operator is global –It is not implemented as a member function of any class Place its prototype outside the class defination, but inside the class header file, vec.h Run CodeWarrior vec07 void operator<<(ostream& strm, VEC& v);


Download ppt "1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29."

Similar presentations


Ads by Google