cpp:vector_overloading_binary_operators
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
cpp:vector_overloading_binary_operators [2017/05/12 06:39] – gthanos | cpp:vector_overloading_binary_operators [2021/05/07 14:34] – gthanos | ||
---|---|---|---|
Line 1: | Line 1: | ||
===== Υπερφόρτωση δυαδικών τελεστών που μπορούν να υλοποιηθούν ως μέλη της κλάσης ή ως φιλικές συναρτήσεις ===== | ===== Υπερφόρτωση δυαδικών τελεστών που μπορούν να υλοποιηθούν ως μέλη της κλάσης ή ως φιλικές συναρτήσεις ===== | ||
- | Παρακάτω | + | Ο παρακάτω |
^ Τελεστής | ^ Τελεστής | ||
- | | %%+%% | // | + | | %%+%% | // |
| %%-%% | // | | %%-%% | // | ||
| %%*%% | // | | %%*%% | // | ||
- | | %%+%% | // | + | | %%+%% | // |
| %%+%% | // | | %%+%% | // | ||
| %%-%% | // | | %%-%% | // | ||
Line 14: | Line 14: | ||
| %%/%% | // | | %%/%% | // | ||
| % | // | | % | // | ||
- | | %%<< | + | | %%<< |
- | | %%>> | + | | %%>> |
- | | %%==%% | + | | %%==%% |
- | | %%!=%% | + | | %%!=%% |
<WRAP center round info 80%> | <WRAP center round info 80%> | ||
- | Μία φιλική μέθοδο και ένα μέλος της κλάσης δεν είναι δυνατόν να υλοποιούν την ίδια λειτουργικότητα υπερφόρτωσης. Θα πρέπει να επιλέξετε έναν από τους δύο τρόπους για να υλοποιήσετε τη λειτουργία της υπερφόρτωσης. | + | Μία φιλική μέθοδος και ένα μέλος της κλάσης δεν είναι δυνατόν να υλοποιούν την ίδια λειτουργικότητα υπερφόρτωσης. Θα πρέπει να επιλέξετε έναν από τους δύο τρόπους για να υλοποιήσετε τη λειτουργία της υπερφόρτωσης. |
</ | </ | ||
Line 31: | 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; |
| | ||
- | 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:: | + | Vector:: |
size = length; | size = length; | ||
array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
Line 81: | Line 81: | ||
} | } | ||
- | Vector:: | + | Vector:: |
size = v.length(); | size = v.length(); | ||
array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
Line 107: | Line 107: | ||
} | } | ||
- | unsigned | + | int Vector:: |
return size; | return size; | ||
} | } | ||
- | int & | + | int & |
if(pos> | if(pos> | ||
cerr << " | cerr << " | ||
Line 138: | 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 157: | 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 179: | 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 197: | Line 192: | ||
}*/ | }*/ | ||
- | Vector Vector:: | + | Vector Vector:: |
int length; | int length; | ||
if (size > v.length()) | if (size > v.length()) | ||
Line 215: | 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 251: | 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 273: | Line 273: | ||
} | } | ||
- | bool Vector:: | + | bool Vector:: |
if( size != v.size ) | if( size != v.size ) | ||
return false; | return false; | ||
Line 282: | Line 282: | ||
} | } | ||
- | 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 305: | 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 386: | Line 334: | ||
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.txt · Last modified: 2021/05/24 06:42 (external edit)