Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 159.234 Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p.209-212, 215,

Similar presentations


Presentation on theme: "1 159.234 Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p.209-212, 215,"— Presentation transcript:

1 Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p , 215,

2 2 #include using namespace std; class XClass{ // a conhtrive d example to illustrate use of this public: XClass(int id = 0) : ID(id) { // single arg constructor } XClass( int a, int b ){ // two arg constructor int tmp = a + b; this->common( tmp ); // not needed system does this-> automatically // same as just common(tmp); } void print(){ // "this" is automatically declared by the system for use in non static member functions cout << "this inside XClasses print member function is " << this << endl; XClass *ptr; ptr = this; // "this" is a pointer to the instantiated object // of which function print is a member XClass anotherXClass; anotherXClass = *this; } int getID(){ return this->ID; } // not needed - system does this-> automatically private: void common( int t ){ / a private member function only used by other members ID = t; } int ID; // some private data }; int main(){ XClass x1(90); // constructs an object XClass *xp; // declares a pointer xp = & x1; // xp now points at the x1 object cout << "ID of x1 is " << x1.getID() << endl; cout << "Address of x1 inside main is " << &x1 << endl; x1.print(); // invokes the print member function of x1 cout << "size of x1 is " << sizeof( x1 ) << endl; return 0; } Example Output: ID of x1 is 90 Address of x1 inside main is 0xbffffc68 this inside XClasses print member function is 0xbffffc68 size of x1 is 4 this is just a pointer to the instantiated object that is available inside non- static member functions of the object itself.

3 3 #include using namespace std; class Complex{ friend Complex operator+( const Complex & c1, const Complex & c2 ); friend ostream &operator<<( ostream & out, const Complex & c1 ); public: Complex( double real=0.0, double imag=0.0) : re(real), im(imag){} ~Complex(){} // empty destructor Complex( Complex const & c ){ // copy constructor re = c.re; im = c.im; } const Complex & operator=( const Complex & c ){ // assignment operator re = c.re; im = c.im; return *this; } void print( ostream & out ) const{ // utility used by output operator out << re << "," << im; } double abs(){ return sqrt( re * re + im * im ); } double Re(){ return re; } double Im(){ return im; } private: double re; double im; }; // addition operator definition for Complex operands Complex operator+( const Complex & c1, const Complex & c2 ){ Complex result( c1 ); result.re += c2.re; result.im += c2.im; return result; } // insertion stream operator, need not be a friend function if it does not access // private data directly ostream &operator<<( ostream & out, const Complex & c1 ){ //out << c1.re << "," << c1.im; c1.print( out ); return out; } int main(){ // test program for Complex class Complex a(1.0, 0.0); Complex b(0.0, 1.0); Complex c(-1.0); // im part will default to 0 Complex d(0.0); // im part will default to 0 Complex x; x = a + b; // this iswhat we want to work (needs + operator) //cout << x.re << "," << x.im << endl; // Illegal, data is private x.print( cout ); // should be 1,1 cout << endl; cout << c << endl; // this is what we want to work (needs output op) Complex y(0.0,0.0); x = y; // 2.0 as a double is automatically promoted to a Complex cout << x << endl; // should be 2,0 Complex c1, c2, c3, c4; c1 = c2 = c3 = c4 = x; // this is what we want to work (needs assignment op) cout << c1 << endl; // should be identical to x - ie 2,0 cout << c2 << endl; // also 2,0 return 0; } Output: 1,1 -1,0 2,0

4 4 Whenever we perform an assignment to an object, the class's assignment operator is invoked. Vect a, b, c, d; //… a = b; b = c = d; //multiple assignments The assignment operator must be a member function. Overloading the assignment operator

5 5 The Assignment Operator class Vect { // an elastic array public: Vect(int n); //n=size of the vector ( or array) ~Vect() {delete [] p;} const Vect &operator=(const Vect & v); private: int *p; int size; };

6 6 The Assignment Operator const Vect& Vect::operator=(const Vect &v){ if(this != &v) { //do nothing if assign to self delete[] p; p = new int[size=v.size]; assert(p); for (int i = 0;i

7 7 The Assignment Operator Always returns a const reference to the object being assigned. const Vect & operator=(const Vect & v); Why? a) it allows, several assignments to be chained together: Complex w, x, y, z; w = x = y = z = Complex(0,0); b) it is more efficient (a copy of the returned object does not have to be created and returned).

8 8 Other Assignment Operators If your application overloads the assignment operator, then the other forms of assignment (e.g., '+=', '-=', '/=', '*=',...) should be overloaded (for consistency make them member functions) just like the assignment operator.

9 9 The [] can be used on both sides of an assignment statement. Vect a(5); a[1] = 4; a[2] = a[1] * 3; Make sure to return a reference so that it may appear on the left-hand side of an assignment. It must be implemented as a member function. Overloading the [ ] Operator

10 10 Overloading the [] Operator We can create an array class that overloads [], and checks arrays bounds: class Vect { public: Vect(int n); ~Vect() {delete [] p;} int & operator[](cons int i); private: int *p; int size; };

11 11 Overloading the [] Operator Vect::Vect(int n) : size(n) { p = new int[size]; assert(p!=0); } int & Vect::operator[](const int i) { assert((i >= 0) && (i < size)); return p[i]; }

12 12 Summary Operator overloading uses: member functions, friend functions or non member functions. We have seen examples of overloading pre/postfix increment, binary (+, +=,…) > extraction and, =, []

13 13 ACM Student Magazine


Download ppt "1 159.234 Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p.209-212, 215,"

Similar presentations


Ads by Google