Presentation is loading. Please wait.

Presentation is loading. Please wait.

Plab – Exercise 5 C++: references, operator overloading, friends.

Similar presentations


Presentation on theme: "Plab – Exercise 5 C++: references, operator overloading, friends."— Presentation transcript:

1 Plab – Exercise 5 C++: references, operator overloading, friends

2 References A reference is an alias – an alternative name to an existing object. (1)int i = 10; (2)int& j = i; // j is a int reference // initialized only once ! (3)j += 5; // changes both i and j (4) int* k = (int*) malloc(…); (5)j = k; //error (6)j = *k; //ok

3 References vs. Pointers Must always refer to “legitimate” objects ­ i.e. allocated object (heap or stack). Cannot be assigned to change the object they refer to (same as array) The usage of references is as if they were the object themselves. What is the advantage in doing so? Usually implicitly implemented by the compiler as pointers  But not always

4 Passing arguments by value void swap(int a, int b) { int temp = a; a = b; b = temp; } …. int main(){ int x=3, y=7; swap(x, y); // still x == 3, y == 7 } “By value” arguments cannot be changed!

5 Passing arguments by pointer/reference void swap( int *a, int *b){ int temp = *a; *a = *b; *b = temp; } …. int main(){ int x=3, y=7; swap(&x, &y); // now x==7, y==3 } void swap( int& a, int& b) { int temp = a; a = b; b = temp; } …. int main(){ int x=3, y=7; swap(x, y); // x == 7, y == 3 } These two codes are equivalent !

6 Passing arguments int * func(int *var0, int &var1, int var2); By pointer By reference By value By pointer But it can be viewed as always passing “by value”! - The value can be pointer or reference!

7 Efficiency – avoid copying arguments Enables modifying variables outside a function Reference – why? But that can be done with pointers too! Everything that can be done with references, can be done with pointers - But some “dangerous” features of pointers cannot be done with references since they are “const addresses”. Easier to optimize by the compiler More convenient in many cases (see examples) Widely used !!!

8 Reference – more Like with pointers, don’t return a reference to a local variable. Why? You can return a pointer or a reference to a variable that will survive the function call, for example: -A heap variable (malloc, new, etc.) -A variable from a lower part of the stack.

9 Reference – example (1)struct Point { (2)int x,y; (3)Point(int i1,int i2){x=i1; y=i2;} (4)} (5)void add( Point& a, Point b ) { // a is reference, b is a copy (6)a.x += b.x; (7)a.y += b.y; (8)} (9)int main() { (10)Point p1(2,3), p2(4,5); (11)add(p1,p2); //note: we don’t send pointers! // p1 is now (6,8) (12)}

10 Operator overloading

11 Operator Overloading Operators like +, -, * are actually functions, and can be overloaded. These operators are for classes ! For example: -class Complex can have operators +,-,/,*, instead of add,sub(),…. -class Vector can have operator[] instead of elementAt(). -One can use > for I/O of his/her classes.

12 class Complex { Complex(double real, double img); inline Complex operator+(const Complex&) const; private: double m_real, m_img; }; // operator + Complex Complex::operator+(const Complex& c) const{ return Complex(m_real + c.m_real, m_img + c.m_img); } class Complex { Complex(double real, double img); inline Complex operator+(const Complex&) const; private: double m_real, m_img; }; // operator + Complex Complex::operator+(const Complex& c) const{ return Complex(m_real + c.m_real, m_img + c.m_img); } #include "complex.h" void f { Complex a(5,6), b(4,6), c(8,7); a = c+b; // a now is (12,13) a = c.operator+(b); //identical } #include "complex.h" void f { Complex a(5,6), b(4,6), c(8,7); a = c+b; // a now is (12,13) a = c.operator+(b); //identical } The right side argument, b, is the argument to the function. The left argument’s function is called.

13 // unary operator- Complex Complex::operator-() const { return Complex(-m_real,-m_img); } // binary operator- Complex Complex::operator-(const Complex& c) const { return Complex(m_real - c.m_real, m_img - c.m_img); } // unary operator- Complex Complex::operator-() const { return Complex(-m_real,-m_img); } // binary operator- Complex Complex::operator-(const Complex& c) const { return Complex(m_real - c.m_real, m_img - c.m_img); } #include "complex.h" void f { Complex a(5,6), b(4,6), c(8,7); a = -b; // a now is (-4,-6) a = c-b; // a now is (4,1) } #include "complex.h" void f { Complex a(5,6), b(4,6), c(8,7); a = -b; // a now is (-4,-6) a = c-b; // a now is (4,1) }

14 #include "mathVector.h" void f() { MathVector a(10); double x; x = a[1]; } #include "mathVector.h" void f() { MathVector a(10); double x; x = a[1]; } operator[] class MathVector { public: // the methods from previous slide // accessor const double& operator[](int c) const; private: int m_size; double *m_data; }; class MathVector { public: // the methods from previous slide // accessor const double& operator[](int c) const; private: int m_size; double *m_data; };

15 Friend function in a class: –Not a method of the class (function!) –have access to the class’s data members –declared inside the class scope class Complex { … friend void print( Complex c); } void print( Complex c ) { //print is not a method //here we have access to Complex’s data members printf(“%f %f\n”,c.m_real,c.m_img); } friend functions

16 Friend classes: –A class can allow other classes to access its private data members. –The friendship is one sided. class Complex { … friend class Algebric; } // Algebric class can access Complex’s data members void Algebric::print( Complex c ) { printf(“%f %f\n”,c.m_real,c.m_img); } friend classes

17 class Complex { … inline friend Complex& operator+( const Complex&, const Complex& ); } –Not a method, but can access the data members. –Usage as in the examples before operator overloading with friend functions


Download ppt "Plab – Exercise 5 C++: references, operator overloading, friends."

Similar presentations


Ads by Google