cpp:vector_overloading_binary_operators2
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| cpp:vector_overloading_binary_operators2 [2017/05/12 08:30] – gthanos | cpp:vector_overloading_binary_operators2 [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ===== Υπερφόρτωση δυαδικών τελεστών μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης ===== | ===== Υπερφόρτωση δυαδικών τελεστών μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης ===== | ||
| - | Παρακάτω θα δούμε την υπερφόρτωση τελεστών που μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης. Οι τελεστές περιγράφονται στον παρακάτω πίνακα. | + | Σε αυτή την ενότητα, |
| - | + | ||
| - | ^ Τελεστής | + | ^ Τελεστής |
| - | | %%+=%% | + | | %%=%% | // |
| - | | %%-=%% | + | |
| - | | %%+=%% | + | |
| - | | %%-=%% | + | |
| - | | %%*=%% | + | |
| - | | %%*=%% | + | |
| - | | %%/ | + | |
| - | | | + | |
| - | | %%<< | + | |
| - | | %%>> | + | |
| | %%[ ]%% | // | | %%[ ]%% | // | ||
| + | | %%+=%% | ||
| + | | %%+=%% | ||
| + | | %%-=%% | ||
| + | | %%-=%% | ||
| + | | %%*=%% | ||
| + | | %%*=%% | ||
| + | | %%/ | ||
| + | | %% %=%% | // | ||
| + | | %%<< | ||
| + | | %%>> | ||
| - | <code cpp Vector.cpp> | + | <code cpp Vector.hpp> |
| #include < | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| using namespace std; | using namespace std; | ||
| + | |||
| + | #ifndef _VECTOR_HPP_ | ||
| + | #define _VECTOR_HPP_ | ||
| 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 | // if not element not found | ||
| Line 38: | Line 42: | ||
| 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; |
| - | Vector operator-(const Vector &v) const; | + | Vector operator*(Vector& |
| - | Vector operator*(Vector &v) const; | + | |
| | | ||
| /* binary operators that modify left operand */ | /* binary operators that modify left operand */ | ||
| - | Vector & operator+=(const Vector &v); | + | |
| - | Vector & operator+=(int a); | + | |
| - | Vector & operator-=(const Vector &v); | + | Vector& operator+=(int a); |
| - | Vector & operator-=(int a); | + | Vector& operator-=(const Vector& v); |
| - | Vector & operator*=(const Vector &v); | + | Vector& operator-=(int a); |
| - | Vector & operator*=(int a); | + | Vector& operator*=(const Vector& v); |
| - | Vector & operator/ | + | Vector& operator*=(int a); |
| - | Vector & operator%=(int a); | + | Vector& operator/ |
| - | Vector & operator<< | + | Vector& operator%=(int a); |
| - | Vector & operator>> | + | Vector& operator<< |
| + | Vector& operator>> | ||
| int & | int & | ||
| | | ||
| }; | }; | ||
| - | Vector:: | + | #endif |
| + | </ | ||
| + | |||
| + | <code cpp Vector.cpp> | ||
| + | #include " | ||
| + | |||
| + | Vector:: | ||
| size = length; | size = length; | ||
| array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
| Line 71: | Line 81: | ||
| } | } | ||
| - | Vector:: | + | Vector:: |
| size = v.length(); | size = v.length(); | ||
| array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
| Line 97: | Line 107: | ||
| } | } | ||
| - | unsigned | + | int Vector:: |
| return size; | return size; | ||
| } | } | ||
| - | int & | + | int & |
| if(pos> | if(pos> | ||
| cerr << " | cerr << " | ||
| Line 137: | Line 147: | ||
| } | } | ||
| - | Vector & | + | Vector& Vector:: |
| if(array!=NULL) | if(array!=NULL) | ||
| delete [] array; | delete [] array; | ||
| Line 151: | Line 161: | ||
| } | } | ||
| - | Vector Vector:: | + | Vector Vector:: |
| int length; | int length; | ||
| if (size > v.length()) | if (size > v.length()) | ||
| Line 169: | Line 179: | ||
| } | } | ||
| - | Vector Vector:: | + | Vector Vector:: |
| int length; | int length; | ||
| if (size > v.length()) | if (size > v.length()) | ||
| Line 187: | Line 197: | ||
| } | } | ||
| - | Vector & Vector:: | + | Vector& Vector:: |
| if(v.length() > size) { | if(v.length() > size) { | ||
| Vector n(v); | Vector n(v); | ||
| Line 199: | Line 209: | ||
| } | } | ||
| - | Vector & Vector:: | + | Vector& Vector:: |
| for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
| array[i] += a; | array[i] += a; | ||
| Line 205: | Line 215: | ||
| } | } | ||
| - | Vector & Vector:: | + | Vector& Vector:: |
| if(v.length() > size) { | if(v.length() > size) { | ||
| Vector n(v); | Vector n(v); | ||
| Line 217: | Line 227: | ||
| } | } | ||
| - | Vector & Vector:: | + | Vector& Vector:: |
| for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
| array[i] -= a; | array[i] -= a; | ||
| Line 223: | Line 233: | ||
| } | } | ||
| - | Vector & Vector:: | + | Vector& Vector:: |
| if( v.size != size ) | if( v.size != size ) | ||
| return *this; | return *this; | ||
| Line 232: | Line 242: | ||
| } | } | ||
| - | Vector & Vector:: | + | Vector& Vector:: |
| for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
| array[i] *= a; | array[i] *= a; | ||
| Line 238: | Line 248: | ||
| } | } | ||
| - | Vector & Vector:: | + | Vector& Vector:: |
| for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
| array[i] /= a; | array[i] /= a; | ||
| Line 244: | Line 254: | ||
| } | } | ||
| - | Vector & Vector:: | + | Vector& Vector:: |
| for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
| array[i] %= a; | array[i] %= a; | ||
| Line 250: | Line 260: | ||
| } | } | ||
| - | int & | + | Vector& Vector:: |
| - | | + | |
| + | | ||
| + | return *this; | ||
| } | } | ||
| - | Vector & Vector:: | + | Vector& Vector:: |
| - | int *array_new = new (nothrow) int [size+1]; | + | |
| - | if(array_new==NULL) { | + | |
| - | cerr << " | + | |
| - | exit(-1); | + | |
| - | } | + | |
| for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
| - | | + | array[i] |
| - | array_new[size] | + | |
| - | | + | |
| - | delete [] array; | + | |
| - | array = array_new; | + | |
| } | } | ||
| - | Vector | + | int & |
| - | | + | return array[pos]; |
| - | if(pos< | + | |
| - | | + | |
| - | int *array_new = new (nothrow) int [size-1]; | + | |
| - | if(array_new==NULL) { | + | |
| - | cerr << " | + | |
| - | exit(-1); | + | |
| - | } | + | |
| - | for(int i=0; i<size; i++) { | + | |
| - | if(i< | + | |
| - | array_new[i] = array[i]; | + | |
| - | if(i==pos) | + | |
| - | continue; | + | |
| - | if(i> | + | |
| - | array_new[i-1] = array[i]; | + | |
| - | } | + | |
| - | size--; | + | |
| - | delete [] array; | + | |
| - | array = array_new; | + | |
| } | } | ||
| + | </ | ||
| + | <code cpp VectorUsage.cpp> | ||
| + | #include " | ||
| int main() { | int main() { | ||
| Line 324: | Line 313: | ||
| f.print(msg=" | f.print(msg=" | ||
| | | ||
| - | | + | cout << "v[2]: " << |
| - | int k = f[2]; | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| } | } | ||
| </ | </ | ||
cpp/vector_overloading_binary_operators2.1494577809.txt.gz · Last modified: 2017/05/12 07:30 (external edit)
