This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
|
cpp:vector_overloading_binary_operators [2017/05/05 13:38] gthanos |
cpp:vector_overloading_binary_operators [2019/05/16 11:14] gthanos |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ===== Υπερφόρτωση δυαδικών τελεστών που | + | ===== Υπερφόρτωση δυαδικών τελεστών που |
| Παρακάτω θα δούμε την υπερφόρτωση τελεστών που δεν μεταβάλλουν τον αριστερό τελεστέο. Οι τελεστές περιγράφονται στον παρακάτω πίνακα. | Παρακάτω θα δούμε την υπερφόρτωση τελεστών που δεν μεταβάλλουν τον αριστερό τελεστέο. Οι τελεστές περιγράφονται στον παρακάτω πίνακα. | ||
| ^ Τελεστής | ^ Τελεστής | ||
| - | | %%+%% | // | + | | %%+%% | // |
| | %%-%% | // | | %%-%% | // | ||
| | %%*%% | // | | %%*%% | // | ||
| - | | %%+%% | // | + | | %%+%% | // |
| | %%+%% | // | | %%+%% | // | ||
| | %%-%% | // | | %%-%% | // | ||
| Line 14: | Line 14: | ||
| | %%/%% | // | | %%/%% | // | ||
| | % | // | | % | // | ||
| - | | %%<< | + | | %%<< |
| - | | %%>> | + | | %%>> |
| + | | %%==%% | ||
| + | | %%!=%% | ||
| <WRAP center round info 80%> | <WRAP center round info 80%> | ||
| - | Μία φιλική μέθοδο και ένα μέλος της κλάσης δεν είναι δυνατόν να υλοποιούν την ίδια λειτουργικότητα υπερφόρτωσης. Θα πρέπει να επιλέξετε έναν από τους δύο τρόπους για να υλοποιήσετε τη λειτουργία της υπερφόρτωσης. | + | Μία φιλική μέθοδος και ένα μέλος της κλάσης δεν είναι δυνατόν να υλοποιούν την ίδια λειτουργικότητα υπερφόρτωσης. Θα πρέπει να επιλέξετε έναν από τους δύο τρόπους για να υλοποιήσετε τη λειτουργία της υπερφόρτωσης. |
| </ | </ | ||
| Line 29: | Line 31: | ||
| class Vector { | class Vector { | ||
| int *array; | int *array; | ||
| - | | + | int size; |
| | | ||
| public: | public: | ||
| - | Vector(unsigned | + | Vector(int length=0); |
| - | Vector(const Vector &v); | + | Vector(const Vector& v); |
| Vector(const Vector *v); | Vector(const Vector *v); | ||
| ~Vector(); | ~Vector(); | ||
| - | | + | int length() const; |
| - | int & | + | int & |
| - | int find(int a) const; | + | int find(int a) const; |
| - | // if not element not found | + | |
| | | ||
| - | void print() const; | + | void print() const; |
| - | void print(string &msg) const; | + | void print(string &msg) const; |
| /* binary operators that don't modify left operand */ | /* binary operators that don't modify left operand */ | ||
| - | | + | |
| - | Vector operator+(const Vector &v) const; | + | Vector operator+(const Vector& v) const; |
| - | //friend Vector operator+(const Vector &v1, const Vector v2); | + | //friend Vector operator+(const Vector& v1, const Vector& v2); // same as above |
| - | Vector operator-(const Vector &v) const; | + | Vector operator-(const Vector& v) const; |
| - | Vector operator*(Vector &v) const; | + | Vector operator*(const Vector& v) const; |
| - | Vector operator+(int a) const; | + | |
| - | friend Vector operator+(int a, const Vector &v); | + | friend Vector operator+(int a, const Vector& v); // Returns a new vector. Each element of vector equals the sum of v[i] + a |
| - | Vector operator-(int a) const; | + | Vector operator-(int a) const; |
| - | Vector operator*(int a) const; | + | Vector operator*(int a) const; |
| - | friend Vector operator*(int a, const Vector &v); | + | friend Vector operator*(int a, const Vector& v); // Returns a new vector. Each element of vector equals the product: |
| - | Vector operator/ | + | Vector operator/ |
| - | Vector operator%(int a) const; | + | Vector operator%(int a) const; |
| | | ||
| - | | + | |
| - | | + | |
| - | Vector operator >> (int a) const; | + | |
| - | //friend Vector operator>> | + | |
| | | ||
| + | Vector operator << (int a) const; | ||
| + | //friend Vector operator<< | ||
| + | Vector operator >> (int a) const; | ||
| + | //friend Vector operator>> | ||
| }; | }; | ||
| - | Vector:: | + | Vector:: |
| size = length; | size = length; | ||
| array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
| Line 76: | Line 81: | ||
| } | } | ||
| - | Vector:: | + | Vector:: |
| size = v.length(); | size = v.length(); | ||
| array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
| Line 102: | Line 107: | ||
| } | } | ||
| - | unsigned | + | int Vector:: |
| return size; | return size; | ||
| } | } | ||
| - | int & | + | int & |
| if(pos> | if(pos> | ||
| cerr << " | cerr << " | ||
| Line 133: | Line 138: | ||
| void Vector:: | void Vector:: | ||
| cout << msg; | cout << msg; | ||
| - | | + | |
| - | cout << array[i]; | + | |
| - | if(i==size-1) | + | |
| - | cout << endl; | + | |
| - | else | + | |
| - | cout << ", "; | + | |
| - | } | + | |
| } | } | ||
| - | void Vector:: | + | Vector& |
| if(array!=NULL) | if(array!=NULL) | ||
| delete [] array; | delete [] array; | ||
| Line 152: | Line 151: | ||
| } | } | ||
| for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
| - | array[i] = v.valueAt(i); | + | array[i] = v.valueAt(i); |
| + | return *this; | ||
| } | } | ||
| - | Vector Vector:: | + | Vector Vector:: |
| int length; | int length; | ||
| if (size > v.length()) | if (size > v.length()) | ||
| Line 174: | Line 174: | ||
| /* | /* | ||
| - | Vector operator+(const Vector &v1, const Vector v2) { | + | Vector operator+(const Vector& v1, const Vector v2) { |
| int length; | int length; | ||
| if (v1.size > v2.size) | if (v1.size > v2.size) | ||
| Line 192: | Line 192: | ||
| }*/ | }*/ | ||
| - | Vector Vector:: | + | Vector Vector:: |
| int length; | int length; | ||
| if (size > v.length()) | if (size > v.length()) | ||
| Line 210: | Line 210: | ||
| } | } | ||
| - | Vector Vector:: | + | Vector Vector:: |
| - | Vector n(size); | + | int length; |
| - | for(int i=0; i<size; i++) | + | if (size < v.size) |
| - | n.array[i] = array[i] * v.array[i]; | + | length = size; |
| + | else | ||
| + | length = v.size; | ||
| + | Vector n(length); | ||
| + | for(int i=0; i<length; i++) | ||
| + | n.array[i] = array[i] * v.array[i]; | ||
| return n; | return n; | ||
| } | } | ||
| - | Vector | + | Vector operator+(const Vector& v, int a) { |
| - | Vector n(size); | + | Vector n(v.size); |
| - | for(int i=0; i<size; i++) | + | for(int i=0; i<v.size; i++) |
| - | n.array[i] = array[i] + a; | + | n.array[i] = v.array[i] + a; |
| return n; | return n; | ||
| } | } | ||
| - | Vector operator+(int a, const Vector &v) { | + | Vector operator+(int a, const Vector& v) { |
| Vector n(v); | Vector n(v); | ||
| for(int i=0; i< | for(int i=0; i< | ||
| Line 246: | Line 251: | ||
| } | } | ||
| - | Vector operator*(int a, const Vector &v) { | + | Vector operator*(int a, const Vector& v) { |
| Vector n(v); | Vector n(v); | ||
| for(int i=0; i< | for(int i=0; i< | ||
| Line 267: | Line 272: | ||
| return n; | return n; | ||
| } | } | ||
| + | |||
| + | bool Vector:: | ||
| + | if( size != v.size ) | ||
| + | return false; | ||
| + | for(int i=0; i<size; i++) | ||
| + | if( array[i] != v.array[i] ) | ||
| + | return false; | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | bool Vector:: | ||
| + | return !(*this == v); | ||
| + | } | ||
| + | |||
| /* | /* | ||
| - | Vector operator<< | + | Vector operator<< |
| Vector n(v); | Vector n(v); | ||
| - | int *array_new = new (nothrow) int [n.size+1]; | ||
| - | if(array_new==NULL) { | ||
| - | cerr << " | ||
| - | exit(-1); | ||
| - | } | ||
| for(int i=0; i< | for(int i=0; i< | ||
| - | | + | n.array[i] |
| - | array_new[n.size] | + | |
| - | n.size++; | + | |
| - | delete [] n.array; | + | |
| - | n.array = array_new; | + | |
| return n; | return n; | ||
| }*/ | }*/ | ||
| Line 286: | Line 296: | ||
| Vector Vector:: | Vector Vector:: | ||
| Vector n(*this); | Vector n(*this); | ||
| - | int *array_new = new (nothrow) int [n.size+1]; | ||
| - | if(array_new==NULL) { | ||
| - | cerr << " | ||
| - | exit(-1); | ||
| - | } | ||
| for(int i=0; i< | for(int i=0; i< | ||
| - | | + | n.array[i] |
| - | array_new[n.size] | + | |
| - | n.size++; | + | |
| - | delete [] n.array; | + | |
| - | n.array = array_new; | + | |
| return n; | return n; | ||
| } | } | ||
| - | /*Vector operator>> | + | /*Vector operator>> |
| - | int pos = v.find(a); | + | |
| - | if(pos< | + | |
| - | return v; | + | |
| Vector n(v); | Vector n(v); | ||
| - | | + | for(int i=0; i< |
| - | if(array_new==NULL) { | + | n.array[i] >>= a; |
| - | cerr << " | + | |
| - | exit(-1); | + | |
| - | } | + | |
| - | | + | |
| - | | + | |
| - | array_new[i] = n.array[i]; | + | |
| - | if(i==pos) | + | |
| - | continue; | + | |
| - | if(i>pos) | + | |
| - | array_new[i-1] = n.array[i]; | + | |
| - | } | + | |
| - | n.size--; | + | |
| - | delete [] n.array; | + | |
| - | n.array | + | |
| return n; | return n; | ||
| }*/ | }*/ | ||
| Vector Vector:: | Vector Vector:: | ||
| - | int pos = find(a); | ||
| - | if(pos< | ||
| - | return *this; | ||
| Vector n(*this); | Vector n(*this); | ||
| - | | + | for(int i=0; i< |
| - | if(array_new==NULL) { | + | n.array[i] >>= a; |
| - | cerr << " | + | |
| - | exit(-1); | + | |
| - | } | + | |
| - | | + | |
| - | | + | |
| - | array_new[i] = n.array[i]; | + | |
| - | if(i==pos) | + | |
| - | continue; | + | |
| - | if(i>pos) | + | |
| - | array_new[i-1] = n.array[i]; | + | |
| - | } | + | |
| - | n.size--; | + | |
| - | delete [] n.array; | + | |
| - | n.array | + | |
| return n; | return n; | ||
| } | } | ||
| Line 359: | Line 326: | ||
| Vector f = v; | Vector f = v; | ||
| v.print(msg=" | v.print(msg=" | ||
| + | if( f == v ) | ||
| + | cout << "f == v " << endl; | ||
| + | else | ||
| + | cout << "f != v " << endl; | ||
| + | | ||
| + | | ||
| Vector k; | Vector k; | ||
| k = f + v; | k = f + v; | ||
| - | k.print(msg=" | + | k.print(msg=" |
| k = f - v; | k = f - v; | ||
| - | k.print(msg=" | + | k.print(msg=" |
| k = f * v; | k = f * v; | ||
| - | k.print(msg=" | + | k.print(msg=" |
| k = f + 2; | k = f + 2; | ||
| - | k.print(msg=" | + | k.print(msg=" |
| + | k = 2 + f; | ||
| + | k.print(msg=" | ||
| k = f - 2; | k = f - 2; | ||
| - | k.print(msg=" | + | k.print(msg=" |
| k = f * 2; | k = f * 2; | ||
| - | k.print(msg=" | + | k.print(msg=" |
| k = f / 2; | k = f / 2; | ||
| - | k.print(msg=" | + | k.print(msg=" |
| k = f % 2; | k = f % 2; | ||
| - | k.print(msg=" | + | k.print(msg=" |
| - | f = f << | + | f = f << |
| - | f.print(msg=" | + | f.print(msg=" |
| - | f = f << 7; | + | f = f >> 2; |
| - | f.print(msg=" | + | f.print(msg=" |
| - | f = f >> 6; | + | |
| - | | + | cout << |
| - | | + | |
| - | | + | cout << |
| } | } | ||
| </ | </ | ||