cpp:vector_overloading_binary_operators
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| cpp:vector_overloading_binary_operators [2019/05/16 07:53] – gthanos | cpp:vector_overloading_binary_operators [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ===== Υπερφόρτωση δυαδικών τελεστών που μπορούν να υλοποιηθούν ως μέλη της κλάσης ή ως φιλικές συναρτήσεις ===== | ===== Υπερφόρτωση δυαδικών τελεστών που μπορούν να υλοποιηθούν ως μέλη της κλάσης ή ως φιλικές συναρτήσεις ===== | ||
| - | Παρακάτω | + | Ο παρακάτω |
| ^ Τελεστής | ^ Τελεστής | ||
| - | | %%+%% | // | + | | %%+%% | // |
| | %%-%% | // | | %%-%% | // | ||
| | %%*%% | // | | %%*%% | // | ||
| Line 14: | Line 14: | ||
| | %%/%% | // | | %%/%% | // | ||
| | % | // | | % | // | ||
| - | | %%<< | + | | %%<< |
| - | | %%>> | + | | %%>> |
| - | | %%==%% | + | | %%==%% |
| - | | %%!=%% | + | | %%!=%% |
| <WRAP center round info 80%> | <WRAP center round info 80%> | ||
| Line 23: | Line 23: | ||
| </ | </ | ||
| - | <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 | + | |
| | | ||
| - | 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 & | + | Vector& operator=(const Vector& v); // assignment operator |
| - | 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; |
| | | ||
| - | bool operator==(const Vector &v) const; | + | bool operator==(const Vector& v) const; |
| - | bool operator!=(const Vector &v) const; | + | bool operator!=(const Vector& v) const; |
| - | + | | |
| - | Vector operator << (int a) const; | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| | | ||
| + | Vector operator << (int a) const; | ||
| + | //friend Vector operator<< | ||
| + | Vector operator >> (int a) const; | ||
| + | //friend Vector operator>> | ||
| }; | }; | ||
| - | Vector:: | + | #endif |
| + | </ | ||
| + | |||
| + | <code cpp Vector.cpp> | ||
| + | #include " | ||
| + | |||
| + | Vector:: | ||
| size = length; | size = length; | ||
| array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
| Line 81: | Line 90: | ||
| } | } | ||
| - | Vector:: | + | Vector:: |
| size = v.length(); | size = v.length(); | ||
| array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
| Line 107: | Line 116: | ||
| } | } | ||
| - | unsigned | + | int Vector:: |
| return size; | return size; | ||
| } | } | ||
| - | int & | + | int & |
| if(pos> | if(pos> | ||
| cerr << " | cerr << " | ||
| Line 138: | Line 147: | ||
| void Vector:: | void Vector:: | ||
| cout << msg; | cout << msg; | ||
| - | | + | |
| - | cout << array[i]; | + | |
| - | if(i==size-1) | + | |
| - | cout << endl; | + | |
| - | else | + | |
| - | cout << ", "; | + | |
| - | } | + | |
| } | } | ||
| - | Vector & | + | Vector& Vector:: |
| if(array!=NULL) | if(array!=NULL) | ||
| delete [] array; | delete [] array; | ||
| Line 161: | Line 164: | ||
| } | } | ||
| - | Vector Vector:: | + | Vector Vector:: |
| int length; | int length; | ||
| if (size > v.length()) | if (size > v.length()) | ||
| Line 180: | Line 183: | ||
| /* | /* | ||
| - | 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 198: | Line 201: | ||
| }*/ | }*/ | ||
| - | Vector Vector:: | + | Vector Vector:: |
| int length; | int length; | ||
| if (size > v.length()) | if (size > v.length()) | ||
| Line 216: | Line 219: | ||
| } | } | ||
| - | 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 252: | Line 260: | ||
| } | } | ||
| - | 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 274: | Line 282: | ||
| } | } | ||
| - | bool Vector:: | + | bool Vector:: |
| if( size != v.size ) | if( size != v.size ) | ||
| return false; | return false; | ||
| Line 283: | Line 291: | ||
| } | } | ||
| - | bool Vector:: | + | bool Vector:: |
| return !(*this == v); | 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 306: | Line 305: | ||
| 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; | ||
| } | } | ||
| + | </ | ||
| + | <code cpp VectorUsage.cpp> | ||
| + | #include " | ||
| int main() { | int main() { | ||
| Line 387: | Line 346: | ||
| 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 >> |
| - | f.print(msg=" | + | f.print(msg=" |
| - | f = f >> | + | |
| - | f.print(msg=" | + | |
| - | f = f >> 6; | + | |
| - | f.print(msg=" | + | |
| if( f == v ) | if( f == v ) | ||
| cout << "f == v " << endl; | cout << "f == v " << endl; | ||
cpp/vector_overloading_binary_operators.1557993202.txt.gz · Last modified: 2019/05/16 06:53 (external edit)
